dsideal2015 2009-04-28 06:20
浏览 37

发布表单时使用输出缓冲区的PHP头重定向不起作用 - 但在其他情况下呢?

I have an unusual problem, only happening on one server. Following code

....
elseif ($_GET['action']=='login') {

 if (empty($_POST['login_name'])) { $_POST['login_name']=''; }
 if (empty($_POST['login_pass'])) { $_POST['login_pass']=''; }
 if (!empty($_POST['send'])) {
  if (($_POST['login_name']==_ADMIN_NAME) and    ($_POST['login_pass']==_ADMIN_PASS)) {
   //Successfully logged in
   $_SESSION['logged']=1;

// DOES NOT WORK
   header('Location: '.$filename);
   die('Command '.$filename);
  }
 }
// Show Form;
include('plogin.inc.php');
} 
elseif ($_GET['action']=='logout') {
 $_SESSION['logged']=-1;
// DOES WORK!
 header('Location: '.$filename);
}

So the problem is this if i click the link logout, all goes well and i get redirected to $filename. If i submit the login form it checks if the post is correct, set's the session var, but then dies instead of redirecting.

I got output buffers on, all error reporting but (no errors), it doesn't redirect when I post with the form even though it definitely passes (because the session var is set and I get to the die part)

What could be the cause for this behavior? Thanks

  • 写回答

7条回答 默认 最新

  • dtbl1231 2009-04-28 06:26
    关注

    might be a simple thing. The header() function does set a header, but does not terminate a script so that the header is sent immidiately after calling header().

    You need to make php send the script result to the client. As you use output buffering, this result is held back until you trigger your output. Try this instead:

    header('Location: '.$filename);
    ob_end_flush();
    exit();
    

    Regards, Mario

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效