【问题标题】:This page is not redirecting properly此页面未正确重定向
【发布时间】:2011-08-11 20:43:52
【问题描述】:

我使用了来自 about.com 的以下脚本:http://php.about.com/od/finishedphp1/ss/php_login_code_2.htm

问题是有几次它给了我这个错误:页面没有正确重定向。 Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。 代码:

<?php

session_start(); 



// Process the POST variables
$username = $_SESSION["user_name"];
//$password = $_POST["password"];


    // Set up the session variables
     $_SESSION["user_name"] = $username;

       $ugh = $_REQUEST['url_name'];

          if($_POST){
           $_SESSION['user_name']=$_POST["user_name"];
          $_SESSION['password']=$_POST["password"];  
           }



              $secret = $info['password'];

            //Checks if there is a login cookie

            if(isset($_COOKIE['ID_my_site']))


             //if there is, it logs you in and directes you to the members page

                { 
           $username = $_COOKIE['ID_my_site']; 

            $pass = $_COOKIE['Key_my_site'];

    $check = mysql_query("SELECT user_name, password 
            FROM users WHERE user_name = '$username' 
            and url_name='$ugh'")or die(mysql_error());

          while($info = mysql_fetch_array( $check )) 



          {

               if (@ $info['password'] != $pass) 
                {

                    }

                  else

                 {

                  header("Location: home.php");



                  }

                     }

                     }


       //if the login form is submitted 

      if (isset($_POST['submit'])) { // if form has been submitted



         // makes sure they filled it in

             if(!$_POST['user_name'] | !$_POST['password']) {

    die('You did not fill in a required field.');

       }

// checks it against the database



if (!get_magic_quotes_gpc()) {

    $_POST['user_name'] = addslashes($_POST['user_name']);


}

$check = mysql_query("SELECT user_name,password 
    FROM users WHERE user_name = '".$_POST['user_name']."' 
    and url_name='".$ugh."'")or die(mysql_error());



         //Gives error if user dosen't exist

          $check2 = mysql_num_rows($check);

         if ($check2 == 0) {

           die('That user does not exist in our database. 
          <a href=add.php>Click Here to Register</a>');

            }

        while($info = mysql_fetch_array( $check ))  

            {

          $_POST['password'] = md5($_POST['password']);
          $_POST['password'] = $_POST['password'];



             //gives error if the password is wrong



           if (@ $_POST['password'] != $info['password']) {

            die('Incorrect password, please try again');


              }

              else 

             { 


          // if login is ok then we add a cookie 

         $_POST['user_name'] = stripslashes($_POST['user_name']); 

     $hour = time() + 3600; 

            setcookie(ID_my_site, $_POST['user_name'], $hour); 

            setcookie(Key_my_site, $_POST['password'], $hour);   



            //then redirect them to the members area 

           header("Location: home.php"); 

            } 

           } 

            } 

          else 

          {  



           // if they are not logged in 

           ?> 

第二个代码:

然后在每个会员页面上,我使用以下内容来确保他们的登录是正确的:

      // Process the POST variables
       $email = $_SESSION["user_name"];


     // Set up the session variables
    $_SESSION["user_name"] = $username;


   if(!isset($_SESSION['user_name'])) { header("Location: log.php");}

【问题讨论】:

  • @Zoolway 更新了问题。

标签: php session redirect login


【解决方案1】:

套用 Blowski:http://kb.mozillazine.org/The_page_is_not_redirecting_properly

这就好像 Firefox 已经达到了它的最大递归深度:它检测到了一个看似无穷无尽的重定向循环

【讨论】:

    【解决方案2】:

    上述脚本中的文件名称是什么?如果第一个脚本的名称是 home.php,那么当用户访问 home.php 时,如果密码错误,它将不断重新加载,因此 Firefox 会返回该消息。

    或者,您的 .htaccess 中有什么导致它的原因吗?

    【讨论】:

    • 第一个严格是 log.php。 htaccess 有一个文件,它使用传入的用户名来显示页面,所以想象一下blowski.domain.com..
    • @KPO 嗯,您也可以发布您的 .htaccess 文件的内容吗?这通常是错误重定向的原因。
    • @KPO 我在尝试调试代码时遇到了真正的问题,因为有很多位似乎正在使用全局变量(或者实际上是错误)。例如第 25 行:$secret = $info['password']; 我的建议是通过删除 .htaccess 使其与一个 URL 一起工作。然后,当您知道它与 PHP 一起使用时,获取与 .htaccess 一起使用的静态版本。然后将两者合并。更好的是 - 尝试将其分解为多个函数,以便您可以单独测试代码的每一位,而不是一个大块。
    • @blowski 谢谢。有没有我可以使用的另一个脚本。我太新了,无法进行更改。谢谢。
    • @KPO 也可以查看一些框架——CodeIgniter、Symfony、CakePHP、Yii 都是具有强大身份验证系统的 PHP 框架,基本上是即插即用的。就个人而言,即使你付钱给某人,我也会要求他们实现一个框架,而不是从头开始。
    猜你喜欢
    • 2017-01-18
    • 2018-06-08
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多