2011-05-18 69 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");