0

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);
?>
Sam
  • 7,252
  • 16
  • 46
  • 65
marrrrrv
  • 29
  • 1
  • 7
  • 1
    possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Jojo Apr 03 '14 at 13:54

3 Answers3

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

Emily Shepherd
  • 1,369
  • 9
  • 20
0

try ob_start(); after session_start();

B_CooperA
  • 659
  • 1
  • 9
  • 28
0

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

J33nn
  • 3,034
  • 5
  • 30
  • 46