0

该代码运行一次,但不会再次加载 customer_login.php 页面。当我第二次尝试时,它会给出 HTTp 404 Not Found 错误。在它工作和出现错误之间,我没有改变任何东西。

    <?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

3 回答 3

0

当您已经登录时,您会重定向到./admin/website/$websitelocation/index.html. 但是变量$websitelocation从何而来?如果您在登录脚本中设置此变量也无济于事!因此,您将被重定向到./admin/website//index.html,这将为您提供 404。

顺便说一句:不要使用相对重定向,最佳做法是在Location标头中包含主机名,因为它得到了更广泛的支持。

于 2012-08-17T15:38:03.470 回答
0

我的猜测是您第二次加载页面时$_SESSION["manager"]已设置,然后它重定向到的 URL 是错误的,因此会给出 404。

我的第二个猜测是,$websitelocation当您尝试进行重定向时没有定义。如果您希望它与manager会话变量一样长,请将其存储$_SESSION['websitelocation']或类似。

于 2012-08-17T15:35:46.467 回答
0

使用目录的常量制作一个包含文件,然后将其包含在您的脚本中。这应该可以让您避免一直正确定义目录的麻烦http://php.net/manual/en/language.constants.predefined.php

于 2012-08-17T15:43:00.170 回答