英文:
Why PHP Session is Empty Even After session_start()?
问题
I just upgraded the existing version of Apache to 2.4 and MySQL to 8.0. After this upgrade I am facing a strange issue with PHP.
PHP $_SESSION
remains an empty array even after calling session_start()
function. Here is the code snippet from my PHP file.
session_start();
if(session_id() != ''){
echo '<script>alert("session is active")</script>';
}
else{
echo '<script>alert="'.json_encode($_SESSION).'"</script>';
}
Here you can see the javascript alert with an empty array.
Did anyone ever face such an issue?
Another if condition check in the file is also returning true.
if ($token != $_SESSION[Codes::FORM_TOKEN_ELEMENT]) {
header("location:index.php?msg=inft&auth=" . urlencode(CryptoJsAes::encryptData(0)));
die;
}
Here $token
has a value but $_SESSION[Codes::FORM_TOKEN_ELEMENT]
does not.
I tried the following code at the top of the page.
if(!isset($_SESSION["ID"])) {
echo "Session Started";
session_start();
$_SESSION["ID"] = rand(78273874,922839929928839);
}
!isset($_SESSION["ID"])
is always returning true.
英文:
I just upgraded the existing version of Apache to 2.4 and MySQL to 8.0. After this upgrade I am facing a strange issue with PHP.
PHP $_SESSION
remains an empty array even after calling session_start()
function. Here is the code snippet from my PHP file.
session_start();
if(session_id() != ''){
echo '<script>alert("session is active")</script>';
}
else{
echo '<script>alert("'.json_encode($_SESSION).'")</script>';
}
Here you can see the javascript alert with empty array.
Did anyone ever face such issue?
another if condition check in file also returning true.
if ($token != $_SESSION[Codes::FORM_TOKEN_ELEMENT]) {
header("location:index.php?msg=inft&auth=" . urlencode(CryptoJsAes::encryptData(0)));
die;
}
Here $token
has value but $_SESSION[Codes::FORM_TOKEN_ELEMENT]
does not.
I tried below code at the top of the page
if(!isset($_SESSION["ID"])) {
echo "Session Started";
session_start();
$_SESSION["ID"] = rand(78273874,922839929928839);
}
!isset($_SESSION["ID"])
is always returning true
答案1
得分: 1
问题在更改/var/lib/php
中默认会话目录的权限后得以解决。我运行了以下命令来解决这个问题。
chmod -R 777 session
英文:
The issue is resolved after changing permissions on default session directory in /var/lib/php
. I ran following command to resolve this issue.
chmod -R 777 session
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论