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