Sessions dropping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Sessions dropping

Post by lostboy »

i am having some problems keeping my session vars alive. i have a user log-in form which sets a session before redirecting the user to another page. In this page I check for the existence of that session var and it seems to be loosing the session as it directs users back to the log in...

Note: most of the code is in functions

Code: Select all

//code to check the log in and set the session var

$result = mysql_query($sql_id,$conn) or die("Can't retrieve user because ".mysql_error());
    if ($result){
      while($row = mysql_fetch_array($result)){
        $user_id = $row['user_id'];
      }
    }//end if

      $_SESSION['user_id'] = $user_id;

    header("location:my_ads.php?a=show");
Any ideas as to why this may drop sessions info?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

im assuming you are calling

Code: Select all

session_start();
at the beginning of th next page right?
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

of course...didn't see the need to burden everyone with the entire page...
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

try this piece of pseudo-code

Code: Select all

$result = mysql_query($sql_id,$conn) or die(mysql_error()); 
if ($result)
{ 
      while($row = mysql_fetch_array($result)){ 
        $user_id = $row['user_id']; 
}
$_SESSION['user_id'] = "$user_id";
if (isset($_SESSION['user_id'])
{
// continue
// do something
}
else
{
// the session doesnt exist
header("location:my_ads.php?a=show");
exit();
}
Post Reply