Forum Moderators: coopster

Message Too Old, No Replies

Reference variable in an external include

         

shs_cmcl

9:51 pm on Jan 2, 2009 (gmt 0)

10+ Year Member



I've got a page that has a couple of variables set, like
$category = 'Foo';
$subcategory = 'Bar';

After the variables are set, there's an include for a page on an external site:
include('http://www.example.com/example.php');

On that example.php page, there is a query to a MySQL database, attempting to pull all the data where category = $category and subcategory = $subcategory, but it's not working -- looks like the external include doesn't see the variables that were set on the original page? Is there another or better way to achieve this? I actually have several pages, each with a different set of variables that I'd like to use to call up this one query.

One alternative I've tried is to just set the variables in the url of the include, like this:

include('http://www.example.com/example.php?category=Foo&subcategory=Bar');

which looks like it might almost work, except that many of the variable values have spaces in them, which seems to break the whole thing.

There's no problem with the page reading the external include -- I echoed the query just to be sure allow_external_includes was on, and echoing the query showed that it just wasn't seeing the variables.

mcavic

11:03 pm on Jan 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your intention is to execute example.php on the remote server, and return the output of that script to the local server, right?

Curl [google.com] is a more proper solution, but regardless, the remote script won't automatically have access to the local variables. You'll have to set the variables in the url, as you mentioned. To avoid problems with spaces, you can urlencode() the variables first.

shs_cmcl

2:08 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



Hadn't thought of that (obviously!), thanks.

penders

2:41 pm on Jan 6, 2009 (gmt 0)

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



What if example.php had some other file extension that prevented it from being parsed on the remote server? May be a security issue, but would that work?

eelixduppy

3:27 pm on Jan 6, 2009 (gmt 0)



>> May be a security issue, but would that work?

Yes, it should work but it would big a risk, especially since db credentials may be revealed.

The script doesn't have access to the local variables because you are not getting the PHP code included into your script, but rather the output from the PHP script on the other server. As penders hinted to it is being parsed on the other server first before yours. If you want to include the PHP code into your script then you are going to need to find another way.