2011-05-18 120 views
0

所有页面,PHP - 重定向

我在我的应用程序,它是WAMP几个HTML页面。

我需要一个登录页面,在成功登录时将用户重定向到应用程序中的其他页面。

我是:

1>无法重定向。我已经定义了header('Location: /X/Y/abc.html')。这是来自wamp home的相对路径,即C:\wamp\www\。我认为这里提到的路径存在问题。

2>登录失败时,用户应该重定向到同一页面。当我尝试使用header('Location: '.$_SERVER['PHP_SELF'])。试图执行的代码给我

所以,我有2个文件夹主要有:

文件夹Z含有名为index.html和下面的HTML代码abc.php
文件夹/ X/Y /包含其实际应用中认证已设置。

HTML代码:

<html> 
    <head> 
     <title> 
     Login 
     </title> 
    </head> 
    <body> 
     <form action="/Z/abc.php" method="post"> 
     <table id="input"> 
     <tr> 
       <td>Admin Username <input type="text" name="username"></td> 
     </tr> 
     <tr> 
       <td>Password <input type="text" name="passwd"></td> 
     </tr> 
     <tr> 
       <td><input type = "submit" id="submitButton"></td> 
     </tr> 
     </form> 
    </body> 
</html> 

PHP代码:

<?php 
    $username="ab"; 
    $password="cd"; 

    if (isset($_POST["username"]) && strcasecmp($username, $_POST["username"])) 
    { 
     header('Location: /X/Y/navigation.html'); 
    } 
?> 

执行代码不重定向我到navigation.html但停在abc.php即URL remaind:http://localhost:81/Z/validate.php

+0

重定向到 '/X/Y/abc.html' 应该工作作为相对根,只要你可以打的“http://服务器/ X/Y/abc.html”。安装任何东西并不重要。 在将任何其他内容发送到缓冲区之前,您是否正在执行重定向? – 2011-05-18 18:48:28

+1

“试图执行代码给我”...? – 2011-05-18 18:49:07

+2

在标题后面调用exit(),因为调用header()不会结束脚本。您可能稍后在脚本中重写HTTP 302。 – 2011-05-18 18:59:43

回答

1

如果登录不成功,您需要将abc.php重定向回登录页面:

abc.php:

<?php 

$username = 'ab'; 
$password = 'cd'; 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if ((isset($_POST['username']) && ($_POST['username'] == $username)) && 
     (isset($_POST['password']) && ($_POST['password'] == $password))) { 
     header("Location: /X/Y/navigation.html"); 
     exit(); 
    } 
} 

header("Location: login.html");