Click to See Complete Forum and Search --> : Variables & Include Files


chestertb
06-02-2004, 08:40 AM
Hi all,

I know that I can include a file, and extract variables from it to use in the parent.

However, can I include a file, and use variables from the parent to perform calculations within the included code segment?

Example...

I have a file, test.php as follows;

<html>
<head>
</head>
<body>
<?php
$test = "Yay... it works!";

include('http://www.somedomain.com/test.inc.php');
?>
</body>
</html>

and test.inc.php is as follows;

<?php

print "This works!
And just to prove it, here is the value of ".$test.".
";
?>

Unfortunately, it doesn't read the value of $test into the external file. Is there a way to do that?

CTB

solavar
06-02-2004, 08:54 AM
It will work if you use the relative path, instead of the URL.

So...


// Not this...
include('http://www.somedomain.com/test.inc.php');

//But this, instead...
include('test.inc.php'); // if in same directory

// or this...
include('/fullpath/to/test.inc.php');

chestertb
06-02-2004, 09:01 AM
thanks...

maybe i should have said
include('http://www.someotherdomain.com/test.inc.php');

The segment of code I want to include is an install routine, and will live on my server. The file that calls it lives on the user's server.

I'm trying to design a way to retain at least a tiny bit of control over where the scripts are used.

The idea is that the included script is the mySQL database table building sequence.

I know... pretty thin protection.

Perhaps I might just abandon the whole idea and give the thing away for free.

CTB

solavar
06-02-2004, 09:21 AM
I see.

When a remote user's file wants to 'include' a file that resides on your server via HTTP, the variables lose their scope. However, you can still transfer them via GET, like this


include('http://www.someotherdomain.com/test.inc.php?test=thistest');
//method doesn't work for PHP versions prior to 4.3.0


Obviously, this may not be practicable if you have lots of variables.

A better approach, I feel, is get_file_contents() from the server and integrate the code into the user's script on the fly.

...Give the code for free?
Well, not if you are building what sounds like a saleable program.

Good luck

chestertb
06-02-2004, 05:00 PM
thanks.

i'm not familiar with "get_file_contents()" but i'll check it out.

i appreciate the help.

CTB