2012-08-17 99 views
0

該代碼有效一次,但不會再加載customer_login.php頁面。當我嘗試第二次時,它會提供HTTp 404 Not Found錯誤。在工作時間和得到錯誤之間,我沒有改變任何東西。PHP頁面一次工作

<?php 
session_start(); 
if(isset($_SESSION["manager"])) 
    { 
     header("location: ./admin/website/".$websitelocation."/index.html"); 
     exit(); 
    } 
    ?> 
    <?php 
if(isset($_POST["username"])&&isset($_POST["password"])) 
    { 
     $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]); 
     $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]); 

     include "./scripts/connect_to_mysql.php"; 
     $sql = mysql_query("SELECT UsersID, Active, Website_Location FROM users WHERE  User_Name='$manager' AND Password='$password' LIMIT 1") or die(mysql_error()); 

     $existCount = mysql_num_rows($sql); 
     echo $existCount; 

     if ($existCount == 1) 
       { 
        while($row = mysql_fetch_array($sql)) 
         { 
          $id = $row["UsersID"]; 
          $active = $row["Active"]; 
          $websitelocation = $row["Website_Location"];        
         } 
        if($active == 'Yes') 
        { 
         $_SESSION["id"] = $id; 
         $_SESSION["manager"] = $manager; 
         $_SESSION["password"] = $password; 

         header("location: ./admin/website/".$websitelocation."/index.html"); 

         exit(); 
        } 
        else 
        { 
         echo "<script type='text/javascript'>alert('You are not authorized to login!!!!');</script> "; 

         echo "click here to go back to <a href='admin_login.php'>login page</a>. <br><br>"; 
         echo "click here to go to <a href='../customer_login.php'>customer login</a>. <br><br>"; 
         echo "click here to go to <a href='../index.php'>JE Designs LLC main page</a>. <br><br>";        

         exit(); 
        } 
       } 
       else 
       { 
        echo "<script type='text/javascript'>alert('The information you have entered is not correct, please try again.');</script> "; 

        echo "<a href=\"javascript:history.go(-1)\">Go back to login page.</a>"; 


        exit(); 
       } 

    } 
    ?> 

    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>User Login</title> 
    </head> 

    <body> 
    <div align="center" id="mainWrapper"> 
    <?php include_once("template_header.php");?> 
    <div id="pageContent"><br /> 
<div align="left" style="margin-left:24px;"> 
    <h2>Log in to view website.</h2> 
    <form id="form1" name="form1" method="post" action=" 
    <?php 
    if(isset($_POST["username"])&&isset($_POST["password"])) 
     { 
      echo "./admin/website/".$websitelocation."/index.html"; 
     } 
     else 
     { 
      echo "customer_login.php"; 
     } 
    ?> " > 
    User Name:<br /> 
     <input name="username" type="text" id="username" size="40" /> 
    <br /><br /> 
    Password:<br /> 
    <input name="password" type="password" id="password" size="40" /> 
    <br /> 
    <br /> 
    <br /> 

    <input type="submit" name="button" id="button" value="Log In" /> 

    </form> 
    <p>&nbsp; </p> 
</div> 
<br /> 
<br /> 
<br /> 
</div> 
<?php include_once("template_footer.php");?> 
</div> 
</body> 
</html>  
+4

它是否重定向到一個不存在的頁面? – Wiseguy 2012-08-17 15:34:43

+0

重定向網址應該是錯誤的。嘗試在發送「位置」標題之前將其打印出來。 – Florent 2012-08-17 15:34:57

+0

頂部$ _SESSION [「manager」]塊中$ websitelocation的值是多少?在頁面底部的重定向(在登錄檢查之後),您將其設置爲$ websitelocation = $ row [「Website_Location」];但是當頁面重新加載時,這個值將會丟失 – Re0sless 2012-08-17 15:35:18

回答

0

當您已經登錄後,您將重定向到./admin/website/$websitelocation/index.html。但變量$websitelocation從哪裏來?如果您在登錄腳本中設置了這個變量,這沒有幫助!所以,你將被重定向到./admin/website//index.html,這給你404

BTW:請不要使用相對重定向,它是包含在Location頭的主機名最佳做法,因爲那樣更廣泛的支持。

+0

這會不會讓customer_login.php出現?當我點擊索引頁面上的鏈接時,表示找不到頁面。我查看了他們所有正確的文件路徑。 – Jacob 2012-08-17 15:52:57

+0

重定向防止customer_login.php在用戶登錄時顯示給用戶,因爲他將被重定向到其他地方。但是當你收到消息時,沒有找到該頁面,我堅信這是指重定向頁面,而不是customer_login.php。只要看看你重定向到哪裏,並找出這個文件是否真的存在。 – 2012-08-17 15:57:02

+0

感謝您的幫助,現在就開始工作。 – Jacob 2012-08-17 16:12:37

0

我的猜測是,你的頁面加載第二次,$_SESSION["manager"]設置和URL它,然後重定向到是錯誤的,因此給出了一個404

我的第二個猜測是,當您嘗試執行重定向時,$websitelocation未定義。如果您希望它持續長達manager會話變量,請將其存儲在$_SESSION['websitelocation']或類似文件中。