Home > Backend Development > PHP Tutorial > PHP的SESSION无效,如何都写入不进去,在别的文件就用的很好的

PHP的SESSION无效,如何都写入不进去,在别的文件就用的很好的

WBOY
Release: 2016-06-13 12:51:52
Original
825 people have browsed it

PHP的SESSION无效,怎么都写入不进去,在别的文件就用的很好的。

<br />
<?php<br />
	session_start();<br />
	if(isset($_POST['sub'])){<br />
		require "./config.php";<br />
		if($_POST['login_name']==$admin_name && $_POST['login_pwd']==$admin_pwd){<br />
			$_SESSION['login_status']=="oko";<br />
		}else{<br />
			echo "<font color='red'><b>帐号或密码错误!</b></font>";<br />
		}<br />
	}<br />
?><br />
<html><br />
	<head><br />
		<title>后台管理页面</title><br />
	</head><br />
	<body><br />
		<?php<br />
			if($_SESSION['login_status']!="oko"){<br />
				var_dump($_SESSION['login_status']);//这个地方一直是NULL<br />
		?><br />
		<form name="login" method="post" action=""><br />
			username:<input type="text" name="login_name" /><br/><br />
			password:<input type="password" name="login_pwd" /><br/><br />
			<input type="submit" name="sub" value="submit" /><br />
		</form><br />
		<?php<br />
			}else{<br />
				echo "ll";<br />
			}<br />
		?><br />
	</body><br />
</html><br />
Copy after login


代码如上,很简单,但是SESSION一直都写不进去,不知道到底是什么原因!!


------解决方案--------------------
$_SESSION['login_status']="oko";  //赋值是一个=号
------解决方案--------------------
修改后的代码

if(isset($_POST['sub'])){
require "./config.php";
if($_POST['login_name']==$admin_name  && $_POST['login_pwd']==$admin_pwd){
$_SESSION['login_status']="oko";//这个地方你多了个“=”
}else{
echo "帐号或密码错误!";
}
}else{
session_start();//session_start();运行一次就可以了,而且要放在代码的最前边
}
?>


后台管理页面


if($_SESSION['login_status']!="oko"){
var_dump($_SESSION['login_status']);//这个地方一直是NULL
?>

username:

password:



}else{
echo "ll";
var_dump($_SESSION['login_status']);//要这个地方就可以打印了
}
?>



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template