Forum Moderators: coopster

Message Too Old, No Replies

Variables in remotely include()'d file

Remote file can't use variables in local file.

         

Warboss Alex

1:56 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Right, bear with me, this is the first time I've done this and haven't used PHP for a few months, sooo..

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 ...

Warboss Alex

2:10 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Waiiiiiiiiit a minute. If I'm using the http:// protocol, that means the file would be evaluated on the remote server, and THEN sent back to me, right?

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 ...

coopster

2:17 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Correct on the parsing. Have you looked through the PHP Filesystem Functions [php.net] to see which might be your best option? fopen() perhaps? file_get_contents()? file()?

Warboss Alex

2:31 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



I guess I'd have to use one of the file functions, and then eval() the code, huh?

Warboss Alex

2:50 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Well, using file_get_contents() and eval() I can run php code from the remote file, but the remote code still can't see my local file's variable. That seems strange to me, or am I missing something more?

$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?

Birdman

2:54 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This seems like a logical time to use eval() [php.net].

[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.

Warboss Alex

3:09 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Well, that worked, as I expected it would, Birdman. The problem however, is this. The remote file is supposedly a mixture of html and php (basically <?=$var?> tags), and I'm using the eval_html functions found on php.net on the eval function page.

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);
}

Warboss Alex

3:14 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



There's actually no problem with the remote file; I tried this:

$var = 'foo';
print eval_html('test: <?php echo "testing: ".$var;?>');
//include eval functions

And the output is:

test: testing:

So, errrr.. What's going wrong? Something in the eval function's not right.

Warboss Alex

6:07 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Okay, the problem is that $var's not in the right scope for the function. Duh. So I either declare it global, or...

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?

Birdman

7:04 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that method should work fine. I was assuming that you didn't have access to the remote file.

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

Warboss Alex

7:17 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Faster with str_replace()? I thought preg_replace() outperformed it every time?

Thanks!

grandpa

8:26 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Reading through to msg 8, I was considering the variable scope. In the same situation my solution was to ensure each page contained the same standard set of global variables. Any variables in any remote functions wouldn't be seen otherwise.

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]?