require / require_once behavior

Hello again. It’s been a little while but I have another question regarding the usage of require and require once.

I was trying to read up on the applicable documentation on require (which redirects me to include) and specifically whether upon failure I would be able to output content (like a debug message) to the browser before the script halted. However, I wasn’t able to find anything regarding this, and therefore am assuming this is not possible.

Are there other ways to test for the failure of including / requiring scripts, send content to the browser, and then die() or exit()?

Thanks!

Yes, many ways. I use this code:
define(‘INCLUDE_CHECK’,1);
require “scripts/functions.php”;
to make sure that the functions.php file is not loaded by someone who goes directly to a page.

You could reverse it and define a variable inside your include and then test for it’s value.

Or, you could use SESSION variables and set the variable inside the include. Then, test for it in your program.

Either way would work and be quite simple. Hope that helps…

You could reverse it and define a variable inside your include and then test for it's value.

Or, you could use SESSION variables and set the variable inside the include. Then, test for it in your program.

I noticed that you haven’t said anything about using require, so I’m guessing that’s another case entirely. I was hoping to do something like this using require without stopping output to the browser. Specifically:

[php]
if ((include_once “{$_SERVER[‘DOCUMENT_ROOT’]}/inc/nav/nav.php”) !== 1) {
echo “

Failure to Load Navigation

”;
}
else {
// Continue to load page
}
[/php]

As:
[php]
if((require_once “{$_SERVER[‘DOCUMENT_ROOT’]}/inc/auth/dbConn.php”) !== 1 || (require_once “${_SERVER[‘DOCUMENT_ROOT’]}/inc/auth/createUser.php”) !==1 )) {
// Redirect to error page or output error page in html format
}
else {
// Continue to load page
}
[/php]

But yeah, if that’s not possible, I’ll use your suggestion instead.

Perhaps I did not understand your question. Here is a way to include a file or display an error if it doesn’t load… Hope that is what you are looking for…

[php]
if(!@include($_SERVER[’/inc/nav/nav.php’])) {
echo “

Failure to Load Navigation

”;
}
[/php]

I think this should work. I didn’t test it, but, should be good. Good luck!

That’s essentially what I was looking for, though I don’t know why I was stingy at first using include vs require. Probably read some scary tutorial article regarding script security and how requires were “more secure” than includes. Anyway’s, here’s the code I have so far. The goal is to basically test to see if a needed script exists, and if it doesn’t (or was corrupted, can’t open, whatever…), output the proper HTTP headers, and send both a friendly message to users of the website that something went wrong, and a developer message for any webmasters and programmers.

[php]
if (!@include_once “{$_SERVER[‘DOCUMENT_ROOT’]}/inc/auth/dbConn.php”) {
header(“HTTP/1.1 500 Internal Server Error”);
//Will overwrite previous header and send HTTP 302 instead; commented out
//header(“Location: {$_SERVER[‘SERVER_NAME’]}/err/errMain.php”);
$errMessage = “Failure to locate database connection script.”;
$debugMessage = $php_errormsg;
exit;
}
[/php]

Well, it should work, except one issue. If you load the “include” and it has bad “guts/code”, you have to test it INSIDE the include.

What I mean is it the include loads anything outside of it or headers or ANY item that is not hard carded into the include, you would have to handle the error INSIDE the include. At least, that is how I see it.

So, if you have some include that pulls data from a DB or other includes, it may “break”. In which case, you really do not know how much was loaded if any. I have seen them load up to the broken line and then continue. So, you could do something like this:

$_SESSION[‘inlcude’]=“loading”;
insert your include…
And, have the last line of your include could be:
$_SESSION[‘include’]=“include_clean”;

Or something similar. This would tell you that the entire include loaded.

Looking back at your original question, I note you are looking at connecting to your DB and creating a user connection. Since you always want to connect to the DB and always want to have user info at hand, I usually use session variables. In the actual login page, once the user’s info is validated, I load their user ID from the DB into a session variable called $_SESSION[‘id’]; . Then, on EVERY page of the site, I check it for >0 and if it is not valid, the page redirects to a “Not Logged In” page. This is handle at the top of every page with one include that opens the DB connection and validates the user ID. Also, in this way, if someone types in the name of the page into the browser, they get a not-logged-in page. Sorry if this is off topic. Just FYI…

Did your new changes work for you this time? If not let us know what you got for errors. Good luck…

Looking back at your original question, I note you are looking at connecting to your DB and creating a user connection. Since you always want to connect to the DB and always want to have user info at hand, I usually use session variables.

Thanks for the suggestion. That particular code you saw was for a registration page, which I haven’t gotten around to programming that just yet. Still have some issues to iron out regarding validation and sanitation; I’ll ask separate questions as needed for clarification.

