note 92459 added to function.include
| From: | jsb17 at cornell dot edu | Date: | Sat, 25 Jul 2009 01:28:27 +0000 |
| Subject: | note 92459 added to function.include | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158713@lists.php.net to get a copy of this message | ||
get_include_contents() scope:
Assuming you need to process a nested file right now and display html from that nested file later,
get_include_contents('myfile.php') works great if your nested file myfile.php is
self-sufficient (needs no outside variables).
However, if myfile.php relies on outside variables, using get_include_contents() is risky business,
because you can NOT pass local variables to a script using get_include_contents(). Unfortunately, I
see no absolute best solution for all occasions (please post if you discover otherwise).
Assuming file2.php needs to access $address:
Does NOT work:
file1.php:
<?php
$address = "123 Main Street";
// file2.php can not access $address
$html_content = get_include_contents("file2.php");
...
// Erratic output
echo($html_content);
?>
Also does NOT work:
file1.php:
<?php
$address = "123 Main Street";
// Like include(), looks for literal file name, instead of script with parameters
$html_content = get_include_contents("file2.php?address=" . urlencode($address));
...
// Empty output and no error message
echo($html_content);
?>
Also does NOT work:
file1.php:
<?php
$address = "123 Main Street";
// Save evaluation for later
$html_content = "include(\"file2.php\");";
...
$address="666 Wall Street";
...
// Incorrect output, assuming file2.php expected the Main Street address
echo eval($html_content)
?>
This DOES work (file1.php and file2.php displayed), but is highly discouraged, and usually
impractical with a slew of html form variables or arrays
file1.php:
<?php
$address = "123 Main Street";
// Save evaluation for later
$html_content = get_include_contents("file2.php");
...
$address="666 Wall Street";
...
// Correct output, displays "123 Main Street"
echo eval($html_content)
?>
file2.php
<?php
// Works, but impractical
// Adding a comment such as "// defined in file1.php" is a bad idea,
// because a future programmer may call file2.php from file3.php, etc...
// rendering your comment false
global $address;
echo $address;
?>
This DOES work, and although it appears to be the best option, it is still high risk.
file1.php:
<?php
$address = "123 Main Street";
// Save evaluation for later
$html_content = "include(\"file2.php\");";
...
// Output is correct, assuming a future programmmmer or your future self
// does not alter $address within the "...", as in the example immediately above
echo eval($html_content)
?>
This example DOES work, but requires world access to your directory and a redundant database
connection in file2.php, and is also impractical for a slew of html form variables or arrays
file1.php:
<?php
$address = "123 Main Street";
// Save evaluation for later
$html_content = "include(\"http
://www.mywebsite.com/my_directory/file2.php?submit=" . urlencode($submit) .
"\");";
...
$address = "666 Wall Street";
...
// Correct output, displays "123 Main Street"
echo eval($html_content)
?>
----
Server IP: 208.69.120.35
Probable Submitter: 76.123.9.43
----
Manual Page -- http://www.php.net/manual/en/function.include.php
Edit -- https://master.php.net/note/edit/92459
Del: integrated -- https://master.php.net/note/delete/92459/integrated
Del: useless -- https://master.php.net/note/delete/92459/useless
Del: bad code -- https://master.php.net/note/delete/92459/bad+code
Del: spam -- https://master.php.net/note/delete/92459/spam
Del: non-english -- https://master.php.net/note/delete/92459/non-english
Del: in docs -- https://master.php.net/note/delete/92459/in+docs
Del: other reasons-- https://master.php.net/note/delete/92459
Reject -- https://master.php.net/note/reject/92459
Search -- https://master.php.net/manage/user-notes.php