Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Not Working in Included File

Status
Not open for further replies.

WebStuck

Programmer
Apr 12, 2003
79
US
Hi,

I have a web page with the following code:

$page_id = 2;

include(" $_SERVER['SERVER_NAME']. "/comments/comments_form.php");

I need the included file, comments_form.php, to be able to use the $page_id variable but it doesn't seem to be able to for some reason.

Thanks!
Ben
 
nope. because you are including it with http which uses the web server to handle the file request. the webserver will in turn, usually, send the php file via the php cgi or application extension which will return the screen output.

i think you probably want to use the file:// protocol
 
jpadie,

I'm not sure what you mean. The include line is working fine. The only problem is that I can't reference the $page_id variable within the comments_form.php file.

Thanks!
Ben
 
you are unlikely to be receiving the source code the php file if you are accessing it with http. you will receive the parsed output of it.

consider
Code:
<?
echo "hello";
?>

Code:
<?
echo htmlentities(file_get_contents("[URL unfurl="true"]http://".$_SERVER[/URL]['SERVER_NAME']."/remotefile.php"));
?>

you might expect to see the output
Code:
<?
echo "hello";
?>

but, provided the web server has php installed on it, you will get
Code:
hello

does that help explain the difference? there are workarounds to the above problem but bottom line: i recommend grabbing includes by using the file system.
 
I don't think you understand what I am asking.

I have a web page with the following code:

<?php
$page_id = 2;

include(" $_SERVER['SERVER_NAME']. "/comments/comments_form.php");
?>

The comments_form.php contains a php script. When I put echo "Page id is $page_id" within the comments_form.php script, it outputs:

Page id is

It doesn't display:

Page id is 2

like I want

Thanks!
Ben
 
What jpadie means, is that by including the file via the http wrapper, the included file gets run independently of the including script and just outputs its results to the including script.

So in essence comments_form.php is unaware of your page_id declaration in the including script.
PHP Online Manual said:
...
If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Appendix N, List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; [red]the script is actually being run on the remote server and the result is then being included into the local script[/red].
...
Technically you should be getting a: Warning: Undefined variable:... warning when running it.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok...so how do I make comments_form.php aware of my page_id declaration?
 
Include it using the filesystem. Where in relation to the including script is comments_form.php located?

e.g:
include(".\comments\comments_form.php");



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Great! It is working now.

Thanks for all your help!
Ben
 
vacunita said:
Include it using the filesystem

did i not say that? twice? as sleipnir214 once posted
what am I, chopped liver?

jpadie first post said:
i think you probably want to use the file:// protocol

jpadie second post said:
i recommend grabbing includes by using the file system.
 
First of all, I meant to thank everybody for their help by saying thanks all for your help but accidentally typed thanks for all your help, but I still meant thanks to everybody for helping. Unfortunately, I didn't quite understand what you meant and couldn't figure it out until vacunita gave me the sample line.

- Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top