Kryllster Posted December 19, 2012 Share Posted December 19, 2012 I have some code some of it mine some of it others and someone im sure has already thought of it but I am trying to make an index.php controller page. But what I can figure out is haow to displa a simple "This Page Does Not Exist" error and Im having no luck. The code I will show works except for the above mentioned. ============================================================== <?php // header include('pages/main_header.php'); $dir = 'pages'; $files = scandir($dir); $page = (isset($_GET['page'])) ? 'intro' : $_GET['page']; if (!in_array($page, $files)) { require_once('pages/' . $_GET['page'] . ".php"); } //footer include('pages/main_footer.php'); ?> Any tips or solutions would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/ Share on other sites More sharing options...
Mad programmer Posted December 19, 2012 Share Posted December 19, 2012 Hello, I looked quickly over your code, but I do not have the possibillity to test it out right now. After your if statement you could add an else and include the 404 page from there. That should work for you. Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400249 Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 proper way would be to edit your web server's default settings for custom error messages for 404 page not found to point to your own page, normaly with a logging script on it and a more human friendly message. Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400252 Share on other sites More sharing options...
Kryllster Posted December 19, 2012 Author Share Posted December 19, 2012 I tried this but it didnt work and Im not sure about the custom error page ======================================================== <?php // header include('pages/main_header.php'); $dir = 'pages/'; $files = scandir($dir); $page = (!isset($_GET['page'])) ? 'intro' : $_GET['page']; if (!in_array($page, $files)){ require_once('pages/' . $_GET['page'] . ".php"); } else { echo "That Page Does Not Exist"; } //footer include('pages/main_footer.php'); ?> This didnt work!! Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400263 Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 let's see a var_dump() of $files Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400265 Share on other sites More sharing options...
Kryllster Posted December 19, 2012 Author Share Posted December 19, 2012 here is the var_dump(); ================================ array(37) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(9) "about.php" [3]=> string(11) "airport.php" [4]=> string(13) "amusement.php" [5]=> string(11) "brawler.php" [6]=> string( "chat.php" [7]=> string(10) "combat.php" [8]=> string(12) "congrats.php" [9]=> string(10) "create.php" [10]=> string(10) "defeat.php" [11]=> string(12) "downtown.php" [12]=> string(10) "footer.php" [13]=> string(10) "header.php" [14]=> string(12) "industry.php" [15]=> string(9) "intro.php" [16]=> string(13) "loggedout.php" [17]=> string(12) "magician.php" [18]=> string( "main.php" [19]=> string(15) "main_footer.php" [20]=> string(15) "main_header.php" [21]=> string(12) "maindrag.php" [22]=> string( "news.php" [23]=> string(7) "old.php" [24]=> string(15) "playerstats.php" [25]=> string(12) "recovery.php" [26]=> string(13) "safeplace.php" [27]=> string(10) "sewers.php" [28]=> string(9) "slums.php" [29]=> string(9) "stats.php" [30]=> string(11) "suburbs.php" [31]=> string(10) "tanker.php" [32]=> string(16) "technologist.php" [33]=> string(4) "test" [34]=> string(11) "victory.php" [35]=> string(16) "vilinterface.php" [36]=> string(13) "weaponeer.php" } ====================================== I dont know what exactly all that means but its reading the files in the directory. Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400266 Share on other sites More sharing options...
Mad programmer Posted December 19, 2012 Share Posted December 19, 2012 (edited) Hello again! I have tested your code on my local server and I got several errors, with these errors the problem was quite easy to find. If you take a look at the code you seem to have made a little mistake ;P $page = (!isset($_GET['page'])) ? 'intro' : $_GET['page']; if (!in_array($page, $files)){ require_once('pages/' . $_GET['page'] . ".php"); } The problem is on the 3rd line above, on the first line you set the variable $page to intro if $_GET['page'] is not set else you just use the $_GET variable. However on the 3rd line you are using the $_GET variable instead of the $page variable. This should work for you: $page = (!isset($_GET['page'])) ? 'intro' : $_GET['page']; if (!in_array($page, $files)){ require_once('pages/' . [color=#ff0000]$page[/color] . ".php"); } Edited December 19, 2012 by Mad programmer Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400267 Share on other sites More sharing options...
Kryllster Posted December 19, 2012 Author Share Posted December 19, 2012 Works perfectly thanks so much I see the problem tho. I would maybe have gotten it prolly not like thatnks to all for your suggestions and the final solution. Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400268 Share on other sites More sharing options...
Kryllster Posted December 19, 2012 Author Share Posted December 19, 2012 Ok well it looks like I jusmped the gun I only tested default.php?page=test when I had a folder in the main directory called test. After I delelted that folder the same error messages pop up. =========================================================== Warning: require_once(pages/test.php): failed to open stream: No such file or directory in C:\xampp\htdocs\vang\default.php on line 8 Fatal error: require_once(): Failed opening required 'pages/test.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\vang\default.php on line 8 ============================================================= back to the drawing board Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400273 Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 updated code looks like what now? Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400277 Share on other sites More sharing options...
Kryllster Posted December 19, 2012 Author Share Posted December 19, 2012 <?php // header include('pages/main_header.php'); $dir = 'pages/'; $files = scandir($dir); $page = (!isset($_GET['page'])) ? 'intro' : $_GET['page']; if (!in_array($_GET['page'], $files)){ require_once('pages/' . $page . ".php"); } else { echo "This page does not exist!!"; } //footer include('pages/main_footer.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400279 Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 your logic looks to be back to front, and you should probably make sure that all the URL variables are in the right case as in_array() is case sensitive. Try this: <?php // header include('pages/main_header.php'); $dir = 'pages/'; $files = scandir($dir); $page = (!isset($_GET['page'])) ? 'intro.php' : $_GET['page']; if (!in_array($page, $files)){ echo "This page does not exist!!"; } else { require_once "pages/{$page}"; } //footer include('pages/main_footer.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400281 Share on other sites More sharing options...
Kryllster Posted December 19, 2012 Author Share Posted December 19, 2012 Yeah it does seem a little backwords I tried copying your code it didnt work for my site Im not sure what else to do from here Im gooleing my butt off trying to find an answer! Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400285 Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 "it didn't work" isn't the most helpfull of statements... Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400291 Share on other sites More sharing options...
Kryllster Posted December 19, 2012 Author Share Posted December 19, 2012 Sorry about that, Im just tired, Im trying to write something that is way over my head and Im sorry for taking up your time. Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400294 Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 I'm just asking - what's not working about it? is there still an error coming up? if so what is it? is it always saying page not found when there should be a page there? If I know what the problem is I can keep trying to help. Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400300 Share on other sites More sharing options...
Kryllster Posted December 19, 2012 Author Share Posted December 19, 2012 Im not getting any error message if the file is in the pages directory I guess what Im looking for is an error message that says something to the effect that the file is unavailabe with a custom error message instead of what was posted earlier. Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400309 Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 right, how about we try this my way then, are you using apache? and if so do you have access to a .htaccess file? Quote Link to comment https://forums.phpfreaks.com/topic/272169-help-with-indexphp/#findComment-1400311 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.