★ wanayoo — archive 1999 http://www.php.net/manual/function.require.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Control Structures
Quick Reference
Control Structures
* if
* else
* elseif
* Alternative syntax for control structures
* while
* do..while
* for
* foreach
* break
* continue
* switch
* require
* include
Manual: require
View the source code for this pageSearch the site



Previous page
 switch
include 
Next page


require()

The require() statement replaces itself with the specified file, much like the C preprocessor's #include works.

An important note about how this works is that when a file is include()ed or require()ed, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes PHP mode again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

require() is not actually a function in PHP; rather, it is a language construct. It is subject to some different rules than functions are. For instance, require() is not subject to any containing control structures. For another, it does not return any value; attempting to read a return value from a require() call results in a parse error.

Unlike include(), require() will always read in the target file, even if the line it's on never executes. If you want to conditionally include a file, use include(). The conditional statement won't affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.

Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.

This means that you can't put a require() statement inside of a loop structure and expect it to include the contents of a different file on each iteration. To do that, use an include() statement.

  1 
  2 require ('header.inc');
  3      

Please note that both include() and require() actually pull the contents of the target file into the calling script file itself; they do not call the target via HTTP or anything like that. So any variable set in the scope in which the inclusion happens will be available within the included file automatically, since it has effectively become a part of the calling file.

  1 
  2 require ("file.inc?varone=1&vartwo=2"); /* Won't work. */
  3 
  4 $varone = 1;
  5 $vartwo = 2;
  6 require ("file.inc");  /* $varone and $vartwo will be available in file.inc */
  7      

Don't be misled by the fact that you can require or include files via HTTP using the Remote files feature; the above holds true regardless.

