How do you stop errors of invalid .inc files?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you stop errors of invalid .inc files?

Post 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;
}
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Brilliant - got the first bit working.
Will also look at the second bit, as that's really clever.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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).
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

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

Post 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.
Last edited by x_mutatis_mutandis_x on Wed Apr 25, 2012 9:35 am, edited 1 time in total.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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'.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

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

Post 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' ); }
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

And '/bookconfirmation' is the error page if they reach Document Expired ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

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

Post 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).
Last edited by x_mutatis_mutandis_x on Thu Apr 26, 2012 9:17 am, edited 1 time in total.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

How is this capturing if it is a "Document Expired" error?
Don't understand - sorry.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

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

Post 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)
Last edited by x_mutatis_mutandis_x on Thu Apr 26, 2012 9:17 am, edited 1 time in total.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

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

Post 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
Last edited by x_mutatis_mutandis_x on Thu Apr 26, 2012 9:19 am, edited 1 time in total.
Post Reply