Welcome to WebmasterWorld Guest from 54.205.209.95

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Includes and Variables

   
11:29 am on Apr 26, 2007 (gmt 0)

10+ Year Member



This should be a simple question.

I have this code

$target = 'blah';
include 'include.php';

Now, is it possible to have include.php communicate with $target, if so how?

Thanks

12:02 pm on Apr 26, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



try echoing $target in the included file, it will work, unless it is a function
4:54 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



I echo it but I get nothing back.

No functions, just defining the variable, including the file and thats all, not working.

Thanks

5:01 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



this is my exact code, cut and paste.

Include.php

$lin = mysql_query("SELECT * FROM link order by id asc", $link) or die ("query 1: " . mysql_error());
echo '<li><h2>LinkWork</h2>'
. '<ul>';
while ($li = mysql_fetch_array($lin)) {
if ($current == $li['base'])
{
echo '';
}
else
{
echo '<li><a href="'.$li['web_url'].'" alt="'.$li['alt_tag'].'">'.$li['display'].'</a></li>';

}
}

Including file

<?php
$current = 'bluewidget';
include("include.php");?>

This is on a wordpress blog if that helps.

Thanks

5:25 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



ok, I tested it

I inserted this into my page
$numfive = 5;

I inserted this into my include

$numfour = 4;
$done = $numfour + $numfive;

when the page is called it returns "4"

any ideas?

5:33 pm on Apr 26, 2007 (gmt 0)

5+ Year Member



I've written PHP include files that like this:

<?php include("example.php");?>

but not as you have above. Does that format work?

maybe:

<? include 'http://www.example.com/example.php?foo=1&bar=2';?>

5:50 pm on Apr 26, 2007 (gmt 0)

5+ Year Member



Okay...

jatar_k was saying to do this I think:

<?php
//Including file:

$current = 'bluewidget'; <---your varible

include('include.php?$current');?>

Hmmm...trying to figure it out myself.

5:51 pm on Apr 26, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I was saying this should work

<?php
$current = 'bluewidget';
include("include.php");?>

strange that it doesn't

let me run a little test, give me a minute or two

5:56 pm on Apr 26, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



here are my files - I have short tags on

inctest.php
<?
$current = 'bluewidget';
include("include.php");
?>

include.php
<?
if (isset($current)) echo '<p>current is set';
else echo '<p>current is not set';

if (!empty($current)) echo '<p>current is not empty';
else echo '<p>current is empty';

echo '<p>the value of current is: ',$current;
?>

output

current is set

current is not empty

the value of current is: bluewidget

so it works fine for me, something else must be going on

included code doesn't change scope so you can get at anything which is accessible in the present scope

6:05 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



tested a little more, when called like this.

include("linkwork.php");

it returns the correct result, but I am trying to call this from many different sites, the same file, it only returns the correct result when called at the server its being hosted on.

Thanks

6:19 pm on Apr 26, 2007 (gmt 0)

5+ Year Member



That could be a security feature preventing you from doing that. (at least I know that something like Flash will not allow you to call on a script hosted elsewhere)

I could be completely wrong about this with PHP though.

But if so...perhaps there is a way for the other sites to grant permission by URL.

6:20 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



example.

I have an include file at bluewidget.com, and I want to include it on bluewidget.com, #*$!, pinkwidget.com.

now the variable needed in my file is found on each of these sites.

<?php
$blue_var = 'text';
include 'http://www.bluewidget.com/file.php';
?>

that variable doesn't seem to pass.

When I insert it into my file on bluewidget.com like this

<?php
$blue_var = 'text';
include 'file.php';
?>

It works fine, but that won't work on pinkwidget.com.

I'm so confused.

Thanks

6:21 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



also, jatar could you address capulet_x's comment for me, thank you.
6:27 pm on Apr 26, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



http include is the issue, it includes the output, not the source

[php.net...]

If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Appendix N, List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
6:30 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



uh oh, so what do you recommend jatar?
6:34 pm on Apr 26, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



include it using the GET method as capulet_x mentioned above

maybe:

<? include 'http://www.example.com/example.php?foo=1&bar=2';?>

6:40 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



ok I have updated it, it's working on my site, I will post back and let you know if it works on the others.

Thanks