0

最近,由于没有“明显”的原因,我的登录表单开始重定向到我网站的 404 页面,我在任何地方都看不到原因。

 <?php echo '<form action="http://' . $_SERVER['SERVER_NAME'] . '/login" method="post">'; ?>
 <li><input type="text" name="username" placeholder="Username"></li>
 <li><input type="password" name="password" placeholder="Password"></li>
 <li><input type="submit" value="Login"></li>
 </form>

然而由于某种原因,它仍然会重定向到 404 页面,而<?php echo'http://' . $_SERVER['SERVER_NAME'] . '/login实际上是相同的页面。除了用户成功登录之外,没有其他重定向,它转到不同的页面。我只是不明白是什么原因造成的,其他人可以解决为什么吗?

编辑:我的 .htaccess 是

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1

验证码:

<?php
if(empty($_POST) === false) {
   $username = $_POST['username'];
   $password = $_POST['password'];

   if(empty($username) === true || empty($password) === true) {
      $errors[] = 'You need to enter a username and password.';
   } else if (user_exists($username) === false) {
      $errors[] = 'We can\'t find that username. Have you registered?';
   } else if (user_active($username) === false) {
      $errors[] = 'You haven\'t activated your account!';
   } else if(strlen($password) > 32 ) {
        $errors[] = 'Password to long.';
   }
     $login = login($username, $password);
     if($login === false){
       $errors[] = 'That username/password combination is incorrect.';
     }else{
        $_SESSION['user_id'] = $login;
        header('Location: ' . $_SERVER['SERVER_NAME'] . '/?msg=1');
        exit();

     }
   }
if(empty($errors)===false){
?>
    <h1>We tried to log you in, but....</h1>
<?php
     print_r($errors);
} ?>
4

2 回答 2

0

您可以省略$_SERVER[]数组并使用静态 url,仅用于调试吗?像:

<form method="POST" action="url.php">
<li><input type="text" name="username" placeholder="Username"></li>
<li><input type="password" name="password" placeholder="Password"></li>
<li><input type="submit" value="Login" name="submit"></li>
</form>

和:

 header('Location: url.php?msg=1');

如果它给你一个 404 错误,它必须来自这个或你的 .htaccess 以这种方式调试它并让我们知道。

于 2012-11-24T21:06:48.453 回答
0

有很多可能的原因。以下是一些故障排除提示:

<?php echo '<form action="http://' . $_SERVER['SERVER_NAME'] . '/login" method="post">'; ?>

Clearly there is something wrong with the url that's being filled into the action of the form:

  1. The "page" in the code above is login in your action. Is it a page, or a directory, or do you have rewrites on? Should the url point to login.php or login.html instead of just login?

  2. When you view source of the rendered page in your browser, what do you see in the action of the form? Is it a legitimate url? If you copy/paste the url into your browser, what do you get?

  3. When using forms like this, any reason you wouldn't just have the action be /login? Server variables should not be relied upon like this, and since you're going to the same url, why not remove the complexity and just go with /login as the action?

  4. Are you using url rewrites? Have the rules changed, or has an htaccess file been modified/moved?

于 2012-11-24T21:09:54.327 回答