Session variables not holding from page to page

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
bigfunkychief
Forum Newbie
Posts: 4
Joined: Fri Apr 18, 2008 1:04 pm

Session variables not holding from page to page

Post by bigfunkychief »

This is starting to drive me crazy...tried scouring the Internet and forums but can't figure it out.

I have an existing website on this Apache webserver that is passing Session Variables just fine...

I am creating a new site, and copied the basic framework from the existing site but for some reason session variables aren't being passed.

My code for home.php (where it registers the vars) is this:

-----{snip}
<?php
session_start();
header("Cache-control: private");

$userlogin = $_GET['userlogin'];

// REGISTER SESSION VARIABLES FOR USER
// ------------------------------------------------

$_SESSION['userlogin'] = $userlogin;
$_SESSION['firstname'] = $firstname;
$_SESSION['lastname'] = $lastname;
$_SESSION['emailaddr'] = $emailaddr;


echo 'User login session var is ' . $_SESSION['userlogin'];
echo '<br><a href="page1.php">Click here to see what happens</a>';
exit();
-----{snip}


And, my page1.php code is this:

-----{snip}
<?php
session_start();
header("Cache-control: private");

echo "Session is: ";
echo $_SESSION['userlogin'];
exit;
?>
-----{snip}


Home.php shows the following output:

User login session var is srace
Click here to see what happens

Page1.php shows the following output:
Session is:




Any help is appreciated! I'm 2 days into trying to figure this out and driving me nuts! Thanks!!
Last edited by bigfunkychief on Fri Apr 18, 2008 1:18 pm, edited 1 time in total.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Session variables not holding from page to page

Post by andym01480 »

Very simple - you have a line before the <?php which is o/p to the browser, therefore session is not started.

Code: Select all

<?php
session_start();
//....
bigfunkychief
Forum Newbie
Posts: 4
Joined: Fri Apr 18, 2008 1:04 pm

Re: Session variables not holding from page to page

Post by bigfunkychief »

I just checked...actually there is no white space before the <?php
I just made sure it was my line 1 in my code, even changed it to this:

----{snip}
<?php session_start();
header("Cache-control: private");

echo "Session is: ";
echo $_SESSION['userlogin'];
exit;
?>
----{snip}

to no avail. Sorry, I noticed my post had the ----{snip} and a line below it, should not have been there.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Session variables not holding from page to page

Post by aceconcepts »

How have you structured your site?

i.e. <html><head> etc...
bigfunkychief
Forum Newbie
Posts: 4
Joined: Fri Apr 18, 2008 1:04 pm

Re: Session variables not holding from page to page

Post by bigfunkychief »

Thanks for the response. Right now the only code is what I've shown in the example. There's no HTML or HEAD tags.

I turned on the error codes at someone else's suggestion and received this:

----{snip}
Warning: session_start() [function.session-start]: The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /var/www/proposals/home.php on line 5

Notice: Undefined variable: firstname in /var/www/site/home.php on line 14

Notice: Undefined variable: lastname in /var/www/site/home.php on line 15

Notice: Undefined variable: emailaddr in /var/www/site/home.php on line 16
User login session var is srace
Click here to see what happens
Warning: Unknown: The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0
----{snip}

After researching the error about invalid chars in the session id, I found someone recommending this fix:

session_name("sess_SomethingSecure");
session_start();

Adding the session_name before the start has seemed to fix my problem. I'm happy to leave it like this, but does anyone know if doing this is any type of security issue or sloppy code?

Thanks for the fast repsonses!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Session variables not holding from page to page

Post by aceconcepts »

Try commenting out the majority of your code and simply see if you session data out puts anything:

Code: Select all

 
<?PHP
session_start();
 
echo session_id();
?>
 
Also take a look at http://uk.php.net/session
bigfunkychief
Forum Newbie
Posts: 4
Joined: Fri Apr 18, 2008 1:04 pm

Re: Session variables not holding from page to page

Post by bigfunkychief »

Without the explicit session name, echo session_id(); returns nothing.

With the explicit session name, it spits out a session ID jd781987s6g9a8s7d98.

Never had to do this with any of my code, so not exactly sure why I need it for this site...keeping in mind though that I have another website running on this server that uses sessions, is it possible they are interfering with each other, and defining an explicit session name separates the two?
Post Reply