dmccabe Posted February 19, 2009 Share Posted February 19, 2009 I am working on a registration script and all is working well, however when the user clicks the link in the email to validate their account, I want it to redirect them to the profile.php page, however I get the modified headers already sent message and realise this becasue the header has to be at the top, so how to get round this? <?php SESSION_START(); include('dbconnect.php'); if (isset($_GET['vcode']) && !empty($_GET['vcode'])) { $vcode = $_GET['vcode']; $uid = $_GET['user']; $query = "SELECT `validation_code` FROM `users` WHERE `ID` = '$uid'"; $result = mysql_query($query); if ($result) { $row = mysql_fetch_row($result); $vcode_var = $row[0]; if ($vcode == $vcode_var) { $_SESSION['uid'] = $uid; header( 'profile.php' ) ; } else { echo "Failure!"; } } else { die(mysql_error()); } } Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/ Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 Given that code, I do not see where it has gone wrong, check that you do not have a whitespace before the <?php as that will cause header errors. Also the correct way to do a header redirect is as follows: header( 'Location: profile.php' ) ; Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766533 Share on other sites More sharing options...
dmccabe Posted February 19, 2009 Author Share Posted February 19, 2009 Nope, no white space Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766536 Share on other sites More sharing options...
dmccabe Posted February 19, 2009 Author Share Posted February 19, 2009 The error is this: Warning: Cannot modify header information - headers already sent by (output started at /home/techmonk/public_html/projects/freelance/dbconnect.php:17) in /home/techmonk/public_html/projects/freelance/register.php on line 16 THis is dbconnect.php <? /*--------- DATABASE CONNECTION INFO---------*/ $hostname="localhost"; $mysql_login=""; $mysql_password=""; $database=""; // connect to the database server if (!($db = mysql_pconnect($hostname, $mysql_login , $mysql_password))){ die("Can't connect to database server."); }else{ // select a database if (!(mysql_select_db("$database",$db))){ die("Can't connect to database."); } } ?> and this is the register.php again: <?php SESSION_START(); include('dbconnect.php'); if (isset($_GET['vcode']) && !empty($_GET['vcode'])) { $vcode = $_GET['vcode']; $uid = $_GET['user']; $query = "SELECT `validation_code` FROM `users` WHERE `ID` = '$uid'"; $result = mysql_query($query); if ($result) { $row = mysql_fetch_row($result); $vcode_var = $row[0]; if ($vcode == $vcode_var) { $_SESSION['uid'] = $uid; header( 'profile.php' ) ; } else { echo "Failure!"; } } else { die(mysql_error()); } } There is stuff after this, but nothing before it and no white spaces Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766540 Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 The same goes for the dbconnect, there cannot be a white space there at the top or bottom. Any whitespace in there will trinkle down. Since Line 16 in the DB file is right at the ?> that is my best guess. Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766546 Share on other sites More sharing options...
gizmola Posted February 19, 2009 Share Posted February 19, 2009 Best practices to help avoid these issues is to always exclude the closing ?> tag in your scripts. Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766552 Share on other sites More sharing options...
dmccabe Posted February 19, 2009 Author Share Posted February 19, 2009 Thanks, I got rid of the error by removing whitepsace at the end of the dbconnet script, but not it just doesnt redirect? Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766565 Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 Also the correct way to do a header redirect is as follows: header( 'Location: profile.php' ) ; Did you change that as suggested? From the code you posted above, does not seem like it. Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766570 Share on other sites More sharing options...
dmccabe Posted February 19, 2009 Author Share Posted February 19, 2009 ah I missed the bit about the Location you were right thanks. However I now have a new problem I get this: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/techmonk/public_html/projects/freelance/register.php on line 37 After making a change to check they weren't already validated: <?php SESSION_START(); include('dbconnect.php'); if (isset($_GET['vcode']) && !empty($_GET['vcode'])) { $vcode = $_GET['vcode']; $uid = $_GET['user']; $query = "SELECT `isactive`,`validation_code` FROM `users` WHERE `ID` = '$uid'"; $result = mysql_query($query); if ($result) { $row = mysql_fetch_row($result); $vcode_var = $row['validation_code']; $isactive = $row['isactive']; if ($isactive < 1) { if ($vcode == $vcode_var) { $query = "UPDATE `users` SET `isactive` = '1' WHERE `ID` = '$uid'"; if (!mysql_query($query)) { die(mysql_error()); } else { $_SESSION['uid'] = $uid; header( 'Location: profile.php' ) ; } ` } else { echo "Authentication failure, there may be a problem with the website, please click here to contact an admin."; } } else { echo "The user requested is already active, please click here to login or here to have your username and password sent to you."; } } else { die(mysql_error()); } } $usererror = 0; $passerror = 0; $emailerror = 0; if (isset($_POST['submit'])) { <---- this is line 37 if (empty($_POST['username'])) { echo "<font color='#FF0000'>You must specify a username!<br /></font>"; $usererror = 1; } elseif (empty($_POST['password'])) { echo "<font color='#FF0000'>You must specify a password!<br /></font>"; $passerror = 1; } elseif ($_POST['password'] != $_POST['confirm_password']) { echo "<font color='#FF0000'>Your password and confirmation do not match!<br /></font>"; $passerror = 1; } elseif (!preg_match("/^[^@]*@[^@]*\.[^@]*$/", $_POST['email'])) { echo "<font color='#FF0000'>Invalid E-mail address, please try again!</font><br />"; $emailerror = 1; } I have indicated line 37, but I have not modified that line nor any of the other lines since posting in this thread so dont see why the error is there? Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766602 Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 ` } else { echo "Authentication failure, there may be a problem with the website, please click here to contact an admin."; } You have a weird ` character there, perhaps that is causing all the fuss...? Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766605 Share on other sites More sharing options...
dmccabe Posted February 19, 2009 Author Share Posted February 19, 2009 Dear Lord... lol thank you! Quote Link to comment https://forums.phpfreaks.com/topic/146010-i-know-the-header-needs-to-be-at-the-top-but-how-do-i-get-round-this-problem/#findComment-766607 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.