w0lf42
w0lf42

Reputation: 528

PHP Session Variables Over Page Load

I am attempting to save variable values between page loads.

I thought that the code below would display nothing the first time a user hit the pages. The next time is should display the number 5. It's displaying a zero (0) the first time and every time after.

<?php
session_start();
$_SESSION['test'];
echo $_SESSION['test'];
$_SESSION['test'] = 5;
?>

Am I doing something wrong? Is it possible that my PHP server is setup incorrectly? etc.

Server Settings

PHP Version 5.3.13
session.auto_start  Off Off
session.bug_compat_42   Off Off
session.bug_compat_warn Off Off
session.cache_expire    180 180
session.cache_limiter   nocache nocache
session.cookie_domain   no value    no value
session.cookie_httponly Off Off
session.cookie_lifetime 0   0
session.cookie_path /   /
session.cookie_secure   Off Off
session.entropy_file    /dev/urandom    /dev/urandom
session.entropy_length  0   0
session.gc_divisor  1000    1000
session.gc_maxlifetime  1440    1440
session.gc_probability  1   1
session.hash_bits_per_character 4   4
session.hash_function   0   0
session.name    PHPSESSID   PHPSESSID
session.referer_check   no value    no value
session.save_handler    files   files
session.save_path   /var/php_sessions   /var/php_sessions
session.serialize_handler   php php
session.use_cookies On  On
session.use_only_cookies    On  On
session.use_trans_sid   1   1

Upvotes: 0

Views: 1332

Answers (4)

Jaspal Singh
Jaspal Singh

Reputation: 1240

  1. Is session.save_path (/var/php_sessions) directory writeable. I see you are not using tmp directory so i am asking this.

  2. session.use_trans_sid should be 0 - if you enable "use_trans_sid" then the session id is attached to the URL everytime. I am not sure what happens on an ajax request but i think it will be attached to.

  3. It is kinda conflicting using session.use_only_cookies as 1 and session.use_trans_sid as 1 since use_only_cookies specifies whether the module will only use cookies to store the session id on the client side.

Upvotes: 3

Hackerman
Hackerman

Reputation: 12305

Something like this??:

  <?php
    session_start();
    if(isset($_SESSION['test']))
    {$_SESSION['test'] = 5;}
    else
    {
      $_SESSION['test'] = "";
    }
    echo $_SESSION['test'];
?>

PS: Its all about the order of doing those things, you should check for the session if exist assign 5, if not create the var and assign nothing...the first time you execute this script not gonna show nothing, but later it's gonna show 5.

Saludos ;)

Upvotes: 0

John Corry
John Corry

Reputation: 1577

$_SESSION['test'];

I think this line is creating a new element in the $_SESSION array with a key of 'test'...since there's no value being assigned to it, it's value is 0.

Try changing it to

if(!array_key_exists('test', $_SESSION)) {
    $_SESSION['test'] = 5;
}

Upvotes: 0

Gekkie
Gekkie

Reputation: 1046

You could try the following:

<?php
session_start();
if (!isset($_SESSION['test'])) {
    $_SESSION['test'] = 0;
}
echo $_SESSION['test'];
$_SESSION['test'] = 5;
?>

Upvotes: 0

Related Questions