我正在编程一个简单的登录系统。它在本地运行得很好,但当我在主机上运行时,我会遇到头函数的问题。
这是我的登录表格:
<form method="post" action="auth.php">
User:<br />
<input type="text" name="name" required><br /><br />
Password:<br />
<input type="password" name="pass" class="input" required><br /><br />
<button>LOGIN</button>
</form>
以及"auth.php":
include '../conection.php';
$user = $mysqli->real_escape_string($_POST['name']);
$pass = sha1($mysqli->real_escape_string($_POST['pass']));
if(isset($user)){
$users = $mysqli->query("select * from admins where admin_user='".$user."' and admin_pass='".$pass."' ");
if($users->fetch_object()){
session_start();
$_SESSION['user'] = $user;
//Im having troubles with this function
header('Location:control.php');
exit;
}else{
echo 'Sorry no access<br /><br />';
//if i remove the "//" to the next header function works in local and hosting
//header('Location:index.php');
}
}else{
echo 'Sorry no access<br /><br />';
}
$mysqli->close();
警告:1st:session_start():无法发送会话缓存限制器。第二:无法修改表头信息。
代码出了什么问题?或者我没有考虑什么?此外,如果您有任何改进此代码的建议,我真的很感激。
谢谢你的帮助。
使用下面的代码,通过将操作更改为action=""
并向新的提交按钮添加isset()
,我已经将代码全部单独运行。之后你可以把它们分开。
不要将<button>LOGIN</button>
与if(isset($_POST['submit']))
条件语句一起使用,它不起作用。它基于我在下面包含的新提交按钮。
首先概述错误:
if($us = $users->fetch_object())
-$us
是一个不做任何事情的杂散变量;它需要被移除。
该行需要读取为if($users->fetch_object())
以下内容也需要删除;没有相应的if
条件:
else{
echo 'Sorry no access<br /><br />';
}
你已经在中声明了:(但不要使用带有头的echo)
else{
echo 'Sorry no access<br /><br />';
//if i remove the "//" to the next header function works in local and hosting
header('Location:index.php');
}
对于由echos和标头引起的headers already sent
,请检查打开的<?php
标记之前是否有空格,或者是否有字节顺序标记(将文件保存为不带BOM的UTF-8)。如果你在去除回声后仍然出现错误,这可能是一个促成因素。
确保上面没有任何东西。你可能会在打开<?php
标签后使用ob_start();
逃脱惩罚。
echo $user
导致了这种情况,所以只需将其删除并使用标头即可。只保留echo
到TEST,但在这样做的时候注释掉标题。你不应该回显标题、HTML等上面的任何内容。
这个echo 'Sorry no access<br /><br />';
是你应该删除它的地方。它是echo
或header
,你不能两者都用。
你可以在这里阅读headers already sent
:
- 如何修复";标头已发送";PHP中的错误
代码:(参见代码中的注释)
<?php
include '../conection.php';
$user = $mysqli->real_escape_string($_POST['name']);
$pass = sha1($mysqli->real_escape_string($_POST['pass']));
if(isset($_POST['submit'])){
$users = $mysqli->query("select * from admins where admin_user='".$user."' and admin_pass='".$pass."' ");
if($users->fetch_object()){
session_start();
$_SESSION['user'] = $user;
//Im having troubles with this function
header('Location:control.php');
exit;
// echo $user; // don't use this with header
}else{
// echo 'Sorry no access<br /><br />'; // don't use this with header
//if i remove the "//" to the next header function works in local and hosting
header('Location:index.php');
exit;
}
} // brace for if(isset($_POST['submit']))
$mysqli->close();
?>
<form method="post" action="">
User:<br />
<input type="text" name="name" required><br /><br />
Password:<br />
<input type="password" name="pass" class="input" required><br /><br />
<input type="submit" name="submit" value="Submit">
<br>
</form>
header()函数向客户端发送一个原始HTTP头。
需要注意的是,在发送任何实际输出之前必须调用header()
首先使用准备好的语句此处
第二步:
header('Location: control.php');
exit;