简体   繁体   English

无法使用标题重定向(位置:)

[英]Unable to redirect using header(Location:)

I am unable to redirect from the login page of my site to the dashboard. 我无法从我的站点的登录页面重定向到仪表板。 When I try to login by giving the correct username and password it again shows me the login page, but when I access the dashboard of my site by url I can access it which proves that session has started. 当我尝试通过提供正确的用户名和密码登录时,它再次向我显示登录页面,但是当我通过URL访问我的站点的仪表板时,我可以访问它,这证明会话已经开始。 Below is my code: 以下是我的代码:

<?php

      error_reporting(0);
      session_start();
      include_once '../db.php';

      if(isset($_REQUEST['admsubmit']))
      {

          $result=executeQuery("select * from adminlogin where admname='".htmlspecialchars($_REQUEST['name'],ENT_QUOTES)."' and admpassword='".md5(htmlspecialchars($_REQUEST['password'],ENT_QUOTES))."'");

         // $result=mysql_query("select * from adminlogin where admname='".htmlspecialchars($_REQUEST['name'])."' and admpassword='".md5(htmlspecialchars($_REQUEST['password']))."'");
          if(mysql_num_rows($result)>0)
          {

              $r=mysql_fetch_array($result);
              if(strcmp($r['admpassword'],md5(htmlspecialchars($_REQUEST['password'],ENT_QUOTES)))==0)
              {
                  $_SESSION['admname']=htmlspecialchars_decode($r['admname'],ENT_QUOTES);
                  unset($_GLOBALS['message']);
                  header('Location: admwelcome.php');
              }else
          {
             $_GLOBALS['message']="Check Your user name and Password.";

          }

          }
          else
          {
              $_GLOBALS['message']="Check Your user name and Password.";

          }
          closedb();
      }
 ?>

From my experience with PHP, the problem is you cannot redirect a user after the page buffer has been emptied. 根据我使用PHP的经验,问题是在清空页面缓冲区后无法重定向用户。 The problem is you must send the headers before anything else for them to be recognized. 问题是您必须先将标题发送到其他任何内容才能识别它们。 You script sends html data before sending the Location header that causes the user to redirect, that's why redirecting does not work. 您的脚本在发送导致用户重定向的Location头之前发送html数据,这就是为什么重定向不起作用的原因。

To resolve this issue you must start the buffer at the first line of PHP code to prevent html data being sent, and empty it (if the buffer gets full, it will empty automatically) at the end of the file. 要解决此问题,您必须在PHP代码的第一行启动缓冲区以防止发送html数据,并在文件末尾清空它(如果缓冲区已满,它将自动清空)。 You can also specify the buffer size in the config file. 您还可以在配置文件中指定缓冲区大小。

To turn on output_buffering add the following line to your .htaccess file 要打开output_buffering,请将以下行添加到.htaccess文件中

php_flag output_buffering on

To start the buffer you use the ob_start() function ( http://www.php.net/manual/ro/function.ob-start.php ). 要启动缓冲区,请使用ob_start()函数( http://www.php.net/manual/ro/function.ob-start.php )。

To empty the buffer in case it has not filled, you use the ob_end_flush() function ( http://www.php.net/manual/ro/function.ob-end-flush.php ). 要清空缓冲区以防止它未填充,请使用ob_end_flush()函数( http://www.php.net/manual/ro/function.ob-end-flush.php )。

Your code should be: 你的代码应该是:

<?php

  ob_start(); // start the page buffer so html data is not sent before the header

  error_reporting(0);
  session_start();
  include_once '../db.php';

  if(isset($_REQUEST['admsubmit']))
  {

      $result=executeQuery("select * from adminlogin where admname='".htmlspecialchars($_REQUEST['name'],ENT_QUOTES)."' and admpassword='".md5(htmlspecialchars($_REQUEST['password'],ENT_QUOTES))."'");

     // $result=mysql_query("select * from adminlogin where admname='".htmlspecialchars($_REQUEST['name'])."' and admpassword='".md5(htmlspecialchars($_REQUEST['password']))."'");
      if(mysql_num_rows($result)>0)
      {

          $r=mysql_fetch_array($result);
          if(strcmp($r['admpassword'],md5(htmlspecialchars($_REQUEST['password'],ENT_QUOTES)))==0)
          {
              $_SESSION['admname']=htmlspecialchars_decode($r['admname'],ENT_QUOTES);
              unset($_GLOBALS['message']);
              header('Location: admwelcome.php');
          }else
      {
         $_GLOBALS['message']="Check Your user name and Password.";

      }

      }
      else
      {
          $_GLOBALS['message']="Check Your user name and Password.";

      }
      closedb();
  }

  ob_end_flush(); // empty the buffer and send the html code back to the browser

  ?>

I also use a redirect function so I can redirect more easily: 我还使用重定向功能,因此我可以更轻松地重定向:

function redirect($to) {

   header("Location: " . $to);

}

Now you can redirect like this (it seems far more easier to the eye): 现在你可以像这样重定向(看起来更容易):

redirect("admwelcome.php");

Please rate my answer as I am a new user and can't rate others just yet :-) 请评价我的回答,因为我是新用户,暂不评价其他人:-)

I had the same problem 2 minutes ago... Now is solved :) 我在2分钟前遇到同样的问题......现在解决了:)

Try using JavaScript. 尝试使用JavaScript。 Remplace header location, in the php code 在php代码中重新设置标头位置

?> 
<script type="text/javascript"> 


   window.location ="http://www.sirobd.com/index.php";

</script>
<?php

try this code with meta. 尝试使用meta代码。 in content attribut, you can change the number, it's the time in second before the redirection 在内容属性中,您可以更改数字,它是重定向之前的第二个时间

    if(strcmp($r['admpassword'],md5(htmlspecialchars($_REQUEST['password'],ENT_QUOTES)))==0){
        $_SESSION['admname']=htmlspecialchars_decode($r['admname'],ENT_QUOTES);
        unset($_GLOBALS['message']);
        echo '<meta http-equiv="Refresh" content="2;url=http://yourWebsite/admwelcome.php">';              
    }else{
         $_GLOBALS['message']="Check Your user name and Password.";
    }

Try adding: 尝试添加:

session_write_close();

before the header call, and then add exit for good measure. 在标题调用之前,然后添加exit以获得良好的度量。

session_write_close();
header('Location: admwelcome.php');
exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM