session variables not passing

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
suedoughnyhm
Forum Newbie
Posts: 6
Joined: Sat Jun 27, 2009 2:58 pm

session variables not passing

Post by suedoughnyhm »

I am trying to learn how to use sessions since I'm working on a log-in based website. In my attempt to learn I've typed a sample from a PHP book (with my own addition of finding and displaying the session_id). It is very basic. A button is pushed which takes the user to page2. Page2 is supposed to show the value of $_SESSION['session_var'] but it doesn't.

Page 1

Code: Select all

<?php
    session_start();
    $sid = session_id();
?>
<html>
<head><title>Welcome to page #1</title></head>
<body>
<?php
    $_SESSION['session_var'] = "testing";
    echo "This is a test of the sessions feature.
        <form action = 'page2.php' method = 'POST'>
        <input type='hidden' name='form_var' value='testing_btn'>
        <input type='submit' value ='go to next page'>
        </form>";
    echo "session_id: $sid";
?>
</body></html>
Page 2

Code: Select all

<?php
// page2.php
    session_start();
    $sid = session_id();
?>
<html>
<head><title>Testing Sessions page 2</title></head>
<body>
<?php
    echo "session_var = {$_SESSION['session_var']}<br>\n";
    echo "form_var = {$_POST['form_var']}<br>\n";
    echo "session_id: $sid";
?>
</body>
</html>
Here is what page2 looks like after the button is pushed.
session_var =
form_var = testing_btn
session_id: 31a7eb2fcb3ef7af59c1d907aba1412b
Obviously, the problem is that session_var should = "testing" but nothing is displayed because it appears to be empty. The session_id's are the same when echoed on page1 and page2 so that's good.

Not knowing anything about sessions or how they worked before I started doing this I've realized that there might be several reasons for this to not be working as I want it to. If someone is willing to stick it out till the end to help me fix this I'd greatly appreciate it but also some suggestions that might put me in the right direction would also be very useful and appreciated as well.

Thanks
suedoughnyhm
Forum Newbie
Posts: 6
Joined: Sat Jun 27, 2009 2:58 pm

Re: session variables not passing

Post by suedoughnyhm »

Still a no go. Cleared all my cookies and tried the new script but the result is the same.
Using: FireFox 3.0.11 and IE 8.0.6
PHP 4.4.8

Here's how it currently looks and the resulting page2.php

page1.php

Code: Select all

<?php
    session_start();
    $_SESSION[uniqid('unique_')] = 1;
    echo '<pre>Session ', print_r($_SESSION, 1), '<pre>';
?>
<html>
<head><title>Welcome to page #1</title></head>
<body>
<?php
    $_SESSION['session_var'] = "testing";
    echo "This is a test of the sessions feature.
        <form action = 'page2.php' method = 'POST'>
        <input type='hidden' name='form_var' value='testing_btn'>
        <input type='submit' value ='go to next page'>
        </form>";
?>
</body></html>
 
page2.php

Code: Select all

<?php
// page2.php
    session_start();
?>
<html>
<head><title>Testing Sessions page 2</title></head>
<body>
<?php
    echo "session_var = {$_SESSION['session_var']}<br>\n";
    echo "form_var = {$_POST['form_var']}<br>\n";
?>
</body>
</html>
Resulting page2.php
session_var =
form_var = testing_btn
Got any other suggestions?
Thanks
suedoughnyhm
Forum Newbie
Posts: 6
Joined: Sat Jun 27, 2009 2:58 pm

Re: session variables not passing

Post by suedoughnyhm »

Here's what I got when I ran the diagnostic you suggested.
Would've gotten back to you sooner about this but apparently someone decided to steal my bike today so I had to make a little trip down to the police station.
session.save_path = /var/php_sessions
session.name = PHPSESSID
session.save_handler = files
session.auto_start = 0
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.serialize_handler = php
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_secure =
session.use_cookies = 1
session.use_only_cookies = 0
session.referer_check =
session.entropy_file =
session.entropy_length = 0
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 1
session.bug_compat_42 = 0
session.bug_compat_warn = 1
See anything unusual in all of this?

