Forum Moderators: coopster

Message Too Old, No Replies

Losing SESSION after refresh moving to another page.

I am losing my session connection after I refresh a page

         

PHPMike

12:28 am on Feb 23, 2008 (gmt 0)

10+ Year Member



The issue that I am having is that after I login and set the $_SESSION variable i am directed to the main page (This all works fine) and I am able to see the Welcome <user>(set with $_SESSION['ID']). However when I refresh the page or try to move to another page I lose the session. Any ideas? I have been using the following code at the beginning of each page:
<?php
session_start();
if (isset($_SESSION['ID']))
echo 'Welcome ' . + $_SESSION['NAME'];
else
header('location:<somepage>.php');
?>

When a person logs in the following code is used:
<?php
...
<their credentials are verified against the DB>
<if they are a valid user>
...
$_SESSION['ID'] = $row['UNAME'];
$_SESSION['NAME'] = $row['NICK'];
$_SESSION['ACCESS'] = $row['ULVL'];
header('location: index.php');
else
header('location: login.php');
?>

This all works just great. Then when you get to the index page the Welcome Mike appears. <refresh(f5)>. Get redirected to the header page redirect. OR I click the link to go to the admin page, (this needs a few variables from the $_SESSION to allow entrance to the page. Can't go to page, redirects me to login. I changed the isset to echo $_SESSION['ID'] to see if the session variables are persistent and I get the variable not set error(after I leave the index page or refresh).
So the question I guess is:
1- what am I doing wrong
-OR-
2- How do I keep these sessions live until a session_destroy() is called or the browser is closed? I really don't want to use cookies as of yet.

Merganser

5:33 am on Feb 23, 2008 (gmt 0)

10+ Year Member



You are using session_start() prior to writing the session variables to begin with arn't you? Not sure this fully accounts for your situation but thought I would check.

In my experience, all you need to do is call session_start() prior to writing your first variables and then make sure that any page which is going to use the session variables also has a session_start().

Also, if you are using sessions, you are using a cookie.

PHPMike

9:57 pm on Feb 25, 2008 (gmt 0)

10+ Year Member



Yes, every page that will be using the session variables begins with session_start().

..code at beginning of each page...
<?php
session_start()
if(isset($_SESSION['ID']
...
<doc......

<html>...