PHP Web Fundamentals Setting Environment Variables - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Web Fundamentals Setting Environment Variables - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Wednesday, May 29, 2019

PHP Web Fundamentals Setting Environment Variables

PHP Web Fundamentals



Setting Environment Variables

Problem

You want to set an environment variable in a script or in your server configuration. Setting environment variables in your server configuration on a host-by-host basis allows you to configure virtual hosts differently.

Solution

To set an environment variable in a script, use putenv():

          putenv('ORACLE_SID=ORACLE'); // configure oci extension

To set an environment variable in your Apache httpd.conf file, use SetEnv:

          SetEnv DATABASE_PASSWORD password

Variables set in httpd.conf show up in the PHP superglobal array $_SERVER, not via getenv( ) or $_ENV.

Discussion

An advantage of setting variables in httpd.conf is that you can set more restrictive read permissions on it than on your PHP scripts. Because PHP files need to be readable by the web server process, this generally allows other users on the system to view them. By storing passwords in httpd.conf, you can avoid placing a password in a publicly available file. Also, if you have multiple hostnames that map to the same document root, you can configure your scripts to behave differently based on the hostnames.

For example, you could have members.example.com and guests.example.com. The members version requires authentication and allows users additional access. The guests version provides a restricted set of options, but without authentication. 

Example   Adjusting behavior based on an environment variable

           $version = (isset($_SERVER['SITE_VERSION']) ? $_SERVER['SITE_VERSION'] :                                     'guest');

           // redirect to http://guest.example.com, if user fails to sign in correctly
           if ('members' == $version) {
                if (!authenticate_user($_POST['username'], $_POST['password'])) {
                     header('Location: http://guest.example.com/');
                     exit;
                }
           }
           include_once "${version}_header"; // load custom header



No comments:

Post a Comment

Post Top Ad