打印

[php] header重定向页面失效?

很郁闷,初学,当查询语句成功以后依然不跳转。这是代码:代码中调用的的header.php包含了数据库链接语句,同时输出一个菜单栏。
复制内容到剪贴板
代码:
<?php
include('./header.php');
?>
<?php
if(isset($_POST['c_submit'])){
  $error=array();
  if(!empty($_POST['c_pass'])){
    $pass=$_POST['c_pass'];
  }
  $query="select u_id from users where u_pass=SHA('$pass')";
  $result=@mysql_query($query);
  if(@mysql_num_rows($result)!=0){
    $query="select c_id from confirm_pass where u_id='{$_SESSION['u_id']}'";
    $result=@mysql_query($query);
    if(@mysql_num_rows($result)==0){ #为该用户创建新的条目
      $num=rand();
      $query="insert into confirm_pass(c_num,u_id) values('$num','{$_SESSION['u_id']}')";
      $result=@mysql_query($query);
      if(@mysql_affected_rows()==1){
        header("Location: safebox.php?x=$num");
      }else{
        echo mysql_error();
      }
    }else{#用户确认信息已存在,仅更新
      $num=rand();
      $query="update confirm_pass set c_num=$num";
      $result=@mysql_query($query);
      if(@mysql_affected_rows()==1){
        header("Location: safebox.php?x=$num");
      }else{
        echo mysql_error();
      }
    }
  }else{
    echo 'password is incrrect.';
  }
}
?>
后来看到一个输出缓冲可以解决这个问题,但是还是不行,不知道是不是我写错了,这是加入输出缓冲后的例子:
复制内容到剪贴板
代码:
<?php
ob_start();
include('./header.php');
?>
<?php
if(isset($_POST['c_submit'])){
  $error=array();
  if(!empty($_POST['c_pass'])){
    $pass=$_POST['c_pass'];
  }
  $query="select u_id from users where u_pass=SHA('$pass')";
  $result=@mysql_query($query);
  if(@mysql_num_rows($result)!=0){
    $query="select c_id from confirm_pass where u_id='{$_SESSION['u_id']}'";
    $result=@mysql_query($query);
    if(@mysql_num_rows($result)==0){ #为该用户创建新的条目
      $num=rand();
      $query="insert into confirm_pass(c_num,u_id) values('$num','{$_SESSION['u_id']}')";
      $result=@mysql_query($query);
      if(@mysql_affected_rows()==1){
        header("Location: safebox.php?x=$num");
        ob_end_clean();
      }else{
        ob_end_flush();
        echo mysql_error();
      }
    }else{#用户确认信息已存在,仅更新
      $num=rand();
      $query="update confirm_pass set c_num=$num";
      $result=@mysql_query($query);
      if(@mysql_affected_rows()==1){
        header("Location: safebox.php?x=$num");
        ob_end_clean();
        exit();
      }else{
        ob_end_flush();
        echo mysql_error();
      }
    }
  }else{
    echo 'password is incrrect.';
  }
}
?>
[ 本帖最后由 daoying2000 于 2008-9-22 15:49 编辑 ]
ob_start对应还有个函数 ob_end_clean

你这里只用将在header 前有输出的部分放入ob_start与ob_end_clean之间就可以了.

[ 本帖最后由 wuleying 于 2008-9-22 16:55 编辑 ]
银子博客
经典站长QQ群:16719484
第三届经典论坛灌水王选举(12月1日-12月31日) 敬请期待
确保跳转的话,header之后立即die;

前提是你代码没问题的话.
23555455(WEB中高程Q群) 我的播客,去代码片断平台
在程序内部跳转还是用js吧
一路小跑!

TOP

还在为头像烦恼?还在为不能关注好友动态烦忧?快来蓝色理想家园吧!