Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > PHP
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 05-02-2005, 05:32 AM   #1 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Login Script against two tables

Hi, I have followed sde's login script with a couple of changes. The main difference is that I 1st want my script to check a tutor database, then if the username & password is not there, check the student database....

Reason tutor will be directed to a different page or have different access rights....

Heres my login.php code

PHP Code:
if ($_REQUEST['DoLogin']=='yes')
  {
  include 'login.inc.php';
  }
?>

Form accepting username and password goes here... 
eg.
<form action="login.php?DoLogin=yes" method="post" name="logindetails" 

<?PHP
exit;
    }   
  }       
?>
So because no variables are set the form appears, the user enters their details and they are processed by login.inc.php:

PHP Code:
<?PHP 
// start session
session_start();

// convert username and password from _POST or _SESSION
if($_POST["username"])
{
  
$username=$_POST["username"];
  
$password=$_POST["password"];  
}
elseif(
$_SESSION["username"])
{
  
$username=$_SESSION["username"];
  
$password=$_SESSION["password"];


// start and register session variables
session_register("username");
session_register("password");

//Connect to database include file
include 'connect.inc';

echo 
"User: $username<hr>";

// query for a user/pass match 
$result=mysql_query("SELECT * FROM tutor
  WHERE username='" 
$username "' and password='" $password "'"); 

$row=mysql_fetch_array($result);
$num=mysql_num_rows($result);

// print login form and exit if failed.
if($num 1){
  
session_destroy();
  echo 
"You are not authenticated.  Please login.<br><br>";
  exit;


if (
mysql_num_rows($result)>0
   {
    
// We have a tutor
    
$fullname=$row['firstname'] .' ' $row['lastname'];
    echo(
"Hello tutor: $fullname<p>");
    if (
$row['password']=$password
     {
         
//Tutor is authenticated
     
echo("Tutor you have logged in correctly");
     } else { echo (
"try again");}     
     
   } else {
   
         
// Check for student
        
$result=mysql_query("SELECT * FROM student 
    WHERE username='" 
$username "' and password='" $password "'");

    
$row=mysql_fetch_array($result);
    
    if (
$row['username']=$username
          {
          
// Confirm
       
$fullname=$row['firstname'] . ' ' $row['lastname'];
       echo(
"Hello Student: $fullname<p>");
       if (
$row['password']=$password
            {
             
// Student is authenticated
         
echo("Student you have logged in correctly");
            } else { echo (
"try again");}    
           }
         }

$loggedin=true;

?>
The tutor section works but a student login just returns the unauthorised access message.

Help is appreciated, have searched the net for logging onto one table then checking another with nothing.
__________________

Last edited by BUFFY : 05-03-2005 at 01:08 AM.
BUFFY is offline   Reply With Quote
Old 05-02-2005, 05:50 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,681
redhead is on a distinguished road
Your if() statements are way off.
PHP Code:
if ($row['password']=$password)
...
if (
$row['username']=$username
...
if (
$row['password']=$password
... 
should be:
PHP Code:
if ($row['password']==$password)
...
if (
$row['username']==$username
...
if (
$row['password']==$password
... 
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-02-2005, 05:56 AM   #3 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
I changed the three but get the same result, tutor is to able log in, but student doesn't.
Its always the unauthorised message when its a username and password not in the tutor table is entered.
__________________
BUFFY is offline   Reply With Quote
Old 05-02-2005, 06:00 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,681
redhead is on a distinguished road
Just noticed your first error in this:
PHP Code:
$result=mysql_query("SELECT * FROM tutor 
  WHERE username='" 
$username "' and password='" $password "'"); 

$row=mysql_fetch_array($result); 
$num=mysql_num_rows($result); 

// print login form and exit if failed. 
if($num 1){ 
  
session_destroy(); 
  echo 
"You are not authenticated.  Please login.<br><br>"
  exit; 

at this point every student login will fail.
PHP Code:
$result=mysql_query("SELECT * FROM tutor 
  WHERE username='" 
$username "' and password='" $password "'"); 

$row=mysql_fetch_array($result); 
$num=mysql_num_rows($result); 

if(
$num 1){ 
   
$result=mysql_query("SELECT * FROM student 
    WHERE username='" 
$username "' and password='" $password "'"); 

    
$row=mysql_fetch_array($result);
    
$num=mysql_num_rows($result); 
    if(
$num 1){ // print login form and exit if failed. 
        
session_destroy(); 
        echo 
"You are not authenticated.  Please login.<br><br>"
        exit; 
    } 

    if (
$row['username']==$username
    { 
       
// we have a student 
       
$fullname=$row['firstname'] . ' ' $row['lastname']; 
       echo(
"Hello Student: $fullname<p>"); 
       echo(
'done');     
       if (
$row['password']==$password
       { 
             
// we have an authenticated student 
             
echo("Student you have logged in correctly"); 
       } 
       else 
       { 
          echo (
"try again");
       }     
    } 

else

    
// We have a tutor 
    
$fullname=$row['firstname'] .' ' $row['lastname']; 
    echo(
"Hello tutor: $fullname<p>"); 
    if (
$row['password']==$password
     { 
     
// we have an authenticated tutor 
     
echo("Tutor you have logged in correctly"); 
     } else { echo (
"try again");}     
   }

$loggedin=true
might be a better aproach.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-02-2005, 06:18 AM   #5 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Your pretty on to it to come straight out with that in a couple of minutes Thanks.

I've tried it but now it shows a blank page so am in the process of debugging...
__________________
BUFFY is offline   Reply With Quote
Old 05-02-2005, 03:13 PM   #6 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
If anyone has any more ideas it may help.
__________________
BUFFY is offline   Reply With Quote
Old 05-02-2005, 03:17 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
can u give a little more detail on your issue? a little more than 'blank page' ?

what should it do?
what is it doing?
what are you doing when it is doing?

maybe a code snippet.

welcome to the site.
__________________
testing 1 2 3
sde is online now   Reply With Quote
Old 05-02-2005, 03:46 PM   #8 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Basically I want the "user" to enter their logon details on login.php, this then POST's the details back to login.php this time with a variable set to yes. The variable call's login.inc.php to perform the authentication to the database.

At first it was working but then i realised it wasn't checking the values against the database just returning the variables. I extended the mysql_query to look more like yours sde It started checking the database but only for the first table queried. The other table's login details resulted in a error statement returned... "User is not authenticated" etc.

So then Ive changed it to suit redheads suggestion which resulted in nothing working I see the logic but am in the process of debugging (im new, it takes me hours) to see if any of it works and which part doesn't.

It should allow either a tutor or a student from different tables to login into a session by checking their username and password against the table feilds. Then just return the fullname (firstname + lastname from table) of the tutor/student back to login.php

The code I am using is what is posted earlier, I changed it now to look like the code redhead supplied....

Login.php (not changed)
PHP Code:
//This code has not been changed, it works fine.
<?PHP
//require_once 'connect.inc';

if ($_REQUEST['ShowRegister']=='yes')
  {
  include 
'reg.inc.php'
  exit; 
  }

if (
$_REQUEST['DoRegister']=='yes')
  {
  
// if failed then exit; 
  
}

if (
$_REQUEST['DoLogin']=='yes')
  {
    include 
'login.inc.php';
  }

  if (!
$loggedin==true
  {
  if (
$loginrequired==true or 
     !
$nologinrequired=="yes"
    {
?>
<form action="login.php?DoLogin=yes" method="post" name="logindetails" class="textblue">
 <table width="90%" border="0" cellspacing="1px" cellpadding="5px">
  <tr> 
   <td colspan="2" class="heading4">Login</td>
  </tr>
  <tr> 
   <td>Username</td>
   <td><input name="username" type="text" id="username" size="40" maxlength="80"></td>
  </tr>
  <tr> 
   <td>Password</td>
   <td><input name ="password" type="password" id="password" size="40" maxlength="20"></td>
  </tr>
  <tr> 
   <td colspan="2" class="textblue">If you do not have a user name, please register <a href="<?PHP echo $_SERVER['PHP_SELF'] . '?ShowRegister=yes' ?>">here</a> 
   </td>
  </tr>
  <tr> 
   <td colspan="2"><input type="submit" name="Login" value="Login"/></td>
  </tr>
 </table>
</form>
    
<?PHP
       
exit;
    } 
  }       
?>
Login.inc.php (changed)
PHP Code:
<?PHP 
//LOGIN.INC.PHP 

// start session
session_start();

// convert username and password from _POST or _SESSION
if($_POST["username"])
{
  
$username=$_POST["username"];
  
$password=$_POST["password"];  
}
elseif(
$_SESSION["username"])
{
  
$username=$_SESSION["username"];
  
$password=$_SESSION["password"];


// start and register session variables 
session_register("username");
session_register("password");

//Connect to database include file
include 'connect.inc';

 
$result=mysql_query("SELECT * FROM tutor
  WHERE username='" 
$username "' and password='" $password "'");

$row=mysql_fetch_array($result);
$num=mysql_num_rows($result);

if(
$num 1){
   
$result=mysql_query("SELECT * FROM student
    WHERE username='" 
$username "' and password='" $password "'");

    
$row=mysql_fetch_array($result);
    
$num=mysql_num_rows($result);
    if(
$num 1){ // print login form and exit if failed.
        
session_destroy();
        echo 
"You are not authenticated.  Please login.<br><br>";
        exit;
    }

    if (
$row['username']==$username)
    {
       
// we have a student
       
$fullname=$row['firstname'] . ' ' $row['lastname'];
       echo(
"Hello Student: $fullname<p>");
       echo(
'done');     
       if (
$row['password']==$password)
       {
             
// we have an authenticated student
             
echo("Student you have logged in correctly");
       }
       else
       {
          echo (
"try again");
       }     
    }
}
else
{
    
// We have a tutor
    
$fullname=$row['firstname'] . ' ' $row['lastname'];
    echo(
"Hello tutor: $fullname<p>");
    if (
$row['password']==$password)
     {
     
// we have an authenticated tutor
     
echo("Tutor you have logged in correctly");
     } else { echo (
"try again");}     
   }
}
$loggedin=true;
Im just cutting bits out to found out what works and whats causing the blank page error.... (nothing gets displayed in login.php)



AND thank you for the welcome, you guys/girls are really good here!
__________________
BUFFY is offline   Reply With Quote
Old 05-02-2005, 04:13 PM   #9 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
UPDATE:

Removed a curly bracket at the bottom and the details load now, but they are completely wrong. It showed what it was meant to before now its all messed up....

eg:
Log in using tutor details: (This used to work)
You are not authenticated. Please login.

Log in using students details: (This never worked)
Hello Tutor: Try again

I think my original -first- code was on the right track but with a couple of major errors.
__________________
BUFFY is offline   Reply With Quote
Old 05-02-2005, 05:30 PM   #10 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Hold the phone

It seems to be working.... I changed a couple of things and dont want to question it!

Only part that doesn't work is if you enter in details not in the database it just shows the login.php page and an empty space where the error message is meant to be (i.e error isn't working)

I tired this code at the bottom before the loggedin variable, it worked when the input was wrong, but showed up under the student details when they logged in successfully:

PHP Code:
//Destroy session if no results are found
if($num == 0){
  
session_destroy();
  echo 
"<br><br>You are not authenticated. Please login.<br><br>";
  exit;

Thanks all for your input... I got to make sure it carries through with the session but it seems okay atm.

PHP Code:
<?PHP
// start session
session_start();

// convert username and password from _POST or _SESSION
if($_POST["username"])
{
  
$username=$_POST["username"];
  
$password=$_POST["password"];  
}
elseif(
$_SESSION["username"])
{
  
$username=$_SESSION["username"];
  
$password=$_SESSION["password"];
}

// start and register session variables
session_register("username");
session_register("password");

//Connect to database include file
include 'connect.inc';

//Echo user name for testing purposes
echo "User: $username<hr>";

// query for a user/pass match
$result=mysql_query("SELECT * FROM tutor
  WHERE username='" 
$username "' and password='" $password "'");

$row=mysql_fetch_array($result);
$num=mysql_num_rows($result);

if (
mysql_num_rows($result)>0)
   {
    
// We have a tutor
    
$fullname=$row['firstname'] .' ' $row['lastname'];
    echo(
"Hello tutor: $fullname<p>");
    if (
$row['password']==$password)
     {
     
//tutor is authenticated
     
echo("tutor you have logged in correctly");
     } else { echo (
"try again");}    
    
   } else {
   
   
// Check for student
    
$result=mysql_query("SELECT * FROM student
    WHERE username='" 
$username "' and password='" $password "'");

    
$row=mysql_fetch_array($result);
    
    if (
$row['username']==$username)
          {
          
// Confirm
       
$fullname=$row['firstname'] . ' ' $row['lastname'];
       echo(
"Hello student: $fullname<p>");
       if (
$row['password']==$password)
           {
             
// student is authenticated
         
echo("student you have logged in correctly");
           } else { echo (
"try again");}    
           }
         }

$loggedin=true;

?>
__________________

Last edited by BUFFY : 05-02-2005 at 05:55 PM.
BUFFY is offline   Reply With Quote
Old 05-02-2005, 08:18 PM   #11 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 637
DJMaze is on a distinguished road
i'm sorry but the above code can be hacked anywhere i want.
I hope it's for study purposes only.

Else give me note and i will write a good tutorial PHP5 based (with a PHP4 code hack)
__________________
DJMaze is offline   Reply With Quote
Old 05-02-2005, 09:32 PM   #12 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
good job buffy .. i got home and came help but you seem to be all fixed now.

how long have you been working with php? is it for work? school? fun?

i wouldn't worry about dj's comments that much. even an advanced programmer could spend large amounts of time securing code depending on the sensitivity level of the site, so at this point in your development, i wouldn't worry about it.

besides, critisism with no explanation or examples is pretty much worthless. anyone could make that same comment about almost any login code examples posted. i'm getting used to seeing these sorts of responses now.

( we still love u djm )
__________________
testing 1 2 3
sde is online now   Reply With Quote