Page 1 of 2

How do you stop errors of invalid .inc files?

Posted: Sat Mar 03, 2012 2:37 pm
by simonmlewis
Hi

We use shorturls for our site, but people will obviously sometimes enter the wrong word after a /. ie. not domain.com/help, but domain.com/hlp

And then it cannot find that page and throws an INCLUDE error.

So is there something in the include script that can be added, so that if it cannot find the file, it redirects?

Here is my include script:

Code: Select all

$page = $_GET['page'];
if ($page == "") { }
else
{
function getPage()
{
 $thispage="includes/".$_GET['page'].".inc";
 include $thispage;
}  
} 

$menu = $_GET['menu'];
if ($menu == "") {}
else
{
function getMenu()
{
$thismenu="includes/menu/".$_GET['menu'].".inc";
include $thismenu;
}
}

Re: How do you stop errors of invalid .inc files?

Posted: Sat Mar 03, 2012 2:47 pm
by califdon
Sure. In your function getPage(), before you try to include the file, test whether the file exists (http://php.net/manual/en/function.file-exists.php) and if it doesn't, refer to an error page instead of the normal page.

Of course, if you wanted to get fancy, you could compare what the user supplied with valid filenames, perhaps a small database table, using such PHP functions as levenshtein() (http://us.php.net/manual/en/function.levenshtein.php), which calculates the "levenshtein distance" between 2 strings, showing how many letter substitutions are necessary to convert one into the other. This would make it fairly easy to recognize "hlp" as "help", for example.

Re: How do you stop errors of invalid .inc files?

Posted: Sat Mar 03, 2012 3:12 pm
by simonmlewis
Brilliant - got the first bit working.
Will also look at the second bit, as that's really clever.

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 8:59 am
by simonmlewis
Got a new issue related to this one.

If someone is posting page after page thru Form POSTs, and they hit the Back button, you get:

[text]Document Expired[/text]

I know you can hit Refresh and Resend, but most don't. How can in PHP you capture this page, and then direct them somewhere else? Ideally back to the first page of POSTS (tho the site may have more than one set of POSTS).

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 9:29 am
by x_mutatis_mutandis_x
simonmlewis wrote:Got a new issue related to this one.

If someone is posting page after page thru Form POSTs, and they hit the Back button, you get:

[text]Document Expired[/text]

I know you can hit Refresh and Resend, but most don't. How can in PHP you capture this page, and then direct them somewhere else? Ideally back to the first page of POSTS (tho the site may have more than one set of POSTS).
You can easily accomplish that:
page1.php (contains form) on form submit -> processPage1.php (process the $_POST data, header('Location: where ever you want to redirect, back to page1.php or a confirmationPage.php);)

What do you mean by one set of posts? You mean forms chained one after another? Just repeat the above for each of the form submission:
page1.php (contains form) on form submit -> processPage1.php (process the $_POST data, header('Location: page2.php');) -> page2.php (contains form2) on form submit -> processPage2.php (process $_POST data; header('Location: page3.php');) .... and so on

If you render html in processPage.php, then the user can click back and they would see 'Document Expired' because the post data is still cached on the browser's end tied to the request which they can re-send by refreshing the page. By doing a client-side redirect using "header()" it will create a new request to the redirect location without any POST data.

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 9:34 am
by simonmlewis
I am posting to domain.co.uk/book each time tho, via <form method='POST' action='/book'>.
So how would I do what you suggest? Bearing in mind, '/book' is "index.php?page=book'.

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 9:38 am
by x_mutatis_mutandis_x
simonmlewis wrote:I am posting to domain.co.uk/book each time tho, via <form method='POST' action='/book'>.
So how would I do what you suggest? Bearing in mind, '/book' is "index.php?page=book'.
Just replace page1.php with '/book', and in index.php: if ($_GET['page'] == 'book') { processBook(); header(Location: '/bookConfirmation' ); }

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 9:41 am
by simonmlewis
And '/bookconfirmation' is the error page if they reach Document Expired ??

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 9:45 am
by x_mutatis_mutandis_x
simonmlewis wrote:And '/bookconfirmation' is the error page if they reach Document Expired ??
Its the page you take them to after you process, you can even take them to error page if your process failed or better yet show errors on top of your form in '/book'. So basically:

Code: Select all

if ($_GET['page'] == 'book') { 

if (!empty($_POST)) {

$errorCode = processBook(); 

if ($errorCode == 0) {
header('Location: /bookConfirmation' ); 
} else {
header('Location: /bookError'); //or if showing errors in form header(Location: "/book?errorCode=$errorCode");
}

} else {
//show form html for '/book' request
//(and error message if passing the error code)
}

}
EDIT: Oops I forgot to check if data was posted to before I can call processBook(), in my previous reply (code above should be correct).

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 10:00 am
by simonmlewis
How is this capturing if it is a "Document Expired" error?
Don't understand - sorry.

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 10:19 am
by x_mutatis_mutandis_x
Not a problem, buddy. Well lets go back to basics.

Scenario1:
- Page1: Form resides in one page1 and gets submitted to page2
- Page2: Process the data from page1 form, and show html to user.

Now the user is in Page2 and if he clicks back he will be taken to Page1 but since the browser cached data that got submitted before it will warn that the cached data is "expired" and if user wants to "re-submit" when he refreshes it.

Scenario2:
- Page1: Form resides in one page1 and gets submitted to page2
- Page2: Process the data from page1 form, redirect to Page3 (do not show html here).
- Page3: Show html to confirm form got processed.

Now user is in Page3. So instead of sending html in response from page2, you sent a "redirect" in your response. So, the browser will create a new request and send it to the redirect page (Page3). Now when user clicks back, the cached data is empty because it is associated with the request from Page2 to Page3 (and Page2 has no form data since you weren't showing any html at all) and the user will be taken back to Page1.

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 10:25 am
by simonmlewis
Sorry my friend, all this "page 2 to page 3, back to page 2 and taken back to page 1"... etc... is totally muddling up my mind.
Are you saying there is a better way to POST data, that doesn't get lost?? Is this a fix for that error - so in essence, capturing "browser about to show error page", and then taking you elsewhere?

Incidentially, it's all happening within the same page. Don't think that's totally relevant here, but said for clarity.

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 11:00 am
by x_mutatis_mutandis_x
x_mutatis_mutandis_x wrote:
simonmlewis wrote:And '/bookconfirmation' is the error page if they reach Document Expired ??
Its the page you take them to after you process, you can even take them to error page if your process failed or better yet show errors on top of your form in '/book'. So basically:

Code: Select all

if ($_GET['page'] == 'book') { 

if (!empty($_POST)) {

$errorCode = processBook(); 

if ($errorCode == 0) {
header('Location: /bookConfirmation' ); 
} else {
header('Location: /bookError'); //or if showing errors in form header(Location: "/book?errorCode=$errorCode");
}

} else {
//show form html for '/book' request
//(and error message if passing the error code)
}

}
EDIT: Oops I forgot to check if data was posted to before I can call processBook(), in my previous reply (code above should be correct).

And this is what you would put in index.php.

Are you using any url rewriting as in /book mapped to index.php?page=book ? The page can be the same, the request should be different (I used page1, page2, page3 to indicate how the request/response works for each page)

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 11:05 am
by simonmlewis
Yes, HTACCESS is rewriting the URLS to be shorturls.

if (!empty($_POST))
Does this check to see if you are in the /book page, and if $_POST is empty, it then sends you to the error page? If so, this too will be a problem, because you reach /book before event submitting anything.

Re: How do you stop errors of invalid .inc files?

Posted: Wed Apr 25, 2012 11:10 am
by x_mutatis_mutandis_x
simonmlewis wrote:Yes, HTACCESS is rewriting the URLS to be shorturls.
Thats ok, I was just wondering
if (!empty($_POST))
Does this check to see if you are in the /book page, and if $_POST is empty, it then sends you to the error page? If so, this too will be a problem, because you reach /book before event submitting anything.
It checks if a form got submitted from index.php?page=book. If $_POST is empty, it will show the form. If it's not empty, then (form was submitted) process (validation, updating database, etc..) the data from $_POST, and if there is an error in processing (i.e., if ($errorCode == 0)), then show error page, else show confirmation page.

Here's the code, properly formatted:

Code: Select all

if ($_GET['page'] == 'book') { 

              if (!empty($_POST)) {
 
                            $errorCode = processBook(); 

                            if ($errorCode == 0) {
                                           header('Location: /bookConfirmation' ); 
                            } else {
                                           header('Location: /bookError'); //or if showing errors in form header(Location: "/book?errorCode=$errorCode");
                            }
 
             } else {
                             //show form html for '/book' request
                            //(and error message if passing the error code)
             } 
}
  
EDIT: Corrected "Location:" to be in quotes, was a typographical error on my part