Thanks again!
suedoughnyhm
Forum Newbie
Posts: 6
Joined: Sat Jun 27, 2009 2:58 pm

Re: session variables not passing

Post by suedoughnyhm »

Also, I added the two lines of code that you suggested to page1.php and page2.php and got some "Warnings" on both pages. Here is what page1.php and page2.php now look like. The Warnings named some of my website's directories so I just replaced that text with ...filepath....

page1.php with Warnings
Warning: session_start() [function.session-start]: open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb/web198/b1980/...filepath.../page1.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb/web198/b1980/...filepath.../page1.php:4) in /hermes/bosweb/web198/b1980/...filepath.../page1.php on line 4

Session Array
(
[unique_4a46fa60dc140] => 1
)


This is a test of the sessions feature.



Warning: Unknown(): open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, 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
page2.php
Warning: session_start() [function.session-start]: open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb/web198/b1980/...filepath.../page2.php on line 5

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb/web198/b1980/...filepath.../page2.php:5) in /hermes/bosweb/web198/b1980/...filepath.../page2.php on line 5

Notice: Undefined index: session_var in /hermes/bosweb/web198/b1980/...filepath.../page2.php on line 11
session_var =
form_var = testing_btn

Warning: Unknown(): open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, 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
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: session variables not passing

Post by a94060 »

there is a directory or permissions error.
Warning: session_start() [function.session-start]: open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb/web198/b1980/...filepath.../page1.php on line 4
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: session variables not passing

Post by a94060 »

if you are on a dedicated box,you may want to change the permissions on the directory, adding write permissions. run ls -al to see who the owner of the directory is. allow writing by the user that apache is running under. If it isnt a dedicated box...I am not really sure what to do.
suedoughnyhm
Forum Newbie
Posts: 6
Joined: Sat Jun 27, 2009 2:58 pm

Re: session variables not passing

Post by suedoughnyhm »

McInfo wrote:
suedoughnyhm wrote:Would've gotten back to you sooner about this but apparently someone decided to steal my bike today so I had to make a little trip down to the police station.
Did you go to file a report or did the police find your bike? If it's still missing, I'm sorry to hear that. I hope you get it back.

For your PHP problem, try this; but don't use it as a permanent solution if it works. I'm pretty sure, like a94060 says, it is a problem with permissions. Hopefully your current working directory has the proper permissions.

Code: Select all

<?php
session_save_path('.');
session_start();
$_SESSION[uniqid('unique_')] = 1;
echo '<pre>Session ', print_r($_SESSION, 1), '<pre>';
?>
Ding ding ding ding!
I've got it working now. It wasn't a permissions problem. I realized I was going to probably have to resort to talking to my host's support so I decided to do a little browsing around on my host's site and found a PHP FAQ section that dealt specifically with sessions.
McInfo, you nailed it. The FAQ defined the path for session_save_path(); and I put that on the line before session_start(); and everything's working as expected now. Thanks a ton for the help. I learned a lot from it. A happy ending to my otherwise crappy weekend which included a stolen bike and the US losing to Brazil in the Confederations Cup Final after being up 2-0 at the half (soccer).

And yes, I went to the police to file a report. You know what the worst part about it was? I bought the bike on Tuesday! Some mother effer's riding around on my brand new bike! Oh well, guess I'll just have to buy a new one with a better lock next time around.

Thanks again!
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: session variables not passing

Post by a94060 »

Glad to hear that. Good luck on your php adventures :lol:
suedoughnyhm
Forum Newbie
Posts: 6
Joined: Sat Jun 27, 2009 2:58 pm

Re: session variables not passing

Post by suedoughnyhm »

a94060 wrote:there is a directory or permissions error.
Thank you very much a94060. Looks like you had it pegged as well!
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: session variables not passing

Post by a94060 »

anytime. Welcome to the community
Post Reply