Related: session variable not carrying over
Problem
Any $_SESSION[""] variables are lost when I navigate from a page to another, though I'm using session_start();
before trying to acces them.
It seems that session_start()
will automatically start a new session unless a session_name()
or a session_id()
is passed to it to specify what session to resume.
So, when it's time to show/hide buttons from server-side, the buttons never get shown, though a verified user has previously been authenticated.
I have read in a few articles that state that setcookie()
would do the trick.
EDIT
I have to set the cookie manually for session id as stated here:
Can I restore a PHP SESSION by its ID?
Code Samples
login.php
<?php
require_once "logout.php";
require_once "../data/data_access.php";
$sid = session_id();
if (empty($sid)) session_start();
$userName = "";
$password = "";
if (isSet($_REQUEST["userName"])) $userName = $_REQUEST["userName"];
if (isSet($_REQUEST["password"])) $password = $_REQUEST["password"];
echo login($userName, $password);
function login($login, $pass) {
if (isAuthenticUser($userName, $password)) {
session_regenerate_id(true);
$_SESSION["authenticated"] = true;
setcookie("cookie_name", session_id(), time() + 1200);
} else $_SESSION["authenticated"] = false;
return $_SESSION["authenticated"];
}
another_file.php
<?php
$sid = session_id();
if (empty($sid)) session_start($_COOKIE["cookie_name"]); // Will this make it?
?>
Does it suffice to retrieve the session id through $_COOKIE["cookie_name"]
or shall I do something else in order to be able to carry over my session variables values?
UPDATE
Ater some deeper digging, I see that under normal circumstances, one doesn't need to work with cookies to restore a session, as PHP does it automatically through the PHPSESSID cookie, which I don't have to care about.
However, I'using jQuery $.post()
and $.ajax()
functions to communicate with the PHP server, so it can't use the URL to GET/POST the SID automatically as it normally would.
Considering this, I managed to find a way to work with cookies manually, so I don't mix things up with session and cookies as mentioned here to another SO user:
PHP Session Not Restoring from Cookies
UPDATE #2
As per the answer I received, I have verified the PHP.ini configuration file to see if the cookies were enabled, etc.
Here it is:
session.save_path = "c:/wamp/tmp"
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 1440
That's it for the cookies and sessions! And though this configuration shall work flawlessly the majority, it doesn't work. The session_start()
function always starts over a new session. The best example is when I go to a section where a session_start()
has been called and that I cannot access my $_SESSION["authenticated"]
.