Forum Moderators: coopster
Local file (on my server, say [myserver.com...]
//local_file.php
<?php
$var = 'foo';
include 'http://www.anotherserver.com/remote_file.php';
?>
Remote file (on another server, [anotherserver.com...]
//remote_file.php
<?php
echo 'This is a test: ';
echo $var;
?>
This didn't work; loading the local_file.php in a browser showed up just 'This is a test: ', but not 'foo' as well. The PHP on the remote side is being parsed correctly, but it can't see $var from my local file.
Can anyone help, or is it something really obvious?
Cheers,
Alex ...
Bah. So now I know why it doesn't work, does anyone have any idea as to how I can get it to work?
In fact, the remote server isn't remote at all, it's just a web_user directory on my domain (/~web_user/), but I can't access it with a relative path, so.. can anyone help?
Alex ...
$code = file_get_contents('http://path/to/remote/file');
//if I do print $code here, it prints out the non-parsed php tags.
$var = 'foo';
//$code has echo $var in it
print eval($code);
But it still can't see $var. What's going wrong?
[edit]ok, you're on that already! It took a long time to write this postr because I tested it first.[/edit]
//local_file.php
<?php
$var = 'foo';
eval(file_get_contents('htp://www.server.com/remote_file.php'));
?>//remote_file.php
echo 'This is a test: ';
echo $var;
Remember, no PHP tags in the remote file.
The functions work fine, evaluate php in the remote file just fine, but they still can't see $var.
I'm doing this:
$var = 'foo';
print eval_html(file_get_contents('http://path/to/remote'));
function eval_buffer($string) {
ob_start();
eval("$string[2];");
$return = ob_get_contents();
ob_end_clean();
return $return;
}
function eval_print_buffer($string) {
ob_start();
eval("print $string[2];");
$return = ob_get_contents();
ob_end_clean();
return $return;
}
function eval_html($string) {
$string = preg_replace_callback("/(<\?=)(.*?)\?>/si","eval_print_buffer",$string);
return preg_replace_callback("/(<\?php¦<\?)(.*?)\?>/si","eval_buffer",$string);
}
Really go for something easier, like having the remote file not have any php at all, just variable place holders, i.e [var].
//remote file
Foo is [var].
Now I need a preg_replace, but I'd ideally want an array of 'replacement' variables, something like the following:
//remote file
<h2>Foo is [foo] and bar is [bar].</h2>
//local file
//get remote file into variable $code
$a['foo'] = 'bar';
$a['bar'] = 'foo'
echo preg_replace("/\[(\S+)\]/e", $a["\$\\1"], $code);
But that's not working.. what've I done wrong? (it's just showing '<h2>Foo is and bar is .</h2>' Anyone help?
Assuming you know all the variable(placeholder) names in the remote file, then it's really simple with str_replace(). Faster too!
//local file
$content = file_get_contents('...remote_file.php');
$find = array('[var1]', '[var2]');
$replace = array('testing...', '123');
$code = str_replace($find, $replace, $code);
print $code;
Regards
Nice that you got that worked out. I want to put all my globals into an include file! Is that a sign of spaghetti code [webmasterworld.com]?