Dynamically Generated URL as Include?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maddux
Forum Newbie
Posts: 2
Joined: Fri May 14, 2010 11:44 am

Dynamically Generated URL as Include?

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Dynamically Generated URL as Include?

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Dynamically Generated URL as Include?

Post 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');
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: Dynamically Generated URL as Include?

Post 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!
maddux
Forum Newbie
Posts: 2
Joined: Fri May 14, 2010 11:44 am

Re: Dynamically Generated URL as Include?

Post 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.
Post Reply