我知道这个简单的问题,在我的程序中, session 变量已经正常工作,但现在显示一些错误。我正在使用 session_start(); 在我的配置页面中启动 session ; ob_start();
然后将这个页面包含在所有页面中。但如果运行该程序,我会收到此错误消息:
Cannot send session cache limiter - headers already sent
如果删除包含的配置页面, session 变量将不起作用。
session_start();
ob_start();
$dbcont = mysql_connect("localhost","root","");
mysql_select_db("emplist",$dbcont);
if($_POST["subSubmit"]=="Submit") {
$user = $_POST["txtUsername"];
$pass = $_POST["txtPassword"];
if ($user == "user" && $pass == "pass") {
$_SESSION["uaid"] = "ADMIN SECTION";
header("Location:welcome.php");
exit();
}else{
$err = "Login Failed. Check You UserID/Password";
}
}
?>
<html>
<head>
<title>.:: Employee Management ::.</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form1" id="form1" onSubmit="return checklogin();">
USER ID : <input type="text" name="txtUsername" id="txtUsername" onKeyUp="return clearerr();">
PASSWORD : <input type="password" name="txtPassword" id="txtPassword" onKeyUp="return clearerr();">
<input type="submit" class="FormButton" name="subSubmit" id="subSubmit" value="Submit">
<input type="reset" class="FormButton" name="butReset" id="butReset" value="Reset">
</form>
</body>
</html>
这是我的主页代码。如果登录成功,那么控件将移动到“Welcome.php”页面,在该页面中我只是回显 session 变量。
最佳答案
将session_start();
移动到最顶部
你看,规则是在session_start();
不是一个字符之前不能有任何东西,否则浏览器会认为网页已经启动,因此在启动 session_start();
之前您无法启动标题。您可以使用任何字符开始标题,包括空格(空格键)。
因此,也要相应地选择您的浏览器。
关于php - php 中的 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3792624/