b1011 Posted September 9, 2007 Share Posted September 9, 2007 I did the mistake of writing my whole website on one index page :-\ The problem is, each page uses if ($_GET['pg']==page){ #code } But if it is not a page, it doesn't execute anything. and the whole site is messed up. Is there anyway to make it so if $_GET['pg'] is not a valid page, then go to a 404 script? Link to comment https://forums.phpfreaks.com/topic/68617-solved-if-page-doesnt-exist/ Share on other sites More sharing options...
torb Posted September 9, 2007 Share Posted September 9, 2007 I would have tied them up something like this: if ($_GET['pg']==page){ #code } elseif ($_GET['pg']==page2){ #code } elseif ($_GET['pg']==page3){ #code } else { echo "Holy crud! There's nothing here!"; } Link to comment https://forums.phpfreaks.com/topic/68617-solved-if-page-doesnt-exist/#findComment-344878 Share on other sites More sharing options...
GingerRobot Posted September 9, 2007 Share Posted September 9, 2007 Alternatively, you could define an array of possible pages: <?php $pages = array('page1','page2','page3');//array of possible pages; if(isset($_GET['pg']){ if(!in_array($_GET['pg'],$pages)){ header("location:error.php"); exit; }else{ //your nasty block of if statements here } } ?> A better solution would be a re-write so you dont have such a block of if-elseif statements. Link to comment https://forums.phpfreaks.com/topic/68617-solved-if-page-doesnt-exist/#findComment-344880 Share on other sites More sharing options...
b1011 Posted September 9, 2007 Author Share Posted September 9, 2007 torb. your idea worked like a charm. Thank you alot. this has been a problem for a while. Link to comment https://forums.phpfreaks.com/topic/68617-solved-if-page-doesnt-exist/#findComment-344894 Share on other sites More sharing options...
prodigy2k7 Posted September 10, 2007 Share Posted September 10, 2007 This is what I like to do... <? $page = $_GET['page']; ?> <? if ($page == "") { include "home.php"; } else { if (file_exists($page . ".php")) { include $page . ".php"; } else { echo "<center>The page <b>$page</b> does not exist.<br>Check back soon!</center>"; } } ?> Basically a URL with http://domain.com/index.php?page=modules "modules.php" will be included if it exists. If not, it echos a does not exist... I like it cuz its simple... Link to comment https://forums.phpfreaks.com/topic/68617-solved-if-page-doesnt-exist/#findComment-345044 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.