我希望当点击提交按钮时,如果管理员已经登录到html页面,应该重定向到admin.php页面,并且学生已经登录,那么它应该重定向到student.php页面。对于admin(硬编码)只有“一个”id-密码组合,所以我想知道是否在php脚本中使用if - new语句,我可以重定向到新的php脚本吗?只使用一个提交按钮就可以做到这一点吗?
<?php
if($_POST['username'] === 'admin' and $_POST['password'] === 'password'){
go to admin.php; //Admin login page
}
else{
go to student.php; //Student has logged in => go to student login page
}
?>
发布于 2015-11-08 09:40:44
请参阅header()
您可以在重定向时这样做:
header('Location: /student.php');
请注意:在页眉函数之前,您可能不会输出任何内容,否则它将无法工作。
或者您可以使用javascript来完成这个任务。
<script>
window.location = "student.php";
</script>
或者进行元刷新
<meta http-equiv="refresh" content="0; url=student.php">
0是延迟,当它应该重定向时。
发布于 2015-11-08 09:48:21
尝尝这个
<?php
if (isset($_POST['submit'])){
if($_POST['username'] === 'admin' && $_POST['password'] === 'password'){
header('Location:admin.php');
}
else if($_POST['username'] === 'student' && $_POST['password'] === 'password'){
header('Location:student.php');
}
else{
header('Location:login.php');
}
}
?>
提交按钮
<input type="submit" name="submit" value="Submit">
发布于 2015-11-08 09:45:36
您想要做的可能是:
<?php
if($_POST['username'] === 'admin' and $_POST['password'] === 'password'){
header('Location:admin.php');
}
else{
header('Location:student.php');
}
?>
这将将用户重定向到所需的页面。
另外,正如Rocky说的,在其他情况下,在头之前不能有任何输出,而您将得到错误:报头已经发送。
http://php.net/manual/en/function.header.php
https://stackoverflow.com/questions/33592566
复制相似问题