Jump to content

Session for user id


princeofpersia

Recommended Posts

Hi guys

 

I need to know how i can create a session for userid from my database, I have done the session for username but not being able to do it for user name, here is my code.

 

my database table is called users and I can login successfully, but i tried few code and it wont show the user id, the column for user id is 'id'

 

<?php

include 'global.php';

$session_username = $_SESSION['username'];


if ($_POST['login'])
{
   //get form data
   $username = addslashes(strip_tags($_POST['username']));
   $password = addslashes(strip_tags($_POST['password']));
   
   if (!$username||!$password)
      echo "Enter a username and password";
   else
   {
    //log in
    $login = mysql_query("SELECT * FROM users WHERE username='$username'");
    if (mysql_num_rows($login)==0)
       echo "No such user";
    else
    {
      while ($login_row = mysql_fetch_assoc($login))
      {

       //get database password
       $password_db = $login_row['password'];

       //encrypt form password
       $password = md5($password);
       
       //check password
       if ($password!=$password_db)
          echo "Incorrect password";
       else
       {
          //check if active
          $active = $login_row['active'];
          $email = $login_row['email'];
          
          if ($active==0)
             echo "You haven't activated your account, please check your email ($email)";
          else
        {
               $_SESSION['username']=$username;     //assign session
            
         header('Location:my.php');  
          }
       }


      }
    }
   }
}
else
{

  if (isset($session_username))
  {
   echo "You are logged in, $session_username., <a href='logout.php'>Log out</a>";
  }
  else
  {
   echo "
   <form action='index.php' method='POST'>
   Username:

   <input type='text' name='username'><p />
   Password:

   <input type='password' name='password'><p />
   <input type='submit' name='login' value='Log in'>
   </form>
   ";
  }

}

?>

 

thanks

 

Link to comment
https://forums.phpfreaks.com/topic/219702-session-for-user-id/
Share on other sites

Users should not "bump" topics that are still on the first page of the forums. If you bump, you must provide additional information. If you resort to bumping, chances are your question needs to be re-thought and re-described (see Eric Raymond's "How To Ask Questions The Smart Way").

Link to comment
https://forums.phpfreaks.com/topic/219702-session-for-user-id/#findComment-1138969
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.