Page 1 of 1

isset code problem

Posted: Wed Jan 06, 2010 8:37 pm
by bharanidharanit
When the user logging into this page, http://localhost/snw/users/login.php and when the users are not properly navigated from the default page means, it will send back the user to the default page.
I used this coding. But even when the user logins from the default page also, it is sending back to the default page.

Code: Select all

if (!isset($txtusrname) || !isset($txtusrpwd)){
    header("Location: http://localhost/snw/default.php");
}
 

Re: isset code problem

Posted: Wed Jan 06, 2010 8:53 pm
by jason
I can assure you of three things:

1. We'll need to see more code, but I already suspect that the problem is that these variables are in fact set.
2. You posted the topic in the wrong forum. I moved it for you to the proper place.
3. Obviously, we can't access http://localhost/, just in case you weren't aware.

Re: isset code problem

Posted: Wed Jan 06, 2010 9:02 pm
by bharanidharanit
This is my whole coding. I know that you can't access my localhost, i actually describes you what i done.

Code: Select all

<?php
//checking whether the users are coming from login page
 
if (!isset($txtusrname) || !isset($txtusrpwd)){
    header("Location: http://localhost/snw/default.php");
}
else
{
//coverting the field values to variables
 
    $usrname = addslashes($_POST['txtusrname']);
    $usrpwd = md5($_POST['txtusrpwd']);
echo ($usrname);
echo ($usrpwd);
//setting the database connection variables
$dbhost = "localhost";
$dbuser = "root";
$dbpwd = "admin";
$dbDatabase = "db_snw";
 
//connecting to the database
$db = mysql_connect("$dbhost","$dbuser","$dbpwd") or die("Error Connecting to the Database");
mysql_select_db("$dbDatabase",$db) or die("Couldn't Select the database");
$result = mysql_query("select * from tbl_login where Username='$username' and Userpwd='$userpwd'");
 
//checking the rows are present
$rowcheck = mysql_num_rows($result);
if($rowcheck>0){
    while ($row = mysql_fetch_array($result)){
        //start the session variable
        session_start();
        session_register('Username');
        echo ('Success');
    }
}
else{
    echo ("Incorrect Username and Password");
}
}
?>

Re: isset code problem

Posted: Wed Jan 06, 2010 9:31 pm
by jason
Yeah, they aren't set, so it will always send them away.

Check $_POST['txtusrname'] and company instead.

Re: isset code problem

Posted: Thu Jan 07, 2010 5:38 am
by bharanidharanit
jason wrote:Yeah, they aren't set, so it will always send them away.

Check $_POST['txtusrname'] and company instead.
I am very beginner in php. That code is too i learnt from tutorial. So can you pls make me more clear. What part do i need to change.
I am already passing txtusrname and txtpassword from my default page.

Re: isset code problem

Posted: Thu Jan 07, 2010 5:48 am
by bharanidharanit
ya thankyou this works for me.

Code: Select all

if (!isset($_POST['txtusrname']) || !isset($_POST['txtusrpwd'])){
    header("Location: http://localhost/snw/default.php");
}
But each and every time when the page loads, my 2 textboxes are filled up with some default texts which i entered at my first login. How to rectify it?

Re: isset code problem

Posted: Thu Jan 07, 2010 7:44 am
by jason
bharanidharanit wrote:ya thankyou this works for me.

Code: Select all

if (!isset($_POST['txtusrname']) || !isset($_POST['txtusrpwd'])){
    header("Location: http://localhost/snw/default.php");
}
But each and every time when the page loads, my 2 textboxes are filled up with some default texts which i entered at my first login. How to rectify it?

Well, let's start with a congrats! You posted you didn't understand, and then 10 minutes later, you solved the problem and posted a new one. =) More importantly, you posted your solution. So, thank you. Other people that might have the same problem will now be able to get the solution. All to often this doesn't happen.

Next, I hope you discovered that with a little bit of work and a nudge in the right direction, you can solve your own problem. That's programming. Even if you are new, it's important to learn early on the mantra of "Try it and find out."

Now, let's move on to your next problem.

Obviously, I can't give you any concrete answers because I don't see the code. However, my immediate guess would be that your browser is merely remember the data that you are entering in the first time. View the source of the page and see if the HTML contains the filled in values, or if it's just appearing in you browser.

Re: isset code problem

Posted: Sun Jan 10, 2010 2:02 pm
by bharanidharanit
Is this the good and secured login system. Will the query works very faster for the users in thousands. Here's my code

Code: Select all

<?PHP
$username = $_POST['txtusrname'];
$userpwd = $_POST["txtusrpwd"];
 
//echo ($username);
//echo ($userpwd);
//connecting to database
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "admin";
    $dbname = "db_snw";
    $dbconn = mysql_connect($dbhost,$dbuser,$dbpass) or die("Can't connect database");  
    mysql_select_db($dbname,$dbconn) or die("can't select table");
 
//removing special characters
    $username = mysql_real_escape_string($username);
 
//selecting row for the current username
 
    $query = "SELECT userpwd,salt
         FROM tbl_users
         WHERE username = '$username';";
    $result = mysql_query($query);
    
    if(mysql_num_rows($result) < 1)
    {
        echo ("No Such User Exists");
    }
 // echo ($query);
 // echo ($result);
    
//checking if the user is true
    $userdata = mysql_fetch_array($result,MYSQL_ASSOC);
    $hash = sha1( $userdata['salt'] . sha1($userpwd) );
    echo ($userdata['userpwd']);
//  echo ($hash);
    //incorrect password
    if($hash != $userdata['userpwd'])   
    {
        echo("Incorrect Login");
        header('Location: default.php');
    }
    else
    {
        echo ("Login Successful");  
    }
 
?>