PHP can I use include to add code to a php script?
When I try using include to add php code to开发者_开发问答 a sendmail script called from a form it doesn't pull in the code like I would expect.
Is there another method I should use to add code to a script?
Thanks in advance.
Besides
include( 'myFile.php' );
you might use
require( 'myFile.php' );
which fails, if the respective file doesn't exist.
In general, I'd propose to use
require_once( 'myFile.php' );
which - even if executed several times - loads the file only once.
Beside these function, give
file_get_contents( 'file.ext' );
a try or maybe
file_get_contents( dirname( __FILE__ ) . '/../file.ext' );
to read relative to the directory of the calling script.
Finally, note that
eval( $someStringVariable );
may be used to make PHP evaluate $someStringVariable as if its contents is PHP source-code.
If all this isn't what you are in search for, please provide more details.
Before you include the form-handling php code, include a file with ONLY the following:
<?php
print '<pre>FILE WAS INCLUDED</pre>';
?>
Name it "test_php_file.php". Then at the top of the file that the form posts to, include the above file like so:
<?php include('test_php_file.php'); ?>
<html>
<head>
...
</html>
If your page prints out "FILE WAS INCLUDED" at the top of the page (assuming Apache parsed it first), then you are good to include the real php code in the same manner. Remember that file inclusion works similarly to inline image rendering: the string you pass include is the filename as well as the relative path. Keep the php file within the same directory as the file that includes it so that way you don't have to worry about the path:
forms/
form.html
test_php_file.php
real_php_file.php
精彩评论