在脚本中有多个PHP“header(url)”,“不能修改标题信息”错误。

时间:2022-09-15 16:39:18

Hey I have some script that is supposed to change the url of the page if it meets certain criteria, or somewhere else if it doesn't.

嘿,我有一些脚本可以改变页面的url,如果它符合某些标准,或者如果它不符合的话。

I'm getting this error:

我得到这个错误:

Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb25c/b1401/ipg.website/process.php:7) in /hermes/bosweb25c/b1401/ipg.website/process.php on line 53

警告:不能修改头信息——已经发送的头信息(输出开始于/hermes/bosweb25c/b1401/ipg.website/process.php:7) in /hermes/bosweb25c/b1401/ipg.website/process。php在53行

Here's my PHP. I'm pretty sure it has to do with the if statement, but it was working fine on my WAMP Server before I uploaded to my web host.

这是我的PHP。我很确定它与if语句有关,但是在我上传到web主机之前,它在我的WAMP服务器上运行良好。

<?php
session_start();
$con = mysql_connect('website', 'members_db_admin', 'password'); 
if (!$con) { 
    die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully<br />'; 

// make members the current db
$db_selected = mysql_select_db('members_db', $con);
if (!$db_selected) {
    die ('Can\'t use members database : ' . mysql_error());
}
$hash_password = md5($_POST['password']);

$email = $_POST['email'];

$result = mysql_query("SELECT email,password FROM `members` WHERE email = '$email'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();

}

$row = mysql_fetch_row($result);

if ($row[0] != $email && $hash_password != $row[1])
{
    $query = "INSERT INTO members (email, password)
    VALUES('".$_POST['email']."','".$hash_password."')";

    // Perform Query
    $result2 = mysql_query($query);

    // Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    if (!$result2) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        //$message .= 'Whole query: ' . $query;
        die($message);
    }   
$_SESSION['email']=$_POST['email'];
$_SESSION['password']=$hash_password;
$_SESSION['loggedin']="YES";
$url = "Location: /welcome.php";
header($url);
}
$_SESSION['email']=$_POST['email'];
$_SESSION['password']=$hash_password;
$url = "Location: /checklogin.php";
header($url);
?>

3 个解决方案

#1


2  

Your problem is echo 'Connected successfully<br />';. You can't print anything out before setting headers with a header() call. Make sure you don't have any echo calls before your header().

您的问题是echo '连接成功
';在使用header()调用设置标题之前,不能打印任何内容。确保在header()之前没有任何echo调用。

#2


0  

try ob_start(); after session_start();

尝试ob_start();后session_start();

#3


0  

If you use utf-8 as encoding for this file try to save it without BOM which can cause such problems.

如果您使用utf-8作为该文件的编码,则尝试在没有BOM的情况下保存它,这会导致此类问题。

#1


2  

Your problem is echo 'Connected successfully<br />';. You can't print anything out before setting headers with a header() call. Make sure you don't have any echo calls before your header().

您的问题是echo '连接成功
';在使用header()调用设置标题之前,不能打印任何内容。确保在header()之前没有任何echo调用。

#2


0  

try ob_start(); after session_start();

尝试ob_start();后session_start();

#3


0  

If you use utf-8 as encoding for this file try to save it without BOM which can cause such problems.

如果您使用utf-8作为该文件的编码,则尝试在没有BOM的情况下保存它,这会导致此类问题。