我们正在开发一个项目,我们的教授说我们需要使用重定向;一个用户和一个用于管理员,如果我们键入用户,它应该重定向到user.php,如果我们登录admin,它应该重定向到admin.php,但标题位置不起作用。为什么呢?
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbnew";
$iusername = $_POST['username'];
$ipassword = $_POST['password'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "Select * FROM acc WHERE username='$iusername' AND password='$ipassword'";
$query = mysqli_query($conn,$sql);
$numrows = mysqli_num_rows($query);
if ($numrows!=0) // if numrows is not 0 it means username and password is correct
{
while($row = mysqli_fetch_assoc()){
$dbusername = $row['Username'];
$dbpassword = $row['Password'];
}
if ($iusername=='admin' && $ipassword=='admin') // if user is found and user is admin then redirect to admin page
{
$_SESSION['Current'] = "admin" ;
echo header('location:admin.php');
}
else //if user is found and it is not admnin this code will run
{
$_SESSION['Current'] = $dbusername ;
echo header('location:user.php');
}
}
mysqli_close($conn);
?>
答案 0 :(得分:1)
请勿使用 echo header('location:admin.php');
代替:
header('Location: admin.php');
请记住,在调用header function()
之前,不要回显/打印任何内容请参阅标题功能手册以进一步阅读。 http://php.net/manual/en/function.header.php
答案 1 :(得分:0)
无需使用echo header('location:file.php');
仅
header('location:admin.php');
header('location:user.php');
很好。