Safe Mode
PHP's Safe Mode attempts to provide a degree of basic security in a shared environment, where multiple user accounts exist on a PHP-enabled web server.When a web server is running PHP in Safe Mode, some functions are disabled completely, and others are available with limited functionality.
Restrictions Enforced by Safe Mode
Functions that attempt to access the filesystem have restricted functionality in Safe Mode. The web server process runs under the same user ID for all web space accounts and must have the appropriate read or write permission to access a file. This is a requirement of the underlying operating system and has nothing to do with PHP itself.When Safe Mode is enabled and an attempt is made to read or write a local file, PHP checks whether file ownership of the script is the same as that of the target file. If the owner differs, the operation is prohibited.
![]() | Write Permission Although Safe Mode implements measures to prevent you from opening another user's files through PHP, the operating system's file permissions may still allow read or even write access to those files at a lower level. Be aware that a user who has shell access to the web server will be able to read any files that are accessible by the web server and write to any file that has global write permission. |
chdir | move_uploaded_file |
chgrp | parse_ini_file |
chown | rmdir |
copy | rename |
fopen | require |
highlight_file | show_source |
include | symlink |
link | touch |
mkdir | unlink |
![]() | Loadable Modules The dl function is disabled in Safe Mode, regardless of the owner of the extension file. Extensions must be loaded into PHP at startup, using the extension directive in php.ini. |
exec | shell_exec |
passthru | system |
popen |
Enabling Safe Mode
You turn Safe Mode on or off by using the safe_mode directive in php.ini. To activate Safe Mode for all users on a shared web server, you use the following directive:
As you learned in the previous section, functions that access the filesystem perform a check on the owner of the file. By default, the check is performed on the file owner's user ID, but you can relax this to check the owner's group ID (GID) instead by turning on the safe_mode_gid directive.If you have shared library files on your system, you can use the safe_mode_include_dir directive to get a list of locations for which the UID/GID check will not be performed when an include or require statement is encountered.
safe_mode = On
![]() | Include Directories If you want to list more than one location in the safe_mode_include_dir directive, you can separate them using colons on Linux/Unix or semicolons on Windows systemsjust as you do with the include_path setting. |
To provide a location from which the system can be executed, you use the safe_mode_exec_dir directive.To allow programs in /usr/local/php-bin to be executed in Safe Mode, you would use the following directive:
safe_mode_include_dir = /usr/local/include/php
safe_mode_exec_dir = /usr/local/php-bin
![]() | Executables Rather than allow execution of all programs from /usr/bin or some other system location, you should create a new directory and copy or link only selected binaries into it. |
safe_mode_allowed_env_vars = PHP_,TZ