Well, it should work, except one issue. If you load the "include" and it has bad "guts/code", you have to test it INSIDE the include.

What I mean is it the include loads anything outside of it or headers or ANY item that is not hard carded into the include, you would have to handle the error INSIDE the include. At least, that is how I see it.

That’s something I didn’t realize; that the variables declared outside of the include are accessible to the include code. I don’t mind handling semantic or logical errors in the include; the main reason for this question was to return HTTP 500 [ friendly error page to the user / comprehensive page to developer ] back upon script loading. After reading around these articles:

[ul][li]Apache Error Document Does Not Redirect[/li]
[li]Apache Fall Back when PHP Fails[/li]
[li]How to Create a Useful 500 Internal Server Error Page[/li]
[li]PHP Rant[/li][/ul]

I learned this is pretty much impossible. The closest I can come is to “fake” the error to the client using:

[php]<?php
set_error_handler(500);
// Output Error Page Content Afterwards
exit(); // or die(); They’re both exactly the same I learned
?>
[/php]

Or have PHP call a script to break something else via perl CGI script to trick the Apache server to send its own 500 code. LOL at that hack! :stuck_out_tongue:

Well, I am a little lost in what you need at this point. First, let’s talk about a #500 error. That is just a web page on a server that declares that there is a 500 error. You can call that page directly on your site if you just find out where it is. Then, you can use PHP to redirect to it when needed.

Now, also, you must think of an include as just code you have in place. So, if you have some “include” file that has, let’s say a routine to connect to your database and make sure the current user is already logged in, this is just PHP code that is “added” to the current code.

So, code like

echo “Ernie says something”;
include “someconnectioncode.php”;
echo “it worked, did it?”;

The included file is just part of the CURRENT file and it is just like you manually typed in the included file.
the ENTIRE include file is placed between the two echo’s and is just code. It follows INLINE and whatever
code is in it follows in order.

Therefore, you can add include’s thru PHP conditions and mix and match tons of HTML/JS/PHP code depending on various options… So, I guess you have to think of a program that flows IN-LINE. One line of code after another. If you have an include, you must think of this as being just another paragraph of code PLACED in the middle of other code. That’s all it is. It fits right in wherever you place the INCLUDE command…

So, since it is in-line code, you can use it that way and test previous include code in order.
You can place several includes one after the other and they can compare the CONTENTS of previous includes… Seems silly, but, in some odd situations, it is handy. One example, I have advertisements on one site that are on sidebars. They do not show up on directly going to the site manually, but, show up if logged in as a member. So, this is done with a conditional include.

Not sure why you would want to force a #500 error instead of putting up a nice error message of your own?

Well, not sure if this was over-bloat or helpful… Hopefully, the latter… What can we do at this point to help?

Short Version: You’ve answered my question already! :smiley: I greatly appreciate the responses. The information regarding how includes are processed was just extra information :stuck_out_tongue:

Long Version: Here’s what I’m planning to do…

Wouldn’t redirecting the user change the HTTP Response code sent to the browser to HTTP #302?

Oooooh, “flowcharts”! (Said like Homer Simpson thinking of donuts! LOL) Don’t see them often these days, kinda miss em a bit… LOL

So, the answer is yes and no. If you are using PHP and you redirect to another page, there is no need to handle a HTTP browser response code. If the include has a bad error, it will “break” the page more often than not. This will not generate much of an error, but, will just break the page. (As far as I know.)

For error messages and pages, I have used them in this manner. I create a nice error message page that looks like the rest of the site, but, without the “fluff” on it. (No ad’s, not as fancy…) It contains three or four areas that contain Heading, Error-Message, Error-Details and a return link of some sort. I load these into SESSION variables and send the user to that page. They see a page similar to the rest of the site, but, with a nice, YOU-MESSED-UP header, with what the error message is and the “details” on how to fix it and how not to let it happen again. These are all sent from the page that located the error and creates the text for each of these items.

In your flowchart, you mentioned database connection failures. These should be handled in the connection code itself. If the connection fails, that code should contain it’s own error handler for that section. But, since the page can not connect to the DB, it needs to end anyways, so loading up a few SESSION variables and redirecting to the the error-message page is okay. The return links sent to it would be to return to somewhere before the connection was tried. If it was a login page, as an example, the return link would be to the login page.

Of course with the new validation routines, most of these types of errors are just done by a pop-up alert or by a DIV tag which just appears from nowhere with the error message inside it.

I also noticed in your flowchart you mention developer vs. user. So, I am assuming your site does some sort of dual access on pages. One for common user’s and one for developers. This can be confusing. Most sites separate their developers from their users. They have ADMIN control panels for the developers and the users use the rest of the site.

Well, I am one that always gives way too much information, but, like people to think outside the box a bit.
Once you have another question, let us know… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service