aquatradehub Posted October 13, 2012 Share Posted October 13, 2012 (edited) Hi, I have this script, and it was working fine until I added require("../mysql/connect.php") I only done this to keep the database username and password separate from the script. Now I get this error Warning: Cannot modify header information - headers already sent by (output started at /customers/4/0/d/zambiheadshop.com/httpd.www/mysql/connect.php:1) in /customers/4/0/d/zambiheadshop.com/httpd.www/registration/processRegister.php on line 111 It was all working fine when the database details were written into the script. It really is driving me mad lol /* Check no duplicate usernames */ require("../mysql/connect.php"); $con = mysql_connect("$host", "$user", "$pass") or die(mysql_error()); $query = "SELECT COUNT(*) AS count FROM members WHERE username='$username'"; @mysql_select_db("$db") or die( "Unable to select database"); $results = mysql_query($query) or die ("Error reading from database"); $existingUsernames = mysql_fetch_array($results); if ($existingUsernames['count'] > 0) { header('Location: usererror.php'); } else { $con = mysql_connect("$host", "$user", "$pass") or die(mysql_error()); $query1 = "SELECT COUNT(*) AS count FROM members WHERE email='$email'"; @mysql_select_db('zambiheadshop_c') or die( "Unable to select database"); $results1 = mysql_query($query1) or die ("Error reading from database"); $existingEmails = mysql_fetch_array($results1); if ($existingEmails['count'] > 0) { header('Location: emailerror.php'); } else { /* Write to MySQL database */ $sql="INSERT INTO members (username, hash, firstname, surname, email, address1, address2, town, county, postcode, birthday, birthmonth, birthyear, paypalemail, terms) VALUES ('$_POST[username]','$hash','$_POST[firstname]','$_POST[surname]','$_POST[email]','$_POST[address1]','$_POST[address2]','$_POST[town]','$_POST[county]','$_POST[postcode]','$_POST[birthday]','$_POST[birthmonth]','$_POST[birthyear]','$_POST[paypalemail]','$_POST[terms]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } /* Send validation email */ // recipients // subject $subject = 'AquaTradeHub.com Validation Email'; // message $message = " <p>Thank you for registering with AquaTradeHub.com</p> <br> <p>Please validate your account <a href='http://www.zambiheadshop.com/validate.php?email=$email'>Click here</a>"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'From: AquaTradeHub.com <validate@aquatradehub.com>' . "\r\n"; // Mail it mail($email, $subject, $message, $headers); /* Redirect visitor to the thank you page */ header('Location: thanks.php'); exit(); }} Many Thanks Edited October 13, 2012 by aquatradehub Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/ Share on other sites More sharing options...
3raser Posted October 13, 2012 Share Posted October 13, 2012 (edited) I'm not too sure if this could be the cause, but is there an accidental whitespace after/before your PHP tags in connect.php file that my be causing the header error? Edited October 13, 2012 by 3raser Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384979 Share on other sites More sharing options...
aquatradehub Posted October 13, 2012 Author Share Posted October 13, 2012 I just checked, and no white space. Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384980 Share on other sites More sharing options...
3raser Posted October 13, 2012 Share Posted October 13, 2012 Well it seems the error is within connect.php - Are you sure it's not returning anything that may cause the header error? Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384981 Share on other sites More sharing options...
floridaflatlander Posted October 13, 2012 Share Posted October 13, 2012 There is a pinned topic and addresses headers issues. What line is 111. About where is it? You can't send any html or even a white space before your redirect Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384982 Share on other sites More sharing options...
PFMaBiSmAd Posted October 13, 2012 Share Posted October 13, 2012 Please read the sticky post for that error - http://forums.phpfreaks.com/topic/1895-header-errors-read-here-before-posting-them/ For output on line 1 of your connect.php file, you either have that file saved with the BOM (Byte Order Mark) characters or you have some character(s) before the <?php tag in that file, or you have some php code on line 1 in that file that is producing output. Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384983 Share on other sites More sharing options...
aquatradehub Posted October 13, 2012 Author Share Posted October 13, 2012 The connect.php file is <?php $host = "host"; $user = "myuser"; $pass = "mypassword"; $db = "mydatabase"; ?> and that is it. line 111 is header('Location: usererror.php'); but if the username check is passed and it moves on to the email check, then the error is on line 121 which is header('Location: emailerror.php'); but if both checks are passed, the data is written to the database, the email is sent, and then the error is header('Location: thanks.php'); but this all worked before I added the include file. Im so confused. Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384985 Share on other sites More sharing options...
aquatradehub Posted October 13, 2012 Author Share Posted October 13, 2012 I added ob_start() to the top of the script, and now it just comes up with Parse error: syntax error, unexpected T_VARIABLE in /customers/4/0/d/zambiheadshop.com/httpd.www/registration/processRegister.php on line 5 The processRegister.php code does other stuff than what is posted above, and all worked fine until I added the ob_start(), so I dont think that was correct. Any ideas? I did read the topic suggested, but cannot see how it helps Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384991 Share on other sites More sharing options...
3raser Posted October 13, 2012 Share Posted October 13, 2012 It would probably make it easier if you gave us the modified code. ;P Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384992 Share on other sites More sharing options...
aquatradehub Posted October 13, 2012 Author Share Posted October 13, 2012 This is the whole script: <?php /* Set e-mail recipient */ $myemail = "you@domain.com"; /* Check all form inputs using check_input function */ $username = check_input($_POST['username'], "Enter your username"); /* Show error message if Passwords do not match */ if ($_POST['password1']!= $_POST['password2']) { show_error("Sorry your passwords did not match "); } $firstname = check_input($_POST['firstname'], "Please tell us your name"); $surname = check_input($_POST['surname'], "Please tell us your surname"); $address1 = check_input($_POST['address1'], "Please enter your address"); $address2 = check_input($_POST['address2'], "Please enter your address"); $town = check_input($_POST['town'], "Please enter your town, village or city"); $county = check_input($_POST['county'], "Please enter your state or county"); $postcode = check_input($_POST['postcode'], "Please enter your postcode"); $birthday = check_input($_POST['birthday'], "Please select your birthday"); $birthmonth = check_input($_POST['birthmonth'], "Please select your birth month"); $birthyear = check_input($_POST['birthyear'], "Please select your birth year"); $terms = check_input($_POST['terms'], "Please accept the terms of this site"); /* Assign POSTS to variables */ $email = ($_POST['email']); $emailconfirm = ($_POST['emailconfirm']); $paypalemail = ($_POST['paypalemail']); $paypalconfirm = ($_POST['paypalconfirm']); /* Check email is valid */ if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { show_error("E-mail is not valid"); } /* Check email confirm is valid */ if(!filter_var($emailconfirm, FILTER_VALIDATE_EMAIL)) { show_error("Confirmed E-mail is not valid"); } /* Show error message if emails do not match */ if ($_POST['email']!= $_POST['emailconfirm']) { show_error("Sorry your emails did not match "); } /* Check PayPal email is valid */ if(!filter_var($paypalemail, FILTER_VALIDATE_EMAIL)) { show_error("PayPal E-mail is not valid"); } /* Check PayPal email confirm is valid */ if(!filter_var($paypalconfirm, FILTER_VALIDATE_EMAIL)) { show_error("PayPal Confirmed E-mail is not valid"); } /* Show error message if PayPal emails do not match */ if ($_POST['paypalemail']!= $_POST['paypalconfirm']) { show_error("Sorry your PayPal emails did not match "); } /* If URL is not valid set $website to empty */ if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website)) { $website = ''; } /* Let's prepare the password encryption */ // Create a 256 bit (64 characters) long random salt // Let's add 'something random' and the username // to the salt as well for added security $salt = hash('sha256', uniqid(mt_rand(), true) . 'something random' . strtolower($username)); // Prefix the password with the salt $hash = $salt . $password1; // Hash the salted password a bunch of times for ( $i = 0; $i < 100000; $i ++ ) { $hash = hash('sha256', $hash); } // Prefix the hash with the salt so we can find it back later $hash = $salt . $hash; /* Check no duplicate usernames */ include("../mysql/connect.php"); $con = mysql_connect("$host", "$user", "$pass") or die(mysql_error()); $query = "SELECT COUNT(*) AS count FROM members WHERE username='$username'"; @mysql_select_db("$db") or die( "Unable to select database"); $results = mysql_query($query) or die ("Error reading from database"); $existingUsernames = mysql_fetch_array($results); if ($existingUsernames['count'] > 0) { header('Location: usererror.php'); } else { $con = mysql_connect("$host", "$user", "$pass") or die(mysql_error()); $query1 = "SELECT COUNT(*) AS count FROM members WHERE email='$email'"; @mysql_select_db("$db") or die( "Unable to select database"); $results1 = mysql_query($query1) or die ("Error reading from database"); $existingEmails = mysql_fetch_array($results1); if ($existingEmails['count'] > 0) { header('Location: emailerror.php'); } else { /* Write to MySQL database */ $sql="INSERT INTO members (username, hash, firstname, surname, email, address1, address2, town, county, postcode, birthday, birthmonth, birthyear, paypalemail, terms) VALUES ('$_POST[username]','$hash','$_POST[firstname]','$_POST[surname]','$_POST[email]','$_POST[address1]','$_POST[address2]','$_POST[town]','$_POST[county]','$_POST[postcode]','$_POST[birthday]','$_POST[birthmonth]','$_POST[birthyear]','$_POST[paypalemail]','$_POST[terms]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } /* Send validation email */ // recipients // subject $subject = 'AquaTradeHub.com Validation Email'; // message $message = " <p>Thank you for registering with AquaTradeHub.com</p> <br> <p>Please validate your account <a href='http://www.zambiheadshop.com/validate.php?email=$email'>Click here</a>"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'From: AquaTradeHub.com <validate@aquatradehub.com>' . "\r\n"; // Mail it mail($email, $subject, $message, $headers); /* Redirect visitor to the thank you page */ header('Location: thanks.php'); exit(); }} /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <meta http-equiv="Content-Type" content"text="" html;="" charset="UTF-8""> <title>AquaTradeHub.com Low Low Prices</title> <link href="/index_files/stdtheme.css" rel="stylesheet" type="text/css"> <meta name="description" content="Buy and sell your fish and aquatic equipment here at low low prices. Extensive information about all fish can be found here."> </head> <body> <div id="wrapper" class="group"> <div id="header"> <h1><a href="/index.html" title="Return to home page"></a></h1> </div> <div id="leftSideBar"> <div id="mainNav"> <span> <h6>Navigation</h6> <a href="index.php" title="Home Page" class="index">Home</a><br> <a href="register.php" title="register" class="register">Register</a><br> <a href="forum.php" title="forum" class="forum">Forum</a><br> <a href="wanted.php" title="wanted" class="wanted">Wanted</a><br> <a href="contact.php" title="contact" class="contact">Contact</a><br> </span> </div> <div id="mainNavBottomLeft"> <span> <h6>Tropical Care Guides</h6> <a href="/tropical/anabantids.php" title="Anabantids" class="anabantids">Anabantids</a><br> <a href="/tropical/catfish.php" title="Catfish" class="catfish">Catfish</a><br> <a href="/tropical/characins.php" title="Characins" class="characins">Characins</a><br> <a href="/tropical/cichlids.php" title="Cichlids" class="cichlids">Cichlids</a><br> <a href="/tropical/cypriniformes.php" title="Cypriniformes" class="cypriniformes">Cypriniformes</a><br><a href="/frogs.php" title="Frogs" class="frogs">Frogs</a><br> <a href="/tropical/livebearers.php" title="Livebearers" class="livebearers">Livebearers</a><br> <a href="/tropical/rainbowfish.php" title="Rainbow Fish" class="rainbowfish">Rainbowfish</a><br> <a href="/tropical/miscellaneous.php" title="Miscellaneous" class="miscellaneous">Miscellaneous FW</a><br> <a href="/tropical/brackish.php" title="Brackish" class="brackish">Brackish</a><br> <a href="/tropical/shrimp.php" title="shrimp" class="shrimp">Shrimp</a><br> <br> </span> </div> <div id="mainNavLastBottomLeft"> <span> <h6>Marine Care Guides</h6> <a href="/marine/angelfishLarge.php" title="Angelfish Large" class="angelfishLarge">Angelfish Large</a><br> <a href="/marine/angelfishDwarf.php" title="Angelfish Dwarf" class="angelfishDwarf">Angelfish Dwarf</a><br> <a href="/marine/anthias.php" title="Anthias" class="anthias">Anthias</a><br> <a href="/marine/bassAndGroupers.php" title="Bass and Groupers" class="bassAndGroupers">Bass and Groupers</a><br> <a href="/marine/bassletsAndAssessors.php" title="Basslets & Assessors" class="bassletsAndAssessors">Basslets & Assessors</a><br> <a href="/marine/batfish.php" title="Batfish" class="Batfish">Batfish</a><br> <a href="/marine/blennies.php" title="Blennies" class="blennies">Blennies</a><br> <a href="/marine/boxfishAndBlowfish.php" title="Boxfish And Blowfish" class="boxfishAndBlowfish">Boxfish And Blowfish</a><br> <a href="/marine/butterflyfish.php" title="Butterfly Fish" class="butterflyFish">Butterflyfish</a><br> <a href="/marine/cardinalfish.php" title="Cardinal Fish" class="cardinalFish">Cardinalfish</a><br> <a href="/marine/chromis.php" title="Chromis" class="chromis">Chromis</a><br> <a href="/marine/clownfish.php" title="Clownfish" class="clownfish">Clownfish</a><br> <a href="/marine/damsels.php" title="Damsels" class="damsels">Damsels</a><br> <a href="/marine/dartfish.php" title="Dartfish" class="dartfish">Dartfish</a><br> <a href="/marine/dragonets.php" title="Dragonets" class="dragonets">Dragonets</a><br> <a href="/marine/eels.php" title="Eels" class="Eels">Eels</a><br> <a href="/marine/filefish.php" title="Filefish" class="filefish">Filefish</a><br> <a href="/marine/foxface.php" title="Foxface" class="foxface">Foxface</a><br> <a href="/marine/flatfish.php" title="Flatfish" class="flatfish">Flatfish</a><br> <a href="/marine/frogfish.php" title="Frogfish" class="frogfish">Frogfish</a><br> <a href="/marine/goatfish.php" title="Goatfish" class="goatfish">Goatfish</a><br> <a href="/marine/gobies.php" title="Gobies" class="gobies">Gobies</a><br> <a href="/marine/grunts.php" title="Grunts" class="grunts">Grunts</a><br> <a href="/marine/hamlet.php" title="Hamlet" class="hamlet">Hamlet</a><br> <a href="/marine/hawkfish.php" title="Hawkfish" class="hawkfish">Hawkfish</a><br> <a href="/marine/hogfish.php" title="Hogfish" class="hogfish">Hogfish</a><br> <a href="/marine/jacks.php" title="Jacks" class="jacks">Jacks</a><br> <a href="/marine/jawfish.php" title="Jawfish" class="jawfish">Jawfish</a><br> <a href="/marine/lionfish.php" title="Lionfish" class="lionfish">Lionfish</a><br> <a href="/marine/parrotfish.php" title="Parrotfish" class="parrotfish">Parrotfish</a><br> <a href="/marine/pipefish.php" title="Pipefish" class="pipefish">Pipefish</a><br> <a href="/marine/pseudochromis.php" title="Pseudochromis" class="pseudochromis">Pseudochromis</a><br> <a href="/marine/rabbitfish.php" title="Rabbitfish" class="rabbitfish">Rabbitfish</a><br> <a href="/marine/rays.php" title="Rays" class="rays">Rays</a><br> <a href="/marine/scorpionfish.php" title="Scorpionfish" class="scorpionfish">Scorpionfish</a><br> <a href="/marine/seahorse.php" title="Seahorse" class="seahorse">Seahorse<br> <a href="/marine/squirrelfish.php" title="Squirrelfish" class="squirrelfish">Squirrelfish</a><br> <a href="/marine/sharks.php" title="Sharks" class="sharks">Sharks</a><br> <a href="/marine/snappers.php" title="Snappers" class="snappers">Snappers</a><br> <a href="/marine/tangs.php" title="Tangs" class="tangs">Tangs</a><br> <a href="/marine/tilefish.php" title="Tilefish" class="tilefish">Tilefish</a><br> <a href="/marine/triggerfish.php" title="Triggerfish" class="triggerfish">Triggerfish</a><br> <a href="/marine/wrasse.php" title="Wrasse" class="wrasse">Wrasse</a><br> <br> </span> </div> </div> <div id="rightSideBar"> <div id="rightNav"> <span> Featured Listings </span> </div> <div id="middleNavRight"> <span> <a href="bannerApplication.php"><? include_once("banners.php"); echo"$bannerAd1"; ?> </a> </span> </div> <div id="bottomNavRight"> <span> <h6>Featured Listings</h6> </span> </div> <div id="bottomNavLastRight"> <span> <a href="photosubmission.php"><? include_once("bannerlarge.php"); echo"$bannerAd2"; ?> </a> </span> </div> </div> <div id="mainContent"> <div id="topNav"> <span> <a href="bannerApplication.php"> <? include_once("bannerAds.php"); echo"$bannerAd"; ?></a> </span> </div> <div id="topLogin"> <span> <form action="login.php"> Username:<input size="10" name="username" type="text"> Password: <input size="10" name="password1" type="password1"> <input value="Login" type="submit"> </form></span> </div> <br> <span> <div id="topSearch"> <span> <form action="login.php" method="post"><font color="#ffffff"><strong>Search:</strong> <input type="text" size="15" name="searchtxt"> <text color="#ffffff"><strong>Category</strong></font> <select> <option value="coldwater">Coldwater Fish</option> <option value="marine">Marine Fish</option> <option value="tropical">Tropical Fish</option> <option value="aquariums">Aquariums / Bowls / Tanks</option> <option value="foods">Foods</option> <option value="liveplants">Live Plants</option> <option value="artificialplants">Artificial Plants</option> <option value="filtration">Filtration System</option> <option value="heating">Heaters</option> <option value="crabs">Crabs / Snails / Algae Eaters</option> <option value="frogs">Tropical Frogs</option> <option value="turtles">Turtles</option> <option value="other">Other Fish Supplies</option> </select> <input type="submit" value="Submit"> </form> </span> </div> <div class="mainText"> <h2> Please correct the following error:</h2> <?php echo $myError; ?> <br /> </div> </div></body></html> <?php exit(); } ?> like I said, it worked fine until I added include("../mysql/connect.php"); Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384994 Share on other sites More sharing options...
Christian F. Posted October 13, 2012 Share Posted October 13, 2012 Re-read PFMaBiSmAd's post, he posted the solution. To give a further hint, I'm almost positive it's the BOM. Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384996 Share on other sites More sharing options...
aquatradehub Posted October 13, 2012 Author Share Posted October 13, 2012 The file is saved as UTF-8 without BOM??? Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1384998 Share on other sites More sharing options...
kicken Posted October 13, 2012 Share Posted October 13, 2012 Open the connect.php file in a hex editor so that you can be sure that there is nothing before the opening <?php tag. If you don't have a hex editor there are some sites online you can use, such as http://en.webhex.net/. If everything looks good, re-upload your files, and double check to make sure you are uploading them to the correct place. Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1385002 Share on other sites More sharing options...
aquatradehub Posted October 13, 2012 Author Share Posted October 13, 2012 (edited) Ok guys, have no idea whats going on, but its all suddenly decided its going to work. Thanks for all your input Edited October 13, 2012 by aquatradehub Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1385006 Share on other sites More sharing options...
kicken Posted October 13, 2012 Share Posted October 13, 2012 The file appears to be ok, no non-printable chars or anything before the tags. Reupload the file to your server, make sure you're overwriting the current one and putting it in the correct place. Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1385007 Share on other sites More sharing options...
aquatradehub Posted October 13, 2012 Author Share Posted October 13, 2012 Ok guys, have no idea whats going on, but its all suddenly decided its going to work. Thanks for all your input Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1385008 Share on other sites More sharing options...
aquatradehub Posted October 13, 2012 Author Share Posted October 13, 2012 Aaaaahhhh! I just realised, although it all seems to work now, it doesnt send the email, whereas before, it was. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/269424-headers-already-sent-by-error-grrrrr/#findComment-1385010 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.