Mr Chris Posted October 1, 2010 Share Posted October 1, 2010 Afternoon All. I wish to re-direct users to a 404 error page on my site if an article does not exist in my database. Here's my code: $SQL = "SELECT headline FROM news WHERE news_id=".mysql_real_escape_string($_GET['news_id']); $result = mysql_query($SQL) OR die(mysql_error()); $num = mysql_num_rows($result);//** Check that the entry exists otherwise send to error pageif ($num > 0) { $row = mysql_fetch_array($result); $headline = $row['headline'];} else { echo "Why is this printed? - I should be leaving this page?"; header("Location: error.php"); exit;} Now the wierd thing is that when I enter a news_id for a value that does not exist it prints the message Why is this printed? - I should be leaving this page? so it's actually going to the ELSE statement which is good, but surely it should not do this as I ask the page to re-direct? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/ Share on other sites More sharing options...
litebearer Posted October 1, 2010 Share Posted October 1, 2010 are you getting any error messages? Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1117976 Share on other sites More sharing options...
Pikachu2000 Posted October 1, 2010 Share Posted October 1, 2010 How can you know which part of the conditional should be executed if you don't find out what the value of $num is by echoing it? Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1117981 Share on other sites More sharing options...
Mr Chris Posted October 1, 2010 Author Share Posted October 1, 2010 Thanks but nope, no error messages. As for the value of $num I do find it out $num = mysql_num_rows($result)? Just wondered if there was anything obvious? Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1117983 Share on other sites More sharing options...
litebearer Posted October 1, 2010 Share Posted October 1, 2010 have you tried using a meta redirect? Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1117993 Share on other sites More sharing options...
Pawn Posted October 1, 2010 Share Posted October 1, 2010 There's nothing wrong with the code. Are you sure the headers aren't already sent? if(!headers_sent()) { header('Location: error.php'); exit; } echo "Headers already sent!"; Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1117998 Share on other sites More sharing options...
Mr Chris Posted October 1, 2010 Author Share Posted October 1, 2010 There's nothing wrong with the code. Are you sure the headers aren't already sent? Thats the problem - any way around this apart from echoing a message saying there is no link or a meta redirect? Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1118003 Share on other sites More sharing options...
Pawn Posted October 1, 2010 Share Posted October 1, 2010 Usually you can reorder the structure of the PHP such that checks that send headers occur at the start of the code, before any output begins. Is there some reason this isn't an option in your case? Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1118014 Share on other sites More sharing options...
jcbones Posted October 1, 2010 Share Posted October 1, 2010 Also, some browsers (Chrome being one) will not redirect unless you also set the Status to 200: header('Status: 200'); Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1118015 Share on other sites More sharing options...
Mr Chris Posted October 1, 2010 Author Share Posted October 1, 2010 Thanks Yeah, I can do. Just how i've created the page. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1118019 Share on other sites More sharing options...
Mr Chris Posted October 1, 2010 Author Share Posted October 1, 2010 Actually, sorry how would I reorder the structure of my PHP to do this. I've misunderstood, do you mean re-order what i've done here?: $SQL = "SELECT headline FROM news WHERE news_id=".mysql_real_escape_string($_GET['news_id']); $result = mysql_query($SQL) OR die(mysql_error()); $num = mysql_num_rows($result); //** Check that the entry exists otherwise send to error page if ($num > 0) { $row = mysql_fetch_array($result); echo $headline = $row['headline']; } else { header("Location: error.php"); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1118053 Share on other sites More sharing options...
BlueSkyIS Posted October 1, 2010 Share Posted October 1, 2010 put that code before any output; before any html, before any empty spaces, before any content is sent to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1118054 Share on other sites More sharing options...
Mr Chris Posted October 1, 2010 Author Share Posted October 1, 2010 Thanks. Had another thought and added <? ob_start();?> To the first part of the script. That won't cause any issues will it and fine to do it this way? Quote Link to comment https://forums.phpfreaks.com/topic/214912-redirect-users-to-a-new-page/#findComment-1118061 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.