file_get_contents with a variable

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
computerwiz3491
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 6:17 pm

file_get_contents with a variable

Post by computerwiz3491 »

I am trying to display another page. I also want a variable in the URL.

Code: Select all

$site=file_get_contents("test.php?id=$id");
This gives me Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/q/a/z/qazwart/html/daniel/php/survey2.php on line 44

I also tried

Code: Select all

$page="test.php?id=$id";
$site=file_get_contents("$page");
This gives me the same error. Any ideas?

Thanx.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

can we see the whole code? The error may not be there.
computerwiz3491
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 6:17 pm

Post by computerwiz3491 »

The problem only exists if I have a variable in the file_get_contents.

Code: Select all

file_get_contents("test.php?id=billy");
gives me no problems
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The question is why you want to do what you are trying to do? The function you are using loads the contents of a file into a variable. You are adding URL parameters to a filesystem call -- which will just give you file name errors.

So let us in on the big secret ... what is your goal here?
(#10850)
computerwiz3491
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 6:17 pm

Post by computerwiz3491 »

I am trying to make a page display if certain conditions are met. I want the url of the accual page to stay hidden from the untrained eye. I am using a print statement to display the page. It works fine as long as I don't want to put a variable as the URL
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

you could try this:

Code: Select all

$page="test.php?id=".trim($id); 
$site=file_get_contents($page);
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

why not just

Code: Select all

<?php
     include("test.php?id=$id");
?>
computerwiz3491
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 6:17 pm

Post by computerwiz3491 »

why not just

Code: Select all

<?php
     include("test.php?id=$id");
?>
I don't want to just include the file because test.php is extreamly complex and does different things depending on how it's opened.[/quote]
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

header redirect or combine the scripts to

Code: Select all

if ($conditionmet) {
  //test.php code here
} else {
  //normal code here
}
i don't quite understand the way your doing it but.. (or indeed why that way)
computerwiz3491
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 6:17 pm

Post by computerwiz3491 »

If I use an include() I get the same error when I use a variable.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

try commenting out the line that pulls in the page, see if you still get the error....
Post Reply