Session Variables Problem

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
siwy202
Forum Newbie
Posts: 2
Joined: Fri May 30, 2003 3:50 am

Session Variables Problem

Post by siwy202 »

Hi

I'm having some puzzling problems with global session variables. Here's just an example of the code i'm testing:

Let's say I have a.php which is as follows:

Code: Select all

<?php 

session_start(); 

if ( !isset($_SESSION['username']) ) { 
   $_SESSION['username'] = 'jason'; 
} 

echo '$_SESSION[username] equals '.$_SESSION['username']; 
echo '<br><br><a href="b.php">Go to page 2</a>'; 

?>
and b.php looks as follows:

Code: Select all

<?php 

session_start(); 

echo '$_SESSION[username] equals '.$_SESSION['username']; 
echo '<br><br><a href="c.php">Go to page 3</a>'; 

?>
(I took these two codes from one of the forums just to demonstrate the kind of problem I'm having). What happens on my host server is that the session variable 'username' is not passed to b.php (It's not set in b.php). I contacted my host (nevidia.com) and asked them to turn global_registers=on. I thought that would fix it, but it didn't. If anyone knows if there are any other server side configurations that I need to be aware of, please let me know.

BTW, my host runs the following:
PHP 4.3.1
Server Operating System: Windows 2000 Server
Server Management: Microsoft IIS 5.0

Thanks, Tom
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

Session variables must be registered, you can't just assign to $_SESSION like that..

Code: Select all

<?php
 session_register();
 if (!session_is_registered ('myvar') 
 {
   $myvar = 'Sierra Nevada Pale Ale';  //
   session_register('myvar');
 }
 if (!empty($_POST['favorite_beer'])) $_SESSION['myvar'] =  $_POST['favorite_beer'];
?>
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Is that so? I have never used session registering yet i have no problem assigning new values to new entries in the $_SESSION superglobal.... is that because of php.ini register_global_variables true ???
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Stoker wrote:Session variables must be registered, you can't just assign to $_SESSION like that..
No - session_register() is deprecated since it relies on register_globals and the manual clearly states that you should not use session_register() and $_SESSION together. If you are using PHP 4.1 or above there is no reason why you should be using session_register().

Mac
Last edited by twigletmac on Fri May 30, 2003 9:27 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have your hosts set up a location for session files to be saved to?

What does it say for session.save_path when you do:

Code: Select all

<?php phpinfo(); ?>
Mac
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Mine by default sais file, but i wrote my own session handler so i use databse now, is it okay if i don't register $_SESSION varaibels?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

mikusan wrote:Mine by default sais file, but i wrote my own session handler so i use databse now
I was kinda replying for siwy202, since it is their thread :wink: .
mikusan wrote:, is it okay if i don't register $_SESSION varaibels?
Absolutely, as I said, if you have PHP 4.1 or above there is no reason why you should need to use session_register().

Mac
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

:oops: oops :oops:
siwy202
Forum Newbie
Posts: 2
Joined: Fri May 30, 2003 3:50 am

Post by siwy202 »

twigletmac, when i do <?php phpinfo(); ?> here's what it gives me for session:

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_dividend 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 C:\Clients\phpuploaddir C:\Clients\phpuploaddir
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off
Post Reply