Trouble with session management with PHP 4

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
Buddha Joe
Forum Newbie
Posts: 6
Joined: Mon Mar 15, 2004 12:38 pm

Trouble with session management with PHP 4

Post by Buddha Joe »

I am new to PHP and am trying to get a handle on session management. I seem to be having a problem with how PHP is configured on my Dev machine. I am using it with Apache 2.0 and have it installed as a module. I am doing all of this on Linux which I am also new to.

The problem I have is when I try to use the following code:

Code: Select all

<?php
session_start();
session_register("SESSION");

if (! isset($SESSION)) {
        $SESSION["count"] = 0;
        echo "<li>Counter initialized, please reload this page to see it increment";
} else {
        echo "<li>Waking up session $PHPSESSID";
        $SESSION["count"]++;
}
echo "<li>The counter is now $SESSION[count] ";
?>
The page does not update as it should. When I uploaded the same code to the production server at my ISP the code works fine.

I comapred the results from phpinfo() from my ISP and my Dev machine and was unable to tell what if anything I may have doen wrong when I installed PHP.

Here are the results from my dev box:

Session Support enabled
Registered save handlers files user

Directive Local Value Master Value

session.auto_start Off Off
session.bug_compat_42 Off Off
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid On On


When I compare this infromationwith that of my ISP the only diffrences I can find appear to be related to the fact that my ISP is using 4.2.2 and I am using 4.3.4, as well as the fact that I compiled PHP with debugging support.. Unfortunetly I am not experinced enough with PHP to be able to tell for sure.

Here is the info from my ISP:

Session Support enabled

Directive Local Value Master Value
session.auto_start Off Off
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_trans_sid 1 1

Any help or suggestions would be greatly appreciated.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Trouble with session management with PHP 4

Post by TheBentinel.com »

Buddha Joe wrote:When I compare this infromationwith that of my ISP the only diffrences I can find appear to be related to the fact that my ISP is using 4.2.2 and I am using 4.3.4, as well as the fact that I compiled PHP with debugging support.. Unfortunetly I am not experinced enough with PHP to be able to tell for sure.
The docs seem to suggest that the version difference may be what's biting you:

(From http://us2.php.net/session_register)

If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

register_globals: important note: Since PHP 4.2.0, the default value for the PHP directive register_globals is off. The PHP community encourages all to not rely on this directive but instead use other means, such as the superglobals.

So maybe change your session_register call to:

$_SESSION["SESSION"] = 0;


Hope it helps.
Buddha Joe
Forum Newbie
Posts: 6
Joined: Mon Mar 15, 2004 12:38 pm

Register_globals

Post by Buddha Joe »

Thanks for the quick reply. After reading your response to my original post I examined the info from both boxs and found that register_globals is enabled on the production box and not on my dev box..

I replaced the $SESSION as you suggested. now the only problem I have is that I am unable to retrive the ID from $PHPSESSID. other then that the script is working. Is this ID being stored in the Superglobal $_SESSION?
Buddha Joe
Forum Newbie
Posts: 6
Joined: Mon Mar 15, 2004 12:38 pm

PHP Session ID

Post by Buddha Joe »

Well I from what I have read so far it does not appear that the SID is stored in the superglobal as it would have to be declared in the array.. but that still leaves me with the question of how to retrive the SID if it is not in $PHPSESSID?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: PHP Session ID

Post by TheBentinel.com »

Buddha Joe wrote:Well I from what I have read so far it does not appear that the SID is stored in the superglobal as it would have to be declared in the array.. but that still leaves me with the question of how to retrive the SID if it is not in $PHPSESSID?
Looks like you could get it with the session_id() function:

http://us3.php.net/manual/en/function.session-id.php

Hope it helps!
Buddha Joe
Forum Newbie
Posts: 6
Joined: Mon Mar 15, 2004 12:38 pm

session_id()

Post by Buddha Joe »

that was it... I really appreciate your help today.. thanks so very much.
Post Reply