Why PHP Session Destroyed?

2019-05-06 16:10发布

问题:

I have this php code,

<?php

session_start();
Print_r($_SESSION);
$_SESSION['value'] = 1;
Print_r($_SESSION);

?>

Why it Prints following, everytime when I refreshes the page..

Array
(
)
Array
(
    [value] => 1
)

It should Print,

Array
(
    [value] => 1
)
Array
(
    [value] => 1
)

I am using lighttpd as http Server on Fedora 14.

回答1:

I read that running chown -R root:lighttpd /var/lib/php/ fixed the problem for others that were having the same issue.

Source:
http://masdeni.com/archives/6-Lighttpd-+-PHP-Session-Problem.html



回答2:

I would test to see if session_start() returns true (session started), for example:

$is_session_started = session_start();

If $is_session_started == false, then you have 1/2 your answer right there. The other 1/2 will lie in figuring out why it is not starting. Per @Ryan above, check your session ini settings.

If you are using cookies for storing the Session ID, make sure that you call session_start() before printing/echoing/returning any other values to the browser.



标签: lighttpd php