Forum Moderators: coopster

Message Too Old, No Replies

Include File Not Getting Variables From Parent

         

joelwsmith

4:15 pm on Jun 16, 2010 (gmt 0)

10+ Year Member



I've been searching for hours on my problem and have found nothing that works, so this post is my last-ditch effort.

I've got index.php that will be used multiple times (over 100) on the site.

I'm using the request_uri to get the subfolder title of the product, then get a text file by that name. This works perfectly, no problems.

The only code on index.php is:
[size=2]<?php

$uri = $_SERVER['REQUEST_URI'];
list($domain, $subfolder1, $subfolder2, $pagename) = split("/", $uri);
$myFile = "http://www.mygreatsite.com/assets/products/" . $subfolder2 . ".txt";
$fh = fopen($myFile, 'r');
$title = fgets($fh);
$sku = fgets($fh);
$price = fgets($fh);
fclose($fh);

?>

<?php include("http://www.mygreatsite.com/full/path/to/include.php") ?>[/size]


The include file is a template page to show products with php and html, complete from <html> to </html>. It works just fine, everything shows up how it should. However, any variables I have set in the index.php will not pass to the include file.

I've tried sessions and that doesn't seem to work, either.

If I put:
[size=2]<?php echo $title; ?>[/size]


anywhere in my include page, nothing shows up. It is blank.

From everything I've read, it is stated that variables (or strings) should always pass through to include files, whether global or not.

Am I missing something?

I'm wondering if it's my hosting provider, we had some other PHP issues with an Image Resize script only working in site root and not in folders.

penders

4:28 pm on Jun 16, 2010 (gmt 0)

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



Are all your files on the same server? I would assume they are, in which case you shouldn't be referring to your file, or the include using the URL. This is different to using the local server-side path, which I think you should be using in this case.

eg.
/home/userdir/public_html/full/path/to/include.php

joelwsmith

4:36 pm on Jun 16, 2010 (gmt 0)

10+ Year Member



Ha, it works!

Amazing how the simplest things will work.

Thank you, penders!

Matthew1980

4:37 pm on Jun 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there joelwsmith,

Where are you including the file in the index file? Have you tried to echo the $title; var after its set from fgets($fh); just a thought there ;)

if you reference a var in an include:-

<?php
include("afile.php");
//If var is defined in afile.php this will echo its value
echo $var;
?>

like wise doing this should be ok:-
<?php
//template file, $var is defined in index, this should carry through.

echo $var;
?>

If you haven't already, turn the error_reporting(E_ALL); on in the index.php file & check that the var names match, because it could be as simple as a typo ;) and make sure that the file processing & getting is functioning properly..

From what the manual says, the include function can have relative or full URLS, so that shouldn't be an issue.

Sorry I can't be more constructive..

[EDIT:] OOp's took me too long to type, glad your sorted out now ;)

Cheers,

MRb

penders

12:40 pm on Jun 17, 2010 (gmt 0)

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



Just to clarify...

From what the manual says, the include function can have relative or full URLS, so that shouldn't be an issue.


As it happens, this is exactly what the problem was. Note the difference between using a URL (HTTP wrapper) and using a server-side (absolute or relative) path. A URL cannot be relative in this case.

Using a URL in an include is entirely different to using the server-side path - even when on the same server. If you use a URL (HTTP wrapper) then the file is processed entirely at source and what is included is only the output - if any (result of echo or text outside of PHP code blocks etc.) - even with a file that is actually on the same server.

Note that many shared servers do not permit the use of URLs in an include by default.

topr8

1:38 pm on Jun 17, 2010 (gmt 0)

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



i'm not an expert on php, however it seems to me that if you try and include:

<?php include("http://www.mygreatsite.com/full/path/to/include.php") ?>

then surely that file will already have been parsed and there will be no php code in it.
(because you are including a uri from a server not a file from a filesystem)

[oh sorry, this is effectively what the previous poster said! thnx penders]

Readie

6:22 pm on Jun 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php include("http://www.mygreatsite.com/full/path/to/include.php") ?>

Just to note, best solution (in my opinion) to getting an absolute pointer for an include that remains portable is the following:

include $_SERVER['DOCUMENT_ROOT'] . '/path/to/file.php';