homepage Welcome to WebmasterWorld Guest from 184.73.104.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Includes and Variables
dkin

10+ Year Member



 
Msg#: 3322333 posted 11:29 am on Apr 26, 2007 (gmt 0)

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

 

jatar_k

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



 
Msg#: 3322333 posted 12:02 pm on Apr 26, 2007 (gmt 0)

try echoing $target in the included file, it will work, unless it is a function

dkin

10+ Year Member



 
Msg#: 3322333 posted 4:54 pm on Apr 26, 2007 (gmt 0)

I echo it but I get nothing back.

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

Thanks

dkin

10+ Year Member



 
Msg#: 3322333 posted 5:01 pm on Apr 26, 2007 (gmt 0)

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

dkin

10+ Year Member



 
Msg#: 3322333 posted 5:25 pm on Apr 26, 2007 (gmt 0)

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?

capulet_x

5+ Year Member



 
Msg#: 3322333 posted 5:33 pm on Apr 26, 2007 (gmt 0)

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';?>

capulet_x

5+ Year Member



 
Msg#: 3322333 posted 5:50 pm on Apr 26, 2007 (gmt 0)

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.

jatar_k

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



 
Msg#: 3322333 posted 5:51 pm on Apr 26, 2007 (gmt 0)

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

jatar_k

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



 
Msg#: 3322333 posted 5:56 pm on Apr 26, 2007 (gmt 0)

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

dkin

10+ Year Member



 
Msg#: 3322333 posted 6:05 pm on Apr 26, 2007 (gmt 0)

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

capulet_x

5+ Year Member



 
Msg#: 3322333 posted 6:19 pm on Apr 26, 2007 (gmt 0)

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.

dkin

10+ Year Member



 
Msg#: 3322333 posted 6:20 pm on Apr 26, 2007 (gmt 0)

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

dkin

10+ Year Member



 
Msg#: 3322333 posted 6:21 pm on Apr 26, 2007 (gmt 0)

also, jatar could you address capulet_x's comment for me, thank you.

jatar_k

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



 
Msg#: 3322333 posted 6:27 pm on Apr 26, 2007 (gmt 0)

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.

dkin

10+ Year Member



 
Msg#: 3322333 posted 6:30 pm on Apr 26, 2007 (gmt 0)

uh oh, so what do you recommend jatar?

jatar_k

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



 
Msg#: 3322333 posted 6:34 pm on Apr 26, 2007 (gmt 0)

include it using the GET method as capulet_x mentioned above

maybe:

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


dkin

10+ Year Member



 
Msg#: 3322333 posted 6:40 pm on Apr 26, 2007 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved