Jump to content

[SOLVED] Do you actually understand how your login works?


Trium918

Recommended Posts

Come to think of it, I really don't!

 

I need help understanding how my log in

process actually works. Thank in advance!

 

I am trying to add $_SESSION['members_id'] = $members_id;

to my log in.

 

The user enters their data into the form. The submit button has

a name attribute called sublogin. The form is then submitted to

a php script by using the POST method. That script is below.

<?php
if (isset($_POST['sublogin']))
#They have just tried logging in
{
    $user_name= trim($_POST['user_name']);
    $password = trim($_POST['password']);

   #Checks that username is in database and password is correct 
   $result = login($_POST['user_name'], $_POST['password']); #Wil I need another variable?

   #Check error codes 
   if($result == 0){
      // unsuccessful login
      echo "<p class='genmed'>You could not be logged in. 
            You must be logged in to view this page.</p>";
  echo "<p class='genmed'> $user_name $password</p>";
      exit;
   }   
   
   // if they are in the database register the user id
   $valid_user = $user_name;
   $_SESSION['valid_user']= $valid_user; 

   $_SESSION['members_id'] = $members_id; #Wil I need to add  another variable to the login function?
   
   $password_str = $password;
   $password_str = md5($password_str);
   $_SESSION['password'] = $password_str;
   	   
}
?>

 

Here is the log in function which is used in

the script above.

<?php
function login($user_name, $password)
// check username and password with db
// if yes, return true
// else return false
{
  // connect to db
  $conn = db_connect();
  if (!$conn)
    return 0;

  $result = mysql_query("SELECT * FROM members_info WHERE  user_name='$user_name' AND password=MD5('$password')");
  
  if ($result){
  
  $sql1="SELECT last_visit FROM members_info WHERE user_name='$user_name'";
  $result1=mysql_query($sql1);
  $row=mysql_fetch_assoc($result1);
  $last=$row['last_visit'];
  
  #Display date as 5/8/2007 format
  $last = date('n/d/Y', strtotime($last));  
  
  $_SESSION['last']=$last;   //use the session variable to hold the previous last visit date.


  $sql="UPDATE members_info SET last_visit=NOW() WHERE user_name='$user_name'";   //now only update it
  $result2=mysql_query($sql);  
  }
  
  if (!$result)
     return 0;
  
  if (mysql_num_rows($result)>0)
     return 1;
  else 
     return 0;
}
?>

Link to comment
Share on other sites

What you need to do is look at parts of code that you do not understand. Whatever it is, go to php.net or google and type it in. Then read whatever comes up and it should help you understand better until finally, the whole thing will make sense.  :)

Link to comment
Share on other sites

What you need to do is look at parts of code that you do not understand. Whatever it is, go to php.net or google and type it in. Then read whatever comes up and it should help you understand better until finally, the whole thing will make sense.  :)

 

I am trying to add $_SESSION['members_id'] = $members_id;

to my log in.

 

Link to comment
Share on other sites

It would help if you had session_start() at the top of your script.

 

Just about to say that ;)

 

I was also about to say this:

#Wil I need to add  another variable to the login function?

Have you tried it without/with another variable? I suppose you haven't been able to without session_start() but try it with now.

Link to comment
Share on other sites

It would help if you had session_start() at the top of your script.

 

How many sessions do I have to start? There is

one already started

 

At the top of every page if it is not included with a session already started.

Link to comment
Share on other sites

It would help if you had session_start() at the top of your script.

 

How many sessions do I have to start? There is

one already started

 

At the top of every page if it is not included with a session already started.

 

I got it working by doing this:

What could go wrong with the way

that I have?

<?php
function login($user_name, $password)
// check username and password with db
// if yes, return true
// else return false
{
  // connect to db
  $conn = db_connect();
  if (!$conn)
    return 0;

  $result = mysql_query("SELECT * FROM members_info WHERE  user_name='$user_name' 
  													AND password=MD5('$password')");

  if ($result){
  
  $sql1="SELECT members_id,last_visit FROM members_info WHERE user_name='$user_name'";
  $result1=mysql_query($sql1);
  $row=mysql_fetch_assoc($result1);
  
  $last=$row['last_visit'];	
      $membersid=$row['members_id'];
  
      $_SESSION['membersid'] = $membersid;
  
  #Display date as 5/8/2007 format
  $last = date('n/d/Y', strtotime($last));  
  
  $_SESSION['last']=$last;   //use the session variable to hold the previous last visit date.


  $sql="UPDATE members_info SET last_visit=NOW() WHERE user_name='$user_name'";   //now only update it
  $result2=mysql_query($sql);  
  }
  
  if (!$result)
     return 0;
  
  if (mysql_num_rows($result)>0)
     return 1;
  else 
     return 0;
}
?>

Link to comment
Share on other sites

What could go wrong with the way

that I have?

 

We can't see ANY call to session_start() in ANY of your code. This needs to be placed in ALL files that you intend to use the $_SESSION array in.

 

One thing I will say about your login script is that it over complicates things. Its also got quite a bit of redundant code. For instance, you run two separate queries against the same table, why?

 

Also, putting the actual login in a function (at least the way you have implemented it) actually limits you in some ways.

 

I could rewrite your login in a much simpler manor but thats not going to help you understand the process. Your very likely to need to make customizations to your login in the future and unless you fully understand how / why it works your going to need to ask for help everytime.

Link to comment
Share on other sites

What could go wrong with the way

that I have?

 

One thing I will say about your login script is that it over complicates things. Its also got quite a bit of redundant code. For instance, you run two separate queries against the same table, why?

 

 

I understand having a session started at the start of

every page intended to use them.

 

I wrote it so that I could keep up with the user's

last log in. I was unaware of the redundant.

<?php
function login($user_name, $password)
// check username and password with db
// if yes, return true
// else return false
{
  // connect to db
  $conn = db_connect();
  if (!$conn)
    return 0;

  $result = mysql_query("SELECT * FROM members_info WHERE  user_name='$user_name' 
  													AND password=MD5('$password')");

  if ($result){
  
  $sql1="SELECT members_id,last_visit FROM members_info WHERE user_name='$user_name'";
  $result1=mysql_query($sql1);
  $row=mysql_fetch_assoc($result1);
  
  $last=$row['last_visit'];	
      $membersid=$row['members_id'];
  
      $_SESSION['membersid'] = $membersid;
  
  #Display date as 5/8/2007 format
  $last = date('n/d/Y', strtotime($last));  
  
  $_SESSION['last']=$last;   //use the session variable to hold the previous last visit date.


  $sql="UPDATE members_info SET last_visit=NOW() WHERE user_name='$user_name'";   //now only update it
  $result2=mysql_query($sql);  
  }
  
  if (!$result)
     return 0;
  
  if (mysql_num_rows($result)>0)
     return 1;
  else 
     return 0;
}
?>

Link to comment
Share on other sites

Why wouldn't this work better?

 

<?php
$conn = db_connect(); // why connect inside the function and not globally?

function login($user_name, $password)
// check username and password with db
// if yes, return true
// else return false
{
    $result = mysql_query("SELECT * FROM members_info WHERE  user_name='$user_name' 
  													AND password=MD5('$password') LIMIT 1");

  if (mysql_num_rows($result)>0){
  $row=mysql_fetch_assoc($result);

          $_SESSION['membersid'] = $row['members_id'];
  
  #Display date as 5/8/2007 format
  $last = date('n/d/Y', strtotime($row['last_visit']));  
  
  $_SESSION['last']=$last;   //use the session variable to hold the previous last visit date.


  $sql="UPDATE members_info SET last_visit=NOW() WHERE membersid='" . $_SESSION['membersid'] . "'";   //now only update it
  mysql_query($sql) OR return false;  
          return true;
  }
  
   return false;
}
?>

Link to comment
Share on other sites

Why wouldn't this work better?

 

<?php
$conn = db_connect(); // why connect inside the function and not globally?

function login($user_name, $password)
// check username and password with db
// if yes, return true
// else return false
{
    $result = mysql_query("SELECT * FROM members_info WHERE  user_name='$user_name' 
  													AND password=MD5('$password') LIMIT 1");

  if (mysql_num_rows($result)>0){
  $row=mysql_fetch_assoc($result);

          $_SESSION['membersid'] = $row['members_id'];
  
  #Display date as 5/8/2007 format
  $last = date('n/d/Y', strtotime($row['last_visit']));  
  
  $_SESSION['last']=$last;   //use the session variable to hold the previous last visit date.


  $sql="UPDATE members_info SET last_visit=NOW() WHERE membersid='" . $_SESSION['membersid'] . "'";   //now only update it
  mysql_query($sql) OR return false;  
          return true;
  }
  
   return false;
}
?>

 

Must be an error some where. I am getting a white page.

Link to comment
Share on other sites

It could be you are inconsistent with your members_id. Which is it? members_id or membersid that can mean a world of difference, also I am not sure if you are trying to implement it or not. Either way figure that you you have a working function.

Link to comment
Share on other sites

It could be you are inconsistent with your members_id. Which is it? members_id or membersid that can mean a world of difference, also I am not sure if you are trying to implement it or not. Either way figure that you you have a working function.

 

It is this part of the code. When I commented it out

it worked.

<?php
  $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='" . $_SESSION['membersid'] . "'";   //now only update it
  mysql_query($sql) or return false; 
?>

Link to comment
Share on other sites

Well sounds like you know where the problem is, now fix it.

 

Ok, it is this line here. Does the or return false has to

be in there are what? It works fine with out it

I was just asking.

<?php
mysql_query($sql) or return false; 
?>

Link to comment
Share on other sites

It doesn't have to, but if something is wrong with the update statement, which it seemed like there was due to the member_id inconsistency. Change that to be OR DIE (mysql_error()) and see what is up. Code is no good if it is flawed with inconsistent and wrongly coded programming.

Link to comment
Share on other sites

It doesn't have to, but if something is wrong with the update statement, which it seemed like there was due to the member_id inconsistency. Change that to be OR DIE (mysql_error()) and see what is up. Code is no good if it is flawed with inconsistent and wrongly coded programming.

 

Everything seems to work!

Last question before solved.

 

Does the script needs to be changed.

<?php
  #Check error codes 
   if($result == 0){
      // unsuccessful login
      echo "<p class='genmed'>You could not be logged in. 
            You must be logged in to view this page.</p>";
  echo "<p class='genmed'> $user_name $password</p>";
      exit;
   }   
?>

 

to this

<?php
  #Check error codes 
   if($result === false){
      // unsuccessful login
      echo "<p class='genmed'>You could not be logged in. 
            You must be logged in to view this page.</p>";
  echo "<p class='genmed'> $user_name $password</p>";
      exit;
   }   
?>

 

Since there is no number being returned

 

return 0

return 1 etc..

 

 

 

Link to comment
Share on other sites

It doesn't have to, but if something is wrong with the update statement, which it seemed like there was due to the member_id inconsistency. Change that to be OR DIE (mysql_error()) and see what is up. Code is no good if it is flawed with inconsistent and wrongly coded programming.

 

Everything seems to work!

Last question before solved.

 

Does the script needs to be changed.

<?php
  #Check error codes 
   if($result == 0){
      // unsuccessful login
      echo "<p class='genmed'>You could not be logged in. 
            You must be logged in to view this page.</p>";
  echo "<p class='genmed'> $user_name $password</p>";
      exit;
   }   
?>

 

to this

<?php
  #Check error codes 
   if($result === false){
      // unsuccessful login
      echo "<p class='genmed'>You could not be logged in. 
            You must be logged in to view this page.</p>";
  echo "<p class='genmed'> $user_name $password</p>";
      exit;
   }   
?>

 

Since there is no number being returned

 

return 0

return 1 etc..

 

*bump*

Link to comment
Share on other sites

Not necessarily. 0 is considered to be false unless you use the === operator. Here is how I would do it instead.

 

<?php
  #Check error codes 
   if (!$result){
      // unsuccessful login
      echo "<p class='genmed'>You could not be logged in. 
            You must be logged in to view this page.</p>";
  echo "<p class='genmed'> $user_name $password</p>";
      exit;
   }   
?>

 

No need to really "set" a condition when that will work just as well.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.