Using GET data with PHP include()
Earlier today I thought I’d edit the title-page wallpaper script, and make it print friendly error messages. Previously it would just die in a very unhelpful way when an error was encountered. It didn’t occur to me before-hand, but my custom error handling script was very antiquated. For one, it had to be called with GET information.
This works well for .htaccess redirects and error documents (which was why I designed it this way a year or so ago), but it is a bad way of calling the error functions from within a running script. For one, PHP won’t allow you to pass GET values in the filename:
include('error.php?code=404'); // will not work
Since I didn’t have the time to overhaul the entire script, I checked for an easy work around. PHP recommends you use URL wrapping instead.
include('http://www.lazymoon.org/error.php?code=404');
But I didn’t like this idea, because if my host disables URL wrapping I’ll be left with broken scripts. Not to mention it takes longer to process.
The easy solution, for those who are interested, is just to set the GET information manually before including the file.
$_GET['code'] = 404;
include('error.php);
This does the job nicely.
Monday, November 12th, 2007 @ 11:43 PM • Next Related Post •
Responses
There are 33 responses for this post.
Comments are open, you can write a comment below.
man, thank you so much!!!
Good one! To logic to make sence : )
Very helpful, thanks!
Thankyou very much =), i was totally owned in this one
Well, it will work too, if you load in the second PHP file the variable.
Example
(main.php):
$code=”404″;
include(“error.php”);
(error.php):
echo “Error Code “.$code;
Thanks for this, very useful little trick
many thanks!
Haha, nice!!! Reading that post was one of those… “DUH! Of course!” moments. Thanks
Hey ! That’s exactly what I was looking for. Thanks a lot!
Err…And if we use a database, I mean if the $_GET variable is dynamic.
easy and useful!!
That… was way easier than I expected. Thanks a bundle.
Great! Just what I needed. Thanks.
Thanks
thank you
Brilliantly simple. Thanks!
“Err…And if we use a database, I mean if the $_GET variable is dynamic”
Grazie anche dall’Italia > Thanks from Italy
This didn’t do the trick for me at all. I set the GET variable manually, then included a file that required the GET variable, or it will spit out an error. Still get the error each time.
Never mind, it did do the trick for me. It was an error with my code. Silly me.
thanks so much!!
I agree with Al, just one of those duh moments. Thank you very much for this helpful post
Haha. I was spending ages trying to do something like this. Thanks for help.
Thanks man! First result on Google, and for good reason, it’s exactly what I needed.
Hello , this helped me Too !!!!
This helped me. Thank you very much. Brilliant and simple.
So logical, but therefore so hard to stuble upon. Thanks!
@ggahnstedt: You can just set the $_GET dynamicly, I also do this myself.
Of course! Thank you!
Thanks man! This is what i needed
Thank you so much, I had the same problem for a long time!!
Awesome man. This is probably the simplest answer EVER to a PHP problem that I have encountered!
Thank you very much!
This is really what i needed.
Thanks alot was having some problems with a script for my site , got it to work now
.