【问题标题】:PHP header and session tags not working after “json_encode” is called调用“json_encode”后 PHP 标头和会话标签不起作用
【发布时间】:2015-12-16 23:59:03
【问题描述】:

我正在使用 Ajax 将登录表单详细信息从 HTML 传递到 PHP。详细信息得到正确传递,所有功能都正常工作,警报也根据输出工作。

但是,如果我在json_encode 下面添加以下会话语句,它就不起作用。执行“检查元素”后,Chrome 的“网络选项卡”中的输出是正确的,但标头功能不起作用,也没有出现 Ajax 中指定的成功或失败警报。

代码如下:

<?php
  ob_start();
  session_start();
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "kites";

  //Create Connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);

  //Check connection
  if(!$conn)
  {
    die("Connection failed: " . mysqli_connect_error());
    alert("Connection failed");
  }
  else
  {
    if( empty($_POST['loginusername']) && empty($_POST['loginpassword']) )
    {
      echo json_encode(array('status'=>false,'msg'=>'No arguments provided'));
    } 
    else
    {

  $loginusername = $_POST['loginusername'];
  $loginpassword = $_POST['loginpassword'];

  $stmt = $conn->prepare("SELECT * FROM register_form WHERE username = ?");
  $stmt->bind_param("s", $loginusername);

  if($stmt->execute())
  {
    $result = $stmt->get_result();
    $num_of_rows = $result->num_rows;
    if($num_of_rows > 0)
    {
      $user = $result->fetch_assoc();
      $salt = $user['salt'];
      $encrypted_password = $user['password'];
      function checkhashSSHA($salt,$password)
      {
        $hash = base64_encode(sha1($password . $salt, true) . $salt);
        return $hash;
      }
      $hash = checkhashSSHA($salt,$loginpassword);
      if($encrypted_password == $hash)
      {
        echo json_encode(array('status'=>true, 'msg'=>'Login Successfull'));
        session_regenerate_id();
        $_SESSION['username']=$loginusername;
        session_write_close();
        //here you can redirect on your file which you want to show after login just change filename ,give it to your filename.
        header("Location:http://localhost/kites/profile.php");
      }
      else
      {
        echo json_encode(array('status'=>false,'msg'=>$encrypted_password ));
      }
    }
    else
    {
      echo json_encode(array('status'=>false,'msg'=>'Wrong username. Please check it and try again'));
    }
  }
  else
  {
    echo json_encode(array('status'=>false,'msg'=>'Something went wrong. Please try again!'));
  }
} } ?> 

如果我删除以下语句:

session_regenerate_id();
$_SESSION['username']=$loginusername;
session_write_close();
header("Location:http://localhost/kites/profile.php");

代码运行正常。

【问题讨论】:

标签: php ajax session login


【解决方案1】:

您遇到错误的代码块: 你把它改成

session_regenerate_id();
$_SESSION['username']=$loginusername;
session_write_close();
echo json_encode(array('status'=>true, 'msg'=>'Login Successfull'));

在你的 Ajax 成功函数中你可以做:

if(status){
   //redirect using window.location
   window.location = "your_redirect_url";
}

P

【讨论】:

  • 这里清理逻辑的方法不错。干得好!
【解决方案2】:

在发送任何输出后,您不能在 PHP 中发送标头。使用输出缓冲 - 解决方法

ob_start(); // you have already start buffering. that's correct
...
echo json_encode(array('status'=>true, 'msg'=>'Login Successfull'));
session_regenerate_id();
$_SESSION['username']=$loginusername;
session_write_close();
//here you can redirect on your file which you want to show after login just change filename ,give it to your filename.
header("Location:http://localhost/kites/profile.php");
ob_end_flush();
...

但毕竟,您不应该在 ajax 调用中使用 header 重定向。它不会按预期工作。

【讨论】:

  • 忘记了 ob_startob_end_flush。但是,这种代码似乎不会起作用,即使它可以起作用,对吧?
  • PHP 在脚本执行结束时自动刷新缓冲区,因此ob_end_flush(); 不会添加任何内容。这不是解决方案。
  • @Tristan,真的吗?在发送标头之前如何强制任何输出?
  • @JakeGould,对。我在帖子中添加了一些注意事项
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-30
  • 2017-09-18
相关资源
最近更新 更多