Nov
12

Using GET data with PHP include()

Programming PHP

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 •

Responses

There are 33 responses for this post.
Comments are open, you can write a comment below.

cutemonster
February 20th, 2008 @ 05:51 PM

man, thank you so much!!!

Emanuel
March 6th, 2008 @ 06:27 AM

Good one! To logic to make sence : )

April 20th, 2008 @ 02:44 AM

Very helpful, thanks!

April 30th, 2008 @ 06:55 AM

Thankyou very much =), i was totally owned in this one :P

Endres
June 5th, 2008 @ 09:52 PM

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;

June 8th, 2008 @ 11:45 PM

Thanks for this, very useful little trick :-)

k3dt
July 16th, 2008 @ 09:33 PM

many thanks!

July 21st, 2008 @ 07:54 AM

Haha, nice!!! Reading that post was one of those… “DUH! Of course!” moments. Thanks ;)

Jorge Contreras
August 21st, 2008 @ 03:16 AM

Hey ! That’s exactly what I was looking for. Thanks a lot!

Redlion
November 18th, 2008 @ 01:00 AM

Err…And if we use a database, I mean if the $_GET variable is dynamic.

December 24th, 2008 @ 11:17 PM

easy and useful!!

February 25th, 2009 @ 02:32 AM

That… was way easier than I expected. Thanks a bundle. :)

March 8th, 2009 @ 09:22 PM

Great! Just what I needed. Thanks.

March 19th, 2009 @ 02:50 AM

Thanks ;)

April 1st, 2009 @ 01:31 AM

thank you

dani
April 7th, 2009 @ 05:20 AM

Brilliantly simple. Thanks!

April 8th, 2009 @ 09:07 AM

“Err…And if we use a database, I mean if the $_GET variable is dynamic”

D.A.T.A.
April 30th, 2009 @ 06:49 AM

Grazie anche dall’Italia > Thanks from Italy

Steven
June 15th, 2009 @ 08:38 AM

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.

Steven
June 15th, 2009 @ 08:42 AM

Never mind, it did do the trick for me. It was an error with my code. Silly me.

June 21st, 2009 @ 05:04 PM

thanks so much!!

Jonn
July 6th, 2009 @ 04:21 AM

I agree with Al, just one of those duh moments. Thank you very much for this helpful post :)

Jack
July 10th, 2009 @ 01:53 AM

Haha. I was spending ages trying to do something like this. Thanks for help.

Allan
August 20th, 2009 @ 06:58 AM

Thanks man! First result on Google, and for good reason, it’s exactly what I needed.

Jmas
October 7th, 2009 @ 03:45 AM

Hello , this helped me Too !!!!

Sezgin Bayrak
November 4th, 2009 @ 10:04 PM

This helped me. Thank you very much. Brilliant and simple.

Will
November 16th, 2009 @ 06:47 PM

So logical, but therefore so hard to stuble upon. Thanks!

@ggahnstedt: You can just set the $_GET dynamicly, I also do this myself.

raj626
December 13th, 2009 @ 08:30 AM

Of course! Thank you!

Mark
December 23rd, 2009 @ 02:23 AM

Thanks man! This is what i needed

January 30th, 2010 @ 11:06 PM

Thank you so much, I had the same problem for a long time!! :D

bob
February 20th, 2010 @ 07:17 AM

Awesome man. This is probably the simplest answer EVER to a PHP problem that I have encountered!

February 21st, 2010 @ 04:55 AM

Thank you very much!

This is really what i needed.

July 3rd, 2010 @ 12:42 PM

Thanks alot was having some problems with a script for my site , got it to work now :D .

Respond