shortysbest Posted April 2, 2010 Share Posted April 2, 2010 is there a simple code for an error page so if the page url is: sw.php?page=home and then the user changes url to something like.. well anything other than home or another valid page in the site. it goes to the url: sw.php?page=error I had written a script that does this however it writes the page content in also and the redirection code has to be in the head yet the script has to be in the body. Quote Link to comment https://forums.phpfreaks.com/topic/197365-php-erorr-page/ Share on other sites More sharing options...
ialsoagree Posted April 2, 2010 Share Posted April 2, 2010 switch ($_GET['mode']) { case 'home': // Do home stuff here break; case 'whatever': // Do a different page here break; case 'error': // Show your error page here break; default: header('Location: ./sw.php?mode=error'); // Sending a header does NOT end the script from processing, if you don't want the script to continue, you should use die() or exit break; } Untested, but I believe this should work. Quote Link to comment https://forums.phpfreaks.com/topic/197365-php-erorr-page/#findComment-1036098 Share on other sites More sharing options...
mikesta707 Posted April 2, 2010 Share Posted April 2, 2010 alternatively you can store all valid pages in an array, or a datebase, and check that way. It would probably look a little cleaner than a long switch block (especially if you have a lot of pages) but would work more or less the same. for example, using an array $valid_pages = array("home", "main", "etc", "etc"); $mode = $_GET['mode']; if (!in_array($mode, $valid_pages)){ //redirect to error } in array: http://php.net/manual/en/function.in-array.php Quote Link to comment https://forums.phpfreaks.com/topic/197365-php-erorr-page/#findComment-1036102 Share on other sites More sharing options...
shortysbest Posted April 2, 2010 Author Share Posted April 2, 2010 switch ($_GET['mode']) { case 'home': // Do home stuff here break; case 'whatever': // Do a different page here break; case 'error': // Show your error page here break; default: header('Location: ./sw.php?mode=error'); // Sending a header does NOT end the script from processing, if you don't want the script to continue, you should use die() or exit break; } Untested, but I believe this should work. This code didnt work. Quote Link to comment https://forums.phpfreaks.com/topic/197365-php-erorr-page/#findComment-1036103 Share on other sites More sharing options...
ialsoagree Posted April 2, 2010 Share Posted April 2, 2010 This code didnt work. Is there anything specific that didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/197365-php-erorr-page/#findComment-1036104 Share on other sites More sharing options...
shortysbest Posted April 2, 2010 Author Share Posted April 2, 2010 This code didnt work. Is there anything specific that didn't work. Well i had to put the name of all my pages in there right, I didn't do that. I'm hoping to find a script that will automatically know which pages i have. I have a script that does that however it also writes the inner pages in as well and i need to put the error part in header, but cannot with having it write the page in body. idk maybe you or someone could help rewrite this for me? im not sure.. im new to php and all. $page = $_GET['page']; if ($page) { $path = "inc/".$page.".php"; if(file_exists($path)) { include($path); } } else { $URL="sw.php?page=home"; header ("Location: $URL"); } Quote Link to comment https://forums.phpfreaks.com/topic/197365-php-erorr-page/#findComment-1036106 Share on other sites More sharing options...
ialsoagree Posted April 2, 2010 Share Posted April 2, 2010 I'm not sure I'm totally understanding what you're asking. That function you posted seems like a great method for determining if the page exists (and I used the wrong _GET index, sorry about that). From what you're saying, I think the problem is that you have a script that writes a generic header and you need to modify that header if you're displaying an error page. Is that what you're asking? OR, are you saying that you can't use the header() function because information was already sent to the browser before? Quote Link to comment https://forums.phpfreaks.com/topic/197365-php-erorr-page/#findComment-1036108 Share on other sites More sharing options...
shortysbest Posted April 2, 2010 Author Share Posted April 2, 2010 I'm not sure I'm totally understanding what you're asking. That function you posted seems like a great method for determining if the page exists (and I used the wrong _GET index, sorry about that). From what you're saying, I think the problem is that you have a script that writes a generic header and you need to modify that header if you're displaying an error page. Is that what you're asking? OR, are you saying that you can't use the header() function because information was already sent to the browser before? I actually posted the wrong code above was actually the modified code. it didn't have the else statement that sends you to the error: $page = $_GET['page']; if ($page) { $path = "inc/".$page.".php"; if(file_exists($path)) { include($path); } else { $URL="sw.php?page=error"; header ("Location: $URL"); } } else { $URL="sw.php?page=home"; header ("Location: $URL"); } But yeah, This code is posted down in a <div> half way down the page so it writes in other pages below the header and everything. The error script has to be the first script in the code to run for it to work though and I am just having problems rewriting it so it looks for the file in the path and then if it isn't a valid page returning the user with the sw.php?page=error directory. It works on the localhost exactly how i want it to, just it isn't a valid way of doing it so it doesnt work on the actual server. Say if you click the link to homepage you go to sw.php?page=home But if the user edits the code like: sw.php?page=77haskjfd7ydshkfadf it would redirect them to sw.php?page=error I have an .htaccess error script but that only works for the sw.php part (only goes to error page when that has been changed) not the, well sub pages i guess you call them. I'm sorry if i'm not explaining it clearly. but i hope i have. And thanks for taking the time to try to help with my problem. I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/197365-php-erorr-page/#findComment-1036114 Share on other sites More sharing options...
ialsoagree Posted April 2, 2010 Share Posted April 2, 2010 From what I'm gathering, the problem is that the header function doesn't work because you've already sent HTML to the browser. It's usually good practice to buffer all your output, and then when your script is done to send all the output back to the user. This helps you avoid situations like this one, where you've started preparing output but suddenly you need to change what you're doing but it's already too late. There's 2 methods to do this, I personally like to use objects that store all the page's HTML as the PHP script executes, and then when I'm done, a method in the object outputs the HTML to the browser and ends the script. But, another method is to use PHP's output buffering functions. When you start output buffering, which is as simple as calling a single function, PHP saves everything you tell your script to echo until you turn output buffering off at which point it sends everything you've echoed to the browser. The nice part is that, header's aren't buffered, so you can still send headers, even after you've used echoes to send output. The output buffering manual is here (in english): http://php.net/manual/en/book.outcontrol.php The specific functions you're looking for are: http://www.php.net/manual/en/function.ob-start.php and http://www.php.net/manual/en/function.ob-end-flush.php Quote Link to comment https://forums.phpfreaks.com/topic/197365-php-erorr-page/#findComment-1036121 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.