Noob help with $_SESSION array

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
solid28
Forum Newbie
Posts: 3
Joined: Sun Sep 20, 2009 9:36 pm

Noob help with $_SESSION array

Post by solid28 »

Hi All,

I am writing some code for a login system for a non-commercial dojo I train at.

I have the whole program written, except that I can't get the $_SESSION array to pass things from page to page.

Below is some test code. Since the test code doesn't access any databases, and was gotten direct off of php.net, I can only assume that there is some setting in the PHP.ini file that needs to be tweaked so that I can pass Session variables from page to page.

Could you please advise me what settings to try and tweak so that this will work?

Code: Select all

<?php
// page1.php
 
session_start();
 
echo 'Welcome to page #1';
 
$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();
 
// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';
 
// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>
Output from page 1:
nothing.

Code: Select all

<?php
// page2.php
 
session_start();
 
echo 'Welcome to page #2<br />';
 
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];   // cat
echo date('Y m d H:i:s', $_SESSION['time']);
 
// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>
Output from page 2:
1969 12 31 19:00:00

Also I have no idea why it outputs 1969...lolz.

Could you please advise me what settings to try and tweak so that this will work with the server environment I am using?

Me and my Dojo thank you,
Evan Jerkunica
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Noob help with $_SESSION array

Post by requinix »

Do you get that for both links?
solid28
Forum Newbie
Posts: 3
Joined: Sun Sep 20, 2009 9:36 pm

Re: Noob help with $_SESSION array

Post by solid28 »

Yes, for both links.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Noob help with $_SESSION array

Post by requinix »

Put

Code: Select all

error_reporting(E_ALL);
ini_set("display_errors", 1);
at line 3 on both files. Then try.

My guess is you'll get a message about "headers already sent" on line 1.
solid28
Forum Newbie
Posts: 3
Joined: Sun Sep 20, 2009 9:36 pm

Re: Noob help with $_SESSION array

Post by solid28 »

Output from page1.php with those 2 lines of code inserted into line 3:

Code: Select all

Warning: session_start() [function.session-start]: open(/var/php_sessions/sess_0c2eb0b673ea02f68bb71bcbdf53c820, O_RDWR) failed: No such file or directory (2) in /hermes/web07d/b1080/pow.****/htdocs/test/session-page-1.php on line 5
 
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /hermes/web07d/b1080/pow.*****/htdocs/test/session-page-1.php:5) in /hermes/web07d/b1080/pow.*****center/htdocs/test/session-page-1.php on line 5
 
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/web07d/b1080/pow.******/htdocs/test/session-page-1.php:5) in /hermes/web07d/b1080/pow.******/htdocs/test/session-page-1.php on line 5
Welcome to page #1
page 2 (link to page 2)
page 2 (link to page 2)
Warning: Unknown: open(/var/php_sessions/sess_0c2eb0b673ea02f68bb71bcbdf53c820, O_RDWR) failed: No such file or directory (2) 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/php_sessions) in Unknown on line 0

Wow, error reporting seems to be useful :p



On line 3 and line 5 I see that "header already sent" bit. Tasairis :drunk: :beer:

On lines 1 and 9 I think that it is looking for the session variables but it fails to find them, right?

The warning on line 11 could probably be fixed by fixing the other warnings I bet, or it may be the root cause of the warnings...I'm not sure.

My host is powweb, and on this page: http://forums.powweb.com/showthread.php?p=451174
Did you set the correct permission for session directory in the php.ini file - session.save_path? I remember when PowWeb changed the locations of tmp files and later renamed our session folders. Some websites were updated while others were not.
So that seems to square with the warnings I am getting up above.

But my next question is how should I go about moving files and reconfiguring my php.ini file?
Where do I find this "session.save_path" bit?

Best,
Evan
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Noob help with $_SESSION array

Post by requinix »

My guess is that PHP can't write to /var/php_sessions. That means sessions won't work.

Shared hosting, right? I don't think you'll be able to fix this yourself - file a support ticket or contact the host directly and tell them about the problem.
Post Reply