2

I've just bought some new server space and am migrating a very simple PHP app to it that works fine on my other server.

For some reason the session data is not being stored in the $_SESSION variable between pages on the new server. I have looked at this similar SO article and many others to try and fix the issue.

I can confirm that:

  1. The session stuff is working fine on my other site. I have looked into the directory defined by session.save_path and there are loads of session files in there (on the OLD server).
  2. I have echoed the contents of the $_SESSION variable at the end of a page, and it is holding the correct data, it's just not being saved.
  3. I have echo'd session_id() for every page and get the same output.
  4. I have had a look in the folder specified by the NEW server's session.save_path folder (/usr/lib/php/session) and there aren't any files in there. I have checked the permissions and it's set at drwxrwx---, which should mean that the php program can write to it, right?
  5. I have no .htaccess file.
  6. I am running CentOS 5.5

This is from the relevant part of the phpinfo() output:

Session Support     enabled
Registered save handlers    files user
Registered serializer handlers  php php_binary wddx

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.hash_bits_per_character 5   5
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/lib/php/session    /var/lib/php/session
session.serialize_handler   php php
session.use_cookies On  On
session.use_only_cookies    Off Off
session.use_trans_sid   0   0

I've contacted the hosting company and they won't look into it because they say it's a software issue and it's not a managed account. Grrrrr...

Just so you can see some code that isn't working, I have stolen code from the article I have linked to:

// info.php
<?
session_start();

$_SESSION['ID'] = "112233";
$_SESSION['token'] = "mytoken";

print $_SESSION['ID'];
print $_SESSION['token'];
?>
<a href="info2.php">info 2</a>

and one called info2 with this code:

// info2.php
<?
session_start();

print $_SESSION['ID'];
print $_SESSION['token'];
?>
<a href="info.php">info</a>

result is printing the array data in info.php, but no output in info2.php (apart from the link of course).

Many thanks in advance for any help.

Community
  • 1
  • 1
Joe
  • 4,852
  • 10
  • 63
  • 82
  • try answers from here: http://bytes.com/topic/php/answers/783445-session-not-holding-value – Marek Sebera Jul 05 '11 at 14:20
  • any header trouble, do you work with error_reporting(-1) to get maximum error messages ? – krifur Jul 05 '11 at 14:20
  • @Marek - I'd everything on that post apart from checking that the session.save_path was correctly set in the php.ini file. But I've done that as well now and it was. – Joe Jul 05 '11 at 14:50
  • @krifur - I have now explicitly set the error reporting via error_reporting(E_ALL). I don't get any error messages though, unfortunately. – Joe Jul 05 '11 at 14:51
  • Sorry, missed your post about that. Can you see something in Apache/PHP Logs? What PHP version do you use? – Marek Sebera Jul 05 '11 at 14:52
  • @Marek - No probs. I've had a look in the error_logs and there's nothing since the thing was installed. PHP version is 5.1.6. Reasonably old I suppose, but I don't see why there would be any problems with it. – Joe Jul 05 '11 at 15:03

2 Answers2

7

Try explicitly closing the session before the script ends; maybe you see some error message then.

<?php
error_reporting(E_ALL); ini_set('display_errors', true);
session_start();
echo '<a href="?', time(), '">refresh</a>', "\n";

echo '<pre>';
echo 'session id: ', session_id(), "\n";

$sessionfile = ini_get('session.save_path') . '/' . 'sess_'.session_id();
echo 'session file: ', $sessionfile, ' ';
if ( file_exists($sessionfile) ) {
    echo 'size: ', filesize($sessionfile), "\n";
    echo '# ', file_get_contents($sessionfile), ' #';
}
else {
    echo ' does not exist';
}

var_dump($_SESSION);
echo "</pre>\n";

$_SESSION['ID'] = "112233";
$_SESSION['token'] = "mytoken";

session_write_close();
echo 'done.';
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • Cheers Volker. But I should have noted that I'd tried that as well. – Joe Jul 05 '11 at 15:12
  • @Joe "I'd tried that as well" - the part that tests the file size of the session file and prints its content, too? ;-) – VolkerK Jul 05 '11 at 15:18
1

If your session file has zero size, make sure there is still disk space available on your server. That was the problem I had.

Check disk space with df -h on a linux server.

grayob
  • 325
  • 3
  • 8