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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing vars with include() fails

Status
Not open for further replies.

IMAR

Technical User
Feb 15, 2005
1
US
Hi all,

at this moment I am working on a script that calculates standings of our online racing competition. In order to do that, I have to connect to a database on a remote server.

I have read in the php manual that

When a file is include()ed, the code it contains inherits the variable scope of the line on which the include() occurs. Any variables available at that line in the calling file will be available within the called file. If the include() occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function.

But for some reason, i simply do not get the script to work. I use a form in the local script to determine which standings are to be shown, and the value is stored in $serie_id. This is done in the main scope (no
function used). $serie_id can be 2, 3 or 4.
If $serie_id is not set yet, it takes value 2 as default.

Then I make an include call to the remote script:

include ("
In that remote script I use $serie_id to make a database query. But so far, the query fails because $serie_id is not known. Also, other variables declared in the local script (like an array for points calculation) are not known.

Are there any known exceptions (f.i. specific server settings) that might explain why the variable is not known on the remote server?

Thanks in advance!
 
Yes ... all you said is true.

Can you give us a piece of the code. Cause i didn't understand what's the problem. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Are you trying to have the remote file inherit variables on you server?

Something like:
Your server:
Code:
$myvar = "Hello World";
include("[URL unfurl="true"]http://domain.com/remote_file.php");[/URL]
Remote Server
Code:
echo "$myvar"; 
 //you want this to print Hello World?

If this is what you want, you will not be able accomplish you goal with an include(). When you include the remote file, you get whatever html is generated by the remote file inserted into your script - not the actual code. You may want to integrate some sort of a parameter passing with a query string. I'm not exactly sure what you are trying to do. -gerrygerry
Go To
 
I should mention (if its not too late), you could always establish a spot in the remote file that the vars you need are printed (somewhere in the source code, perhaps commented out??) and the do a remote read and assign these vars... but the printed var location would have to be constant. Example follows:

[YourPage.php]
<?
$fcontents = file (&quot;$myvar = $fcontents[4];
echo &quot;$myvar&quot;;
?>

[RemoteScript.php]
Line 0: <HTML>
Line 1: <BODY>
Line 2: <h1>This is sample text</h1>
Line 3: <!--
Line 4: <? echo &quot;$some_var&quot;; ?>
Line 5: --> -gerrygerry
Go To
 
Look this way it does not inherit too ... cause when you do the file() it executes the file as it was in the http server and so, it echoes the value of the var, in that case is null.

If he wants to pass values to the other script, he MUST pass them by POST,GET,COOKIE or SESSION. In this case GET is the best method.

file(&quot;
I think it's %20 to replace the space char. Well, the best way is to do a rawurlencode.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top