Session Problems

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
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Session Problems

Post by php_wiz_kid »

Here's the problem. I can't seem to get any sessions to work. I've tried making my own, I've tried the sessions I got on CDs that came with some books. None of them are working. This makes me think it's something on my local machine. Does anybody have any idea to what this problem could be? I'm pretty new to PHP and MySQL, but I think I'm getting pretty good at it, so please be patient with me.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

How are they not working? Are you receiving error messages?
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

No, not error messages. The session variables just don't seem to follow from page to page. So I set the variable on one page, and send it to another and the variable is empty. I use the session_start() function, and the $_SESSION global variable. I figure that I have something wrong setup in the php.ini file or something, but I havn't messed with anything that has to do with sessions in the file.
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Here's an example:

page1.php
<?php
session_start();

$_SESSION['valid_user'] = 1;

echo "The user identification number is $_SESSION['valid_user']";

?>
<a href="page2.php">Page 2</a>

page2.php
<?php
session_start();

echo "The user identification number is $_SESSION['valid_user']";

?>

Now, the variable on page2.php would show up empty. It would say "The user identification number is", and then it will just stop. Now this is off the top of my head so I'm hoping it's right.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Can you post some sample code?
patchesthedog
Forum Newbie
Posts: 1
Joined: Wed Jul 02, 2003 9:23 pm
Location: Cleveland, OH

Post by patchesthedog »

Look at the Apache error log to see if you are getting any errors when you try to start a session.

If the directory to post session information is not set up correctly, you can still create sessions, but the data that you associate with the session cannot be stored.

I ran into this problem when I started playing around w/ PHP and after I changed the PHP.INI (I'm in a windows environment) to point to a valid directory for session information (I used, 'C:\APACHE GROUP\APACHE2\TMP') then things began working correctly.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

how do you move from page to page?
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

i would check the php.ini file, you may need to fix your session temp dir, or somehting like that.

also set "display_errors = On;" to make sure you're not getting any unreported errors =)
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post by Jean-Yves »

Have you tried displaying the value outside of the main string quotes:

Code: Select all

echo "The user identification number is " . $_SESSION&#1111;'valid_user'];
I got a parse error when I tried it the way it was before (ie inside the main quotes).
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Thanks everybody, I now have my sessions working. I had to change the php.ini to make it work. Again, thanks to everyone who posted.
Post Reply