In PHP3, it is possible to execute a return statement inside a require()ed file, as long as that statement occurs in the global scope of the require()ed file. It may not occur within any block (meaning inside braces ({}). In PHP4, however, this ability has been discontinued. If you need this functionality, see include().


User Contributed Notes: require


moron@industrial.org
15-Jul-1999 09:58
There seem to be some issues with included code and file paths under safe mode. As far as I can tell, the included code from a subdirectory does not know the relative path anymore and any file commands (i.e. fopen()) will result in: "open_basedir restriction in effect. Unable to verify location of file"


joe@ego-software.com
09-Sep-1999 04:49
Actually according to recent tests I preformed the: <br> <b>require()</b> function <br> works faster than the <br> <b>include()</b> function <br> and it seems to work in every thing I have needed it for take a look at <a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bhttp%3A%2Fwww.dtheatre.com%26quot%3B%26gt%3B&y=1999 Dtheatre.com</a> where I use 12 files to generate the first page one with the global functions and one with the poll and one with news etc.. and require worked for them all.


peter@helpnet.com.au
16-Oct-1999 06:34
Why does require("/include/xx.php3"); fail when used in a page in the home directory? This works: require($DOCUMENT_ROOT . "/include/xx.php3"); The first version seems to work if in a page that is in a subdirectory.


thomas@vcsites.com
25-Oct-1999 10:49
When using require, I suggest making the filename .php3 or something which executes code and NOT .inc. The reason why I say this is because .inc may not be registered on your server as any file-type so the browser will display the text in the window. I experienced the ability to see my mysql database usernames and passwords when the filename was .inc. When I changed it to .php3, the code was executed and the informaiton wasn't compromised.


kevin@fury.com
30-Nov-1999 06:28
In response to Peter, I believe require("/includes/myinclude.php3") fails where require($DOCUMENT_ROOT . "/includes/myinclude.php3") succeeds is because php is running in the unix environment, not the apache environment, meaning that '/includes/myinclude.php3' is a call relative to the root directory of the filesystem. This is a guess, but I wouldn't be surprised.


rj_s@myrealbox.com
09-Dec-1999 08:21
The 'document contains no data' message seems require's way of saying it can't find you document. Make sure your include path is okay and restart apache when you change the include path.


digita1l@hotmail.com
25-Dec-1999 06:34
a major security risk is leaving *.inc files world readable.  In apache you can add the following to your httpd.conf file to deny access to *.inc:

<Files ~ ".inc">  
    Order allow,deny
    Deny from all
</Files>



wachak@chek.com
29-Dec-1999 02:54
The following snipets are getting me in trouble:

&lt;?php

require("input_sweep.php3");

$visit_album_ref = sweep($visit_album_ref);
$visit_password = sweep($visit_password);

...SNIP...

setcookie("cookie_sessid", $visit_sessid, 0, "", $users_global->server); //Line 49
header("Location: http://$users_global->server/Strawman_server/show_album_page.php3?non_cookie_sessid=$visit_sessid&page=0x00"); // Line 50

For reference, here is the complete content of input_sweep.php3:

&lt;?php
function sweep ($input_string) {
   return (ereg_replace('[|<>"]', "", $input_string));
}  
?>

The error result follows: QUOTE Warning: Oops, php3_SetCookie called after header has been sent in /home/httpd/html/Strawman_global/visit_auth.php3 on line 49 Warning: Cannot add more header information - the header was already sent (header information may be added only before any output is generated from the script - check for text or whitespace outside PHP tags, or calls to functions that output text) in /home/httpd/html/Strawman_global/visit_auth.php3 on line 50 UNQUOTE If I comment out the require() and the two calls to sweep(), my troubles are over. WHY? Does require() force header output? As you can infer from the fact that the warnings go away, the snipped portion of the script sends nothing to the browser. This is the only require() statement in the script. Any other toughts you care to share with this rookie are welcome - Am I checking for the right characters in sweep to keep hackers at bay? Do you see an obvious reason why the cookie isn't making it to the page the browser is redirected to? Is there a correct way to "include" definitions of utility functions like sweep without wrecking cookies and headers? Thanx! Wil


wachak@chek.com
02-Jan-2000 11:26
It turns out that the annotations with setcookie() cover this issue extensively. Part of the "complete content" of input_sweep.php3 were 3 forgotten spaces after the closing tag (?>). These invisible killers forced ejection of the headers before returning control to the main script. Wil


bethany.johnson@wcom.com
27-Jan-2000 03:11
There seems to be a problem with using a
require
 within a function. It works fine within the main body of the program, but if I use it within a function, PHP simply stops executing the script. Any suggestions?


jmuzic1@aol.com
16-Feb-2000 11:42
I am trying to include a perl file that has a question mark in the url. I have tried the
$poll_id = 1;
command with no luck either. I want to include it like this.
&lt;? include("/home/musiclai/public_html/cgi-bin/qpoll20.pl?poll_id=1")?>
and I know the reason because the ? in the URL conflicts with the ending ?.


kaan@mindspring.com
29-Feb-2000 09:19
Im trying to have the 'require' tag retrieve another php script from another directory. It does so correctly but the 'required script' refers to files within its directory so I get a fatal error message informing me it was unable to open the referred files. How can I get this to work?


zcampbell@lynx.com
18-Mar-2000 02:52
to kaan: i think maybe require() doesnt understand relative paths (at least from what i've tried). just put the entire path in the require slot (e.g. if the file resides in "/home/httpd/htdocs/", try using require('/home/httpd/htdocs/somedir');) if THAT doesnt work, try parsing the file yourself on the command line and see what if any errors occur zeph :)


php@soffen.com
11-Apr-2000 04:03
A method to set an include path (ala -I in gcc) might be a good addition. That way you can create "include" directories and beable to do something like:
&lt;?php
requirepath("/html_files/includes");
require ("mainheader.inc");
?>
This way you don't have to use require("/html_files/includes/mainheader.inc"); and if your server setup changes you won't be affected as badly.


amackay@gusnet.cx
09-May-2000 03:13
the note to hide include files is good but it is done wrong. the propper way is:
<Files ~ "\.inc$">
  Order allow,deny
  Deny from all
</Files>
otherwise people could not access files named "fooincluded" or "foo.include" or anything with the letters i-n-c in it.


 About Notes


Previous page
 switch
include 
Next page



Site Statistics


Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.