Page 1 of 1

Dynamically Generated URL as Include?

Posted: Fri May 14, 2010 11:53 am
by maddux
Is this possible? I am playing around with one of the pages on my site but can't get a dynamic url to work as an a php include

My question is why would <?php include("lines.php"); ?> work fine but not <?php include("lines.php?sport=nfl&period=0"); ?> IF www.website.com/lines.php?sport=nfl&period=0 is a valid url. Is it just because the url is dynamic?

This is the error I am getting
Warning: main(lines.php?sport=nfl&period=0) [function.main]: failed to open stream: No such file or directory

If I can't get this to work my alternative is using an iframe instead which I want to avoid because there will be 3 different iframes on the page and because the url is dynamic the size is always changing the iframe will have to use a scroll bar.

Anyone have a better way to do this?

Re: Dynamically Generated URL as Include?

Posted: Fri May 14, 2010 1:16 pm
by requinix
include() et al. want to include files. Actual, living, breathing files. For all it knows "lines.php?sport=nfl&period=0" could be a file.
'Course, it isn't.

You need to pretend that you're executing that script.

Code: Select all

$_GET["sport"] = "nfl";
$_GET["period"] = 0;
include "lines.php";
With that said, I'm not too sure you want to be doing it this way.

Re: Dynamically Generated URL as Include?

Posted: Fri May 14, 2010 1:46 pm
by Weirdan
tasairis wrote: You need to pretend that you're executing that script.

Code: Select all

$_GET["sport"] = "nfl";
$_GET["period"] = 0;
include "lines.php";
Also you could pretend you're including remote file over http:

Code: Select all

include 'http://www.website.com/lines.php?sport=nfl&period=0';
However, by default remote includes are disabled in PHP since v5.2.0 . In this particular case it seems mere readfile($url) would suffice though:

Code: Select all

readfile('http://www.website.com/lines.php?sport=nfl&period=0');

Re: Dynamically Generated URL as Include?

Posted: Fri May 14, 2010 2:01 pm
by hypedupdawg
If you are including this script into your file, surely you do not even need to use $GET. All you need to do is declare them before the include:

Code: Select all

<?php
$sport = "nfl";
$period = 0;
include ("lines.php");
?>
Then, in the included file, you just need to write an exception to the $_GET system:

Code: Select all

<?php
if(!$sport)
	{
	$sport = $_GET['sport'];
	}
?>
That way, if you already have a defined $sport, it will take that value, but if not, it will look for the value in the URL.

Of course, you would need to validate any sort of input with regex afterwards. Hope this helps!

Re: Dynamically Generated URL as Include?

Posted: Fri May 14, 2010 3:11 pm
by maddux
Thanks for all the replies guys. I tested the first solution that tasairis advised and I got it working without any errors. Problem is if I wanted to add multiple includes on the same page each one after the first one just says loading for every include after the first so I am going to have to figure out why the script is doing that.