简体   繁体   中英

Multiple PHP “header(url)” in script, “Cannot modify header information” Error

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.

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

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
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);
?>

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() .

try ob_start(); after session_start();

如果您使用utf-8作为此文件的编码,请尝试保存它而不会导致此类问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM