2015-11-08 281 views
1

我希望当点击提交按钮时,如果admin已经登录了html页面,应该重定向到admin.php页面并且学生已经登录,它应该重定向到student.php页面。对于admin(硬编码),将只有'一个'id密码组合,所以我想知道如果在php脚本中使用if-else语句是否可以重定向到新的php脚本? 是否可以仅使用一个SUBMIT按钮来完成此操作?PHP-Admin,student LogIn

 <?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 
    } 
    ?> 

回答

1

header()

您可以重定向做到这一点:

header('Location: /student.php'); 

请注意:您可能不头功能否则它不工作之前输出任何东西。

或者你可以用JavaScript

<script> 
    window.location = "student.php"; 
</script> 

或者与元刷新

<meta http-equiv="refresh" content="0; url=student.php"> 

0做到这一点的延迟,当它应该重定向。

1

你想要做的可能是以下内容:

<?php 
    if($_POST['username'] === 'admin' and $_POST['password'] === 'password'){ 
    header('Location:admin.php'); 
    } 
    else{ 
    header('Location:student.php'); 
    } 
?> 

这将重定向用户到你想要的页面。

而且洛基说,你不能有你的头之前任何输出otherwhile你会得到错误:头部已经发送

http://php.net/manual/en/function.header.php

1

如果像你说的值是硬编码,那么这段代码应该工作:

<?php 
if($_POST['username'] === 'admin' && $_POST['password'] === 'password'){ 
    header("Location: admin.php"); //Admin login page 
} 
else{ 
header("Location: student.php"); //Student has logged in => go to student login page 
} 
?> 

硬编码值是不是在大多数情况下,一个很好的选择,但是这个代码是什么将做到这一点。

你甚至可以使用下列元刷新:

<meta http-equiv="refresh" content="0; url=student.php"> 

还有其他的方法来做到这一点很好,但我不认为你需要他们。希望这有助于:)

+0

我知道,我问要做到这一点在小型项目的语句。但是,只有管理员是硬编码的,我会为学生ID密码组合检查写一个脚本。 –

1

尝试这个

<?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">