如何使用PHP重定向到登录页面

时间:2015-03-19 16:03:33

标签: php html if-statement

我正在尝试重定向我的php登录页面,以便在用户获得授权的情况下进入页面(r_index.php),如果用户未获得授权,则返回登录页面(login.html)。

这是我的代码:

    <?php  
if ("password"=="$password") { // Start the condition  ?>  
<a href="r_index.php">Manage classes</a>  
<?php     } // End the condition ?>     
<?php if ("password"=="") { ?>     
<a href="login.html">Login</a>     
<?php } 
?>. 

我做错了什么?我该如何解决?

4 个答案:

答案 0 :(得分:1)

用以下代码替换您的代码:

<?php  
 if ("password"== $password) { 
    header("location:r_index.php");  
 }
 else if ($password=="") {   
    header("location:login.html");     
 } 
?> 

答案 1 :(得分:0)

如果您想重定向,请使用:

header('Location: http://www.example.com/r_index.php');

代码。

答案 2 :(得分:0)

<?php
$accessGranted = false;

if($password == 'password') {
   $accessGranted = true;
}

if($accessGranted) {
   header('Location: r_index.php');
}
else {
   header('Location: login.html');
}
exit;

答案 3 :(得分:0)

实际上你的语法是错误的,否则在php中使用HTML没有问题。它会很好用。

请确保不要将变量放在引号内,并按如下所示更改语句:

if($password=="password")

if($Password==" ")
相关问题