Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redirect 位置标题不起作用,有什么想法吗?_Redirect_Header_Location - Fatal编程技术网

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

Redirect 位置标题不起作用,有什么想法吗?,redirect,header,location,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'

正如您在下面看到的,我有一个登录页面,它将根据结果返回到索引。当我来到这一页时,它什么也不做。会话的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');
    }
}

?>


当我将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');
    }
}

?>


您还可以添加
connect\u db.php
文件的内容吗?根据手册,“在发送任何实际输出之前,必须先调用header(),无论是通过普通HTML标记、文件中的空行还是从PHP。使用include或require函数或其他文件访问函数读取代码,并且在调用header()之前输出空格或空行,这是一个非常常见的错误。”。。。如果可能,请附上
php.ini
的内容。我意识到,当我将connect\u db.php文件内容直接添加到此代码时,它可以工作,但当我使用“include”功能时,它不能工作。任何想法
可能它无法连接到MySQL服务器,因此,它通过
die('cannotconnect:'.MySQL_error())输出一些数据无论如何,检查我下面的答案谢谢仍然没有运气也没有显示echo$buffer;我真的很困惑,我尝试过使用window.location.href='/index.php?login=yes',但如果计数不是==1,则不起作用