Jump to content

[SOLVED] Testing with sessions problem


graham23s

Recommended Posts

Hi Guys,

 

i did some testing with session authentication a few days ago what i had was this:

 

login_check.php

 

<?php
     session_start();

     mysql_connect("localhost", "root", "xxxxx");
     mysql_select_db("xxxxx");

     $username = $_POST['username'];
     $password = $_POST['password'];

     $sql = "SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password'";
     $result = mysql_query($sql); 

     if (mysql_num_rows($result) == 0) {

      echo "Sorry username and password combination not found.";

     } else {

      while($row = mysql_fetch_array($result)) {
    
        $_SESSION['username'] = $row['username'];
        
     }
    
       header("location:members.php");

     }  
?>

 

and to authenticate at the top of every page:

 

<?php
     session_start();

     if (!isset($_SESSION['username'])) {
   
     ## Not logged in, redirect to login page ###########################################

     header("login.php");

     } 
     
     ## a variable for quick access #####################################################
     
     $logged_in_user = $_SESSION['username'];

     echo "You are logged in successfully ($logged_in_user)";
?>

 

it worked fine, if i logged in with a different username, it displayed:

 

echo "You are logged in successfully (WHATEVER THE USERNAME IS)";

 

but then i noticed if i logged in as graham and visited another profile , when i went back to the welcome page it displayed that users name instead of the one i logged in with, have i fogotten to do something with the code above at all?

 

thanks guys

 

Graham

Link to comment
Share on other sites

Is register_globals on?  You can check by creating a script that calls phpinfo()

 

The impact is that with register_globals on, $username is registered as a global variable.  That means that if you set $username later in your script, you will be modifying the session value.

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.