Page 1 of 1

php multi-level login system not working!

Posted: Tue Aug 19, 2008 10:56 pm
by coldfiretech
Okay i am trying to make a multi-level login system. I have 3 fields in the users table, login, password, and subtype.

Depending on subtype i want to login to one of 3 different panels.
I cannot get this to work for the life of me!!! Please help!

Code: Select all

 
<?php
$db_host = 'localhost';
$db_user = '****';
$db_pass = '*****';
$db_db = '******';
$con = mysql_connect($db_host, $db_user, $db_pass) or die('MySQl Connection Error:'.mysql_error());
    mysql_select_db($db_db, $con) or die('MySQL Error: Cannot select table');
$user = $_POST['login'];
$passwd = $_POST['password'];
$sql0 = "SELECT * FROM users";
$results = mysql_query($sql0,$con);
while($row = mysql_fetch_array($results))
if ($user == $row['login'] && $passwd == $row['password']) 
{
$subtype = $row['subtype'];
 
            //typecheck
                        if ($subtype="atype") {
                        print "<META HTTP-EQUIV=\"refresh\" content=\"2; URL=/*****/a.php\">";
                } 
                        elseif ($subtype="btype") {
                        print "<META HTTP-EQUIV=\"refresh\" content=\"2; URL=/******r/b.php\">";
                }
                        elseif ($subtype="ctype") {
                        print "<META HTTP-EQUIV=\"refresh\" content=\"2; URL=/******/c.php\">";
                }
//end of typecheck
 
} 
else {
print "<META HTTP-EQUIV=\"refresh\" content=\"2; URL=/index.html\">";
}
mysql_close ($con);
?>
 

Re: php multi-level login system not working!

Posted: Tue Aug 19, 2008 11:13 pm
by nowaydown1
You're using an assignment operator (=) instead of a comparison one (==) in your if checks. Also, why not just pass the username and password to the database and let it do the leg work on checking for the credentials. Looping over every row in the table is going to get ugly quick once you have a fair amount of records.

Hope that helps.

Re: php multi-level login system not working!

Posted: Tue Aug 19, 2008 11:17 pm
by coldfiretech
I already changed the = to == still didnt work.. That and could you show me and example of what you were saying about the other thing... Im new to php. ;)

Re: php multi-level login system not working!

Posted: Wed Aug 20, 2008 12:00 am
by nowaydown1
This is an example of using the database to do the legwork instead of looping over the rows. I just hacked this together really quick so it's possible there are errors with it. I meant it to be more of a learning example than a practical implementation.

Code: Select all

 
<?php
 
$db_host = 'localhost';
$db_user = '****';
$db_pass = '*****';
$db_db = '******';
$con = mysql_connect($db_host, $db_user, $db_pass) or die('MySQL Connection Error:'.mysql_error());
mysql_select_db($db_db, $con) or die('MySQL Error: Cannot select table');
 
$user = mysql_real_escape_string($_POST['login']);
$passwd = mysql_real_escape_string($_POST['password']);
 
if(!empty($user) && !empty($passwd)) {
    $checkUserAccount = "SELECT subtype FROM users WHERE login='$user' AND passwd ='$passwd' LIMIT 1";
    $result = mysql_query($checkUserAccount);
    if(!$result) {
        die('There was a problem running the query: ' . mysql_error());
    } else {
        $resultRow = mysql_fetch_array($result);
        switch($resultRow["subtype"]) {
            case "atype":
                 print "<META HTTP-EQUIV=\"refresh\" content=\"2; URL=/*****/a.php\">";
                 break;
            case "btype":
                 print "<META HTTP-EQUIV=\"refresh\" content=\"2; URL=/******r/b.php\">";
                 break;
            case "ctype":
                 print "<META HTTP-EQUIV=\"refresh\" content=\"2; URL=/******/c.php\">";
                 break;
            default:
                 print "<META HTTP-EQUIV=\"refresh\" content=\"2; URL=/index.html\">";
                 break;
        }
    }
} else {
    die('Please provide both a username and password.');
}
 
mysql_close ($con);
?>