The PHP safe mode is an attempt to solve the shared-server security
problem. It is architecturally incorrect to try to solve this
problem at the PHP level, but since the alternatives at the web
server and OS levels aren't very realistic, many people,
especially ISP's, use safe mode for now.
By default, Safe Mode does a UID compare check when
opening files. If you want to relax this to a GID compare,
then turn on safe_mode_gid.
Whether to use UID (FALSE) or
GID (TRUE) checking upon file
UID/GID checks are bypassed when
including files from this directory and its subdirectories (directory
must also be in include_path
or full path must including).
As of PHP 4.2.0, this directive can take a colon (semi-colon on
Windows) separated path in a fashion similar to the
rather than just a single directory.
The restriction specified is actually a prefix, not a directory name.
This means that "safe_mode_include_dir = /dir/incl" also allows
access to "/dir/include" and "/dir/incls" if they exist. When you
want to restrict access to only the specified directory, end with a
slash. For example: "safe_mode_include_dir = /dir/incl/"
If the value of this directive is empty, no files with different
UID/GID can be included in
PHP 4.2.3 and as of PHP 4.3.3. In earlier versions, all files could be
If PHP is used in safe mode, system() and the other
functions executing system programs
refuse to start programs that are not in this directory.
You have to use / as directory separator on all
environments including Windows.
Setting certain environment variables may be a potential security breach.
This directive contains a comma-delimited list of prefixes. In Safe Mode,
the user may only alter environment variables whose names begin with the
prefixes supplied here. By default, users will only be able to set
environment variables that begin with PHP_ (e.g. PHP_FOO=BAR).
If this directive is empty, PHP will let the user modify ANY
This directive contains a comma-delimited list of environment
variables that the end user won't be able to change using
putenv(). These variables will be protected
even if safe_mode_allowed_env_vars is set to allow to change them.
Limit the files that can be opened by PHP to the specified
directory-tree, including the file itself. This directive
is NOT affected by whether Safe Mode is
turned On or Off.
When a script tries to open a file with, for example,
fopen() or gzopen(),
the location of the file is checked. When the file is outside the
specified directory-tree, PHP will refuse to open it. All symbolic
links are resolved, so it's not possible to avoid this restriction
with a symlink.
The special value .
indicates that the working directory of the script will be used as the
base-directory. This is, however, a little dangerous as the working directory
of the script can easily be changed with chdir().
In httpd.conf, open_basedir can be turned off
(e.g. for some virtual hosts)
the same way as
any other configuration directive with "php_admin_value open_basedir
Under Windows, separate the directories with a semicolon. On all
other systems, separate the directories with a colon. As an Apache
module, open_basedir paths from parent directories are now
The restriction specified with open_basedir is actually a
prefix, not a directory name. This means that "open_basedir =
/dir/incl" also allows access to "/dir/include" and
"/dir/incls" if they exist. When you want to restrict access
to only the specified directory, end with a slash. For example:
"open_basedir = /dir/incl/"
Support for multiple directories was added in 3.0.7.
Warning: SAFE MODE Restriction in effect. The script whose uid is 500 is not
allowed to access /etc/passwd owned by uid 0 in /docroot/script.php on line 2
However, there may be environments where a strict UID
check is not appropriate and a relaxed GID check is
sufficient. This is supported by means of the safe_mode_gid switch. Setting it to
On performs the relaxed GID checking,
setting it to Off (the default) performs
If instead of safe_mode, you set an
open_basedir directory then all
file operations will be limited to files under the specified directory.
For example (Apache httpd.conf example):
If you run the same script.php with this
then this is the result:
Warning: open_basedir restriction in effect. File is in wrong directory in
/docroot/script.php on line 2
You can also disable individual functions. Note that the
directive can not be used outside of the php.ini file which means that
you cannot disable functions on a per-virtualhost or per-directory basis
in your httpd.conf file.
If we add this to our php.ini file:
Then we get this output:
Warning: readfile() has been disabled for security reasons in
/docroot/script.php on line 2
These PHP rectrictions are not valid in executed binaries, of course.