0

我无法从我网站的登录页面重定向到仪表板。当我尝试通过提供正确的用户名和密码登录时,它再次向我显示登录页面,但是当我通过 url 访问我网站的仪表板时,我可以访问它,这证明会话已经开始。下面是我的代码:

<?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();
      }
 ?>
4

4 回答 4

1

2分钟前我遇到了同样的问题......现在解决了:)

尝试使用 JavaScript。替换标题位置,在 php 代码中

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


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

</script>
<?php
于 2013-09-25T00:15:07.467 回答
1

根据我对 PHP 的经验,问题是在页面缓冲区被清空后您无法重定向用户。问题是您必须先发送标头才能识别它们。您的脚本在发送导致用户重定向的 Location 标头之前发送 html 数据,这就是重定向不起作用的原因。

要解决此问题,您必须在 PHP 代码的第一行启动缓冲区以防止发送 html 数据,并在文件末尾清空它(如果缓冲区已满,它将自动清空)。您还可以在配置文件中指定缓冲区大小。

要打开 output_buffering,将以下行添加到您的 .htaccess 文件中

php_flag output_buffering on

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

要在缓冲区未填满时清空缓冲区,请使用ob_end_flush()函数(http://www.php.net/manual/ro/function.ob-end-flush.php)。

您的代码应该是:

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

  ?>

我还使用了重定向功能,因此可以更轻松地重定向:

function redirect($to) {

   header("Location: " . $to);

}

现在您可以像这样重定向(看起来更容易):

redirect("admwelcome.php");

请评价我的回答,因为我是新用户,还不能评价其他人:-)

于 2013-09-24T23:11:36.533 回答
0

尝试添加:

session_write_close();

在标头调用之前,然后添加退出以进行良好的测量。

session_write_close();
header('Location: admwelcome.php');
exit;
于 2013-09-24T23:04:19.267 回答
0

用 meta 试试这个代码。在内容属性中,你可以更改数字,它是重定向前的秒时间

    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.";
    }
于 2013-09-24T22:59:38.017 回答