Include pages with variables in URL

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

MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Include pages with variables in URL

Post by MarkAshley »

Hi all

I need to include a page with variables in the URL. E.g.:

Code: Select all

require('page.php?var1=value');
This doesn't work because it tries to use the whole string 'page.php?var1=value' as the filename. Is there any way to include a page with variables in the address so the variables are GETtable?

Thanks
Mark
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

Never mind, this won't achieve what I want it to anyway :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

With included files, all variables declared before the inclusion are available to the script.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

As an example:

Page1.php

Code: Select all

<?php
$this_var = 'Hello World!';
?>
Page2.php

Code: Select all

<?php
include 'Page1.php';
echo $this_var;
?>
Result

Code: Select all

Hello World!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

actually I think a more relevant example would be:
FileOne.php

Code: Select all

<?php
echo $content;
?>
FileTwo.php

Code: Select all

<?php
$content = 'Hello World!';
include 'FileOne.php';
?>
Result:

Code: Select all

Hello World!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sure, if you want warnings and notices for undefined variables...:wink:
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

I know existing variables will be available to the included/required pages. But I wanted to include the variables with the page, so that after including 'page.php?var=123', the variable $_GET[var] would exist with a value of 123. I don't know if this is possible, but I don't need to do it any more anyway :)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I know you don't need it any more anyway but this let's assume this:

somepage.php?id=50

Code: Select all

include("someotherpage.php");
someotherpage.php

Code: Select all

echo $_GET['id'];
// yields 50
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Everah wrote:Sure, if you want warnings and notices for undefined variables...:wink:
well the point was that he was trying to pass a variable from the page he's working with to a page he's including...

and how do you figure there is an error or a warning? If you're on page two and you include page one... there shouldn't be any errors... :?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The Ninja Space Goat wrote: FileOne.php

Code: Select all

<?php
// and this value is ???
echo $content;
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Everah, I think you're confused.

re-read NSG's post carefully, you'll see it wouldn't give an error or a warning.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, I deserve to be slapped. That was udder-ly (like nipple on a cow udder-ly) stupid of me. Ok slap away... Ninja, you first, then MarkAshley, then Burrito.

/ Man I hate being stupid on a Friday afternoon. This just means all sorts of hell are going to break loose at home when I am stupid like that during the day. :cry:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

eh... slapping probably isn't necessary...


...on the other hand... if you really want me to...
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Code: Select all

$value1 = "SomeText";

include_once("mypage.php?var1=") . '$value1';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The value you give to include() can only be a file location. Nothing else.
Post Reply