Url include для php

include()

The include() statement includes and evaluates the specified file.

The documentation below also applies to require() . The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error . In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. Be warned that parse error in included file doesn’t cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does.

Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is . , current working directory is /www/ , you included include/a.php and there is include «b.php» in that file, b.php is first looked in /www/ and then in /www/include/ . If filename begins with ./ or ../ , it is looked only in include_path relative to the current working directory.

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

Example 16-5. Basic include() example

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function.

Example 16-6. Including within functions

/* vars.php is in the scope of foo() so * * $fruit is NOT available outside of this * * scope. $color is because we declared it * * as global. */ foo(); // A green apple echo «A $color $fruit»; // A green ?>

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes 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 .

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 M 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.

Читайте также:  Strftime function in php

Windows versions of PHP prior to PHP 4.3.0 do not support accessing remote files via this function, even if allow_url_fopen is enabled.

Example 16-7. include() through HTTP

Remote file may be processed at the remote server (depending on the file extension and the fact if the remote server runs PHP or not) but it still has to produce a valid PHP script because it will be processed at the local server. If the file from the remote server should be processed there and outputted only, readfile() is much better function to use. Otherwise, special care should be taken to secure the remote script to produce a valid and desired code.

See also Remote files , fopen() and file() for related information.

Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it’s possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.

Because include() is a special language construct, parentheses are not needed around its argument. Take care when comparing return value.

Example 16-8. Comparing return value of include

Note: In PHP 3, the return may not appear inside a block unless it’s a function block, in which case the return() applies to that function and not the whole file.

Example 16-9. include() and the return() statement

Читайте также:  Javascript object key length

$bar is the value 1 because the include was successful. Notice the difference between the above examples. The first uses return() within the included file while the other does not. If the file can’t be included, FALSE is returned and E_WARNING is issued.

If there are functions defined in the included file, they can be used in the main file independent if they are before return() or after. If the file is included twice, PHP 5 issues fatal error because functions were already declared, while PHP 4 doesn’t complain about functions defined after return() . It is recommended to use include_once() instead of checking if the file was already included and conditionally return inside the included file.

Another way to «include» a PHP file into a variable is to capture the output by using the Output Control Functions with include() . For example:

Example 16-10. Using output buffering to include a PHP file into a string

In order to automatically include files within scripts, see also the auto_prepend_file and auto_append_file configuration options in php.ini .

Note: Because this is a language construct and not a function, it cannot be called using variable functions

See also require() , require_once() , include_once() , get_included_files() , readfile() , virtual() , and include_path .

Источник

Url include для php

The behaviour of these functions is affected by settings in php.ini .

Filesystem and Streams Configuration Options

Name Default Changeable Changelog
allow_url_fopen «1» PHP_INI_SYSTEM
allow_url_include «0» PHP_INI_SYSTEM Deprecated as of PHP 7.4.0.
user_agent NULL PHP_INI_ALL
default_socket_timeout «60» PHP_INI_ALL
from «» PHP_INI_ALL
auto_detect_line_endings «0» PHP_INI_ALL Deprecated as of PHP 8.1.0.
sys_temp_dir «» PHP_INI_SYSTEM

Here’s a short explanation of the configuration directives.

This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.

This option allows the use of URL-aware fopen wrappers with the following functions: include , include_once , require , require_once .

Note:

This setting requires allow_url_fopen to be on.

Define the user agent for PHP to send.

Default timeout (in seconds) for socket based streams. Specifying a negative value means an infinite timeout.

Читайте также:  Php экранирование кавычек в запросе

The email address to be used on unauthenticated FTP connections and as the value of From header for HTTP connections, when using the ftp and http wrappers, respectively.

When turned on, PHP will examine the data read by fgets() and file() to see if it is using Unix, MS-Dos or Macintosh line-ending conventions.

This enables PHP to interoperate with Macintosh systems, but defaults to Off, as there is a very small performance penalty when detecting the EOL conventions for the first line, and also because people using carriage-returns as item separators under Unix systems would experience non-backwards-compatible behaviour.

User Contributed Notes 3 notes

I’m surprised this isn’t mentioned in docs here, but to set these values at runtime use «ini_set()». For example:

ini_set ( «auto_detect_line_endings» , true );

// Now I can invoke fgets() on files that contain silly \r line endings.
?>

If you want to use auto_detect_line_endings, e.g. to recognize carriage return on a Classic Mac file, you must set it before calling fopen. You can then reset it to its original value. E.g,

$original = ini_get(«auto_detect_line_endings»);
ini_set(«auto_detect_line_endings», true);
$handle = fopen($someFile, «r»);
ini_set(«auto_detect_line_endings», $original);
while (($line = fgets($handle)) !== false) echo «$line\n»; // etc
>

Keep in mind also that Mac OS X bash does not handle carriage returns well, so if it seems like your code is not working when testing from the command line, redirect your output to a file and then try looking at that. On my system, doing it directly on the command line only showed the last line (with or without this setting turned on).

Also note that this will not do what you want if you have a file with mixed line endings (!). If you really care about that case, you have to do something else, like run the file through a translation first and then read it.

Please note that although you may try to set default_socket_timeout to something over 20s, you may get tricked by the Linux kernel.

The default value of tcp_syn_retries is set to 5, which will effectively timeout any TCP connection after roughly 20s, no matter what limits you set in PHP higher than this.

The value can be altered by root only, like this:

echo 6 > /proc/sys/net/ipv4/tcp_syn_retries

A value of 6, as above, will give you a timeout up to ~45s.

Источник

Оцените статью