PHP会话为空,即使调用session_start()后也是如此。

huangapple go评论126阅读模式
英文:

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() != &#39;&#39;){
	echo &#39;&lt;script&gt;alert(&quot;session is active&quot;)&lt;/script&gt;&#39;;
}
else{
	echo &#39;&lt;script&gt;alert(&quot;&#39;.json_encode($_SESSION).&#39;&quot;)&lt;/script&gt;&#39;;
}

PHP会话为空,即使调用session_start()后也是如此。

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(&quot;location:index.php?msg=inft&amp;auth=&quot; . 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[&quot;ID&quot;])) {
	echo &quot;Session Started&quot;;
	session_start();
	$_SESSION[&quot;ID&quot;] = rand(78273874,922839929928839);
}

!isset($_SESSION[&quot;ID&quot;]) 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

huangapple
  • 本文由 发表于 2023年3月7日 12:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定