位置标题不起作用,有什么想法吗?

时间:2015-06-22 06:44:21

标签: redirect header location

如下所示,我有一个登录页面,它将根据结果返回索引。当我来到这个页面它什么也没做。会话的ect注册和登录,但我必须手动重新进入我自己的页面?

<?php
session_start();

include("connect_db.php");

$tbl_name="users"; 
$email=$_POST['email']; 
$pass=$_POST['pass']; 

$sql="SELECT * FROM $tbl_name WHERE email='$email' and pass='$pass'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if($count==1){

    while($row = mysql_fetch_array($result)) {
        $username=$row['username'];
        $_SESSION['username']=$username;
        header('location: index.php?login=yes');
    }
    else {
        header('location: index.php?login=no');
    }
}

?>

1 个答案:

答案 0 :(得分:0)

  

我意识到我直接添加connect_db.php文件内容   这个代码可以工作,但是当我使用'include'功能时它不会。任何   想法

如果是这样,请尝试输出缓冲。

现在您的代码应如下所示:

<?php
// Output buffering
ob_start();
include("connect_db.php");
$buffer = ob_get_clean();
echo $buffer;
// End OB

session_start();

$tbl_name="users"; 
$email=$_POST['email']; 
$pass=$_POST['pass']; 

$sql="SELECT * FROM $tbl_name WHERE email='$email' and pass='$pass'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if($count==1){

    while($row = mysql_fetch_array($result)) {
        $username=$row['username'];
        $_SESSION['username']=$username;
        header('location: index.php?login=yes');
    }
    else {
        header('location: index.php?login=no');
    }
}

?>