Page 1 of 2

Include redundancy

Posted: Tue Sep 28, 2010 5:19 am
by AdamBlackwell
This is my first post, so I would like to say hi to everyone first!

I have recently embarked on my first major project using php & msqli and it is important that everything is as good as it can be. I have used include() to pull together modules of code, as standard design suggests. But I have become aware that the include() function is not 100% reliable and as such have began looking at a fail eligant solution.

The basic solution I have come up with is to attempt to save the $_POST array into a file for later use and then refresh the page for another attempt to include() the critical modules of code.

Am I over cooking it with this level of redundancy, or is there something I am missing?
Any thoughts & suggestions are much appreciated.

Best regards
Adam

Re: Include redundancy

Posted: Tue Sep 28, 2010 5:24 am
by DigitalMind
AdamBlackwell wrote:to include() the critical modules of code
I would use require() (require_once) instead of include() in this case

Re: Include redundancy

Posted: Tue Sep 28, 2010 5:28 am
by AdamBlackwell
Thanks for your fast responce DigitalMind

I used include() because failure does not produce a critical error, thus the failure can be managed

Re: Include redundancy

Posted: Tue Sep 28, 2010 5:43 am
by DigitalMind
BTW who told you include() is not 100% reliable and why?
If you still want to save $_POST data somewhere, why not to save it in $_SESSION? It might be easier to check if some data is in the "buffer". Or I got you wrong? :)

Re: Include redundancy

Posted: Tue Sep 28, 2010 6:06 am
by Eran
Am I over cooking it with this level of redundancy, or is there something I am missing?
Yes. The include operation is completely dependable, once you have the correct path of the file to include. Debug your site before you deploy it to a live environment and this shouldn't be an issue. If the included file is required for the site to operate, use require as Alexander suggested.
include() should produce some error warnings if the file is not found. If you aren't seeing those, you should increase the level of your error_reporting and turn on display_errors

Re: Include redundancy

Posted: Tue Sep 28, 2010 6:14 am
by AdamBlackwell
I read that includes can very occasionally fail on page 527 in chapter 25 of PHP and MySQL Web Development, third edition by Luke Welling & Laura Thomson. But having re-read that bit it does attribute the cause to hard drive problems and human error in directory permissions changing.

The problem with using $_SESSION is that it is tempory, and if the error means the user leaves the computer then any data is then lost. I want the user to be able to leave the computer and the data be available for processing next time...

I just realised that if include() is not working for the above reasons then I wont be able to write to a file anyway :oops: .

Thanks pytrin, I think I have over estimated the chances of any failure.

Re: Include redundancy

Posted: Tue Sep 28, 2010 8:10 am
by alex.barylski
I read that includes can very occasionally fail on page 527 in chapter 25 of PHP and MySQL Web Development, third edition by Luke Welling & Laura Thomson. But having re-read that bit it does attribute the cause to hard drive problems and human error in directory permissions changing.
If you have HDD problems or permission errors, no function will work on that file. That book (if I am thinking of the current one) is really dated in many of it's practices.
The problem with using $_SESSION is that it is tempory, and if the error means the user leaves the computer then any data is then lost. I want the user to be able to leave the computer and the data be available for processing next time...
Session ID's are stored in cookies, by default they exist for as long as the browser window is opened. I believe you change these expiry values so that SID's are persisted for longer period of time.

http://ca.php.net/manual/en/function.se ... params.php

Cheers,
Alex

Re: Include redundancy

Posted: Tue Sep 28, 2010 10:05 am
by AdamBlackwell
Hi Alex, thanks for your reply. The book is a few years old (2004) it has taken me about 4 years of reading and practice to get to a stage of doing something big with it all and I really want a design that is robust, hence all the redundancy stuff.

Using cookie control is a great suggestion, thank you!
PCSpectra wrote:If you have HDD problems or permission errors, no function will work on that file.
I know very little about servers and since HDD problems or permission errors are essentially what I am trying to code a solution for I may be wasting my time if nothing will run anyway. When you say "no function will work on that file" if possible can you expand a little more on this please!

Thanks in advance
Adam

Re: Include redundancy

Posted: Tue Sep 28, 2010 2:23 pm
by John Cartwright
AdamBlackwell wrote:I know very little about servers and since HDD problems or permission errors are essentially what I am trying to code a solution for I may be wasting my time if nothing will run anyway. When you say "no function will work on that file" if possible can you expand a little more on this please!

Thanks in advance
Adam
What Alex meant was, if you are having hard drive failure, include() not working is the least of your concern, considering your entire server is going down the toilet (not just this particular function)!

Re: Include redundancy

Posted: Tue Sep 28, 2010 3:05 pm
by josh
Yes include() can fail due to permissions being changed among other things. Most functions in PHP can fail like this. The solution is to define a custom error handler. Then you can show the user a custom "oops" page, send an email to alert you of the error, log the error, whatever you want to do.

So lets say some hacker logs in and changes the file permissions around, immediately your page would just be displaying an error, or if you overrode the error handler, they'd see a custom page. You could email the error to yourself, or better yet rely on a 3rd party ping monitoring service that has the capability to monitor your page's content. If the page's content changes, you would get an SMS text message or some other notification, prompting you to login & fix the file permissions & restore your system.

Re: Include redundancy

Posted: Wed Sep 29, 2010 3:40 am
by AdamBlackwell
Thanks for your post Josh - 3rd party error monitoring with sms alerts is a great idea, I will definitely use that one.

Now I understand that if include() stops working it is a signal that (in Johns colourful words :) ) "the server is going down the toilet", maybe the title of this post should have been: How to code for server failure?

Anyway I can imagine that this subject is big enough to write a book on, but surprisingly I can't find very much info about this subject. Does anybody have a good suggestion for further reading on this subject?

Re: Include redundancy

Posted: Wed Sep 29, 2010 4:41 am
by josh
A good program separates its error handling from the main program code. This covers a variety of topics, but mainly exception handling. You should definitively validate inputs. When coding a class think of all the ways you could break it later, and code exceptions for those situations. Install a system like Zend Platform that automatically logs exceptions & errors and allows you to graphically debug them within the IDE (if you can get that part working right with Zend Studio).

Otherwise, just have good error handling (exceptions) and log them, and log all PHP errors. Have a 0 tolerance for PHP errors. Some teams operate with 1,000s of NOTICE errors going to the error_log and brush it off like its no a problem, just don't do that.

Re: Include redundancy

Posted: Wed Sep 29, 2010 5:32 am
by Eran
If you are worried about the uptime and availability of your site, consider installing monitoring tools on your server such a munin / nagios. Those tools monitor the health of different services and take action when something happens (such as Emailing you or restarting the server).

Re: Include redundancy

Posted: Wed Sep 29, 2010 5:50 am
by josh
If the tools is running on the server itself, how can it tell you if network is lost? That is where services like www.siteuptime.com have an advantage. There are even more expensive ones that track the kind of experience & surfing speed the users are getting.

Re: Include redundancy

Posted: Wed Sep 29, 2010 7:45 am
by Eran
Like everything, fault recovery and monitoring has many levels. You can use an external service or multiple servers using heartbeat or similar tools. Pick the solution most relevant to your needs and the amount of availability you need