PHP CookBook Free Open Book

PHP CookBook

Previous Section Next Section

Recipe 8.23 Reading Configuration Variables

8.23.1 Problem

You want to get the value of a PHP configuration setting.

8.23.2 Solution

Use ini_get( ):

// find out the include path:
$include_path = ini_get('include_path');

8.23.3 Discussion

To get all configuration variable values in one step, call ini_get_all( ). It returns the variables in an associative array, and each array element is itself an associative array. The second array has three elements: a global value for the setting, a local value, and an access code:

// put all configuration variables in an associative array
$vars = ini_get_all( );
print_r($vars['include_path']);
Array
(
    [global_value] => .:/usr/local/lib/php/
    [local_value] => .:/usr/local/lib/php/
    [access] => 7
)

The global_value is the value set from the php.ini file; the local_value is adjusted to account for any changes made in the web server's configuration file, any relevant .htaccess files, and the current script. The value of access is a numeric constant representing the places where this value can be altered. Table 8-3 explains the values for access. Note that the name access is a little misleading in this respect, as the setting's value can always be checked, but not adjusted.

Table 8-3. Access values

Value

PHP constant

Meaning

1

PHP_INI_USER

Any script, using ini_set( )

2

PHP_INI_PERDIR

Directory level, using .htaccess

4

PHP_INI_SYSTEM

System level, using php.ini or httpd.conf

7

PHP_INI_ALL

Everywhere: scripts, directories, and the system

A value of 6 means the setting can be changed in both the directory and system level, as 2 + 4 = 6. In practice, there are no variables modifiable only in PHP_INI_USER or PHP_INI_PERDIR, and all variables are modifiable in PHP_INI_SYSTEM, so everything has a value of 4, 6, or 7.

You can also get variables belonging to a specific extension by passing the extension name to ini_get_all( ):

// return just the session module specific variables
$session = ini_get_all('session');

By convention, the variables for an extension are prefixed with the extension name and a period. So, all the session variables begin with session. and all the Java variables begin with java., for example.

Since ini_get( ) returns the current value for a configuration directive, if you want to check the original value from the php.ini file, use get_cfg_var( ):

$original = get_cfg_var('sendmail_from'); // have we changed our address?

The value returned by get_cfg_var( ) is the same as what appears in the global_value element of the array returned by ini_get_all( ).

8.23.4 See Also

Recipe 8.24 on setting configuration variables; documentation on ini_get( ) at http://www.php.net/ini-get, ini_get_all( ) at http://www.php.net/ini-get-all, and get_cfg_var( ) at http://www.php.net/get-cfg-var; a complete list of configuration variables and when they can be modified at http://www.php.net/function.ini-set.php.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Strings
    Chapter 2. Numbers
    Chapter 3. Dates and Times
    Chapter 4. Arrays
    Chapter 5. Variables
    Chapter 6. Functions
    Chapter 7. Classes and Objects
    Chapter 8. Web Basics
    8.1 Introduction
    Recipe 8.2 Setting Cookies
    Recipe 8.3 Reading Cookie Values
    Recipe 8.4 Deleting Cookies
    Recipe 8.5 Redirecting to a Different Location
    Recipe 8.6 Using Session Tracking
    Recipe 8.7 Storing Sessions in a Database
    Recipe 8.8 Detecting Different Browsers
    Recipe 8.9 Building a GET Query String
    Recipe 8.10 Using HTTP Basic Authentication
    Recipe 8.11 Using Cookie Authentication
    Recipe 8.12 Flushing Output to the Browser
    Recipe 8.13 Buffering Output to the Browser
    Recipe 8.14 Compressing Web Output with gzip
    Recipe 8.15 Hiding Error Messages from Users
    Recipe 8.16 Tuning Error Handling
    Recipe 8.17 Using a Custom Error Handler
    Recipe 8.18 Logging Errors
    Recipe 8.19 Eliminating 'headers already sent' Errors
    Recipe 8.20 Logging Debugging Information
    Recipe 8.21 Reading Environment Variables
    Recipe 8.22 Setting Environment Variables
    Recipe 8.23 Reading Configuration Variables
    Recipe 8.24 Setting Configuration Variables
    Recipe 8.25 Communicating Within Apache
    Recipe 8.26 Profiling Code
    Recipe 8.27 Program: Website Account (De)activator
    Recipe 8.28 Program: Abusive User Checker
    Chapter 9. Forms
    Chapter 10. Database Access
    Chapter 11. Web Automation
    Chapter 12. XML
    Chapter 13. Regular Expressions
    Chapter 14. Encryption and Security
    Chapter 15. Graphics
    Chapter 16. Internationalization and Localization
    Chapter 17. Internet Services
    Chapter 18. Files
    Chapter 19. Directories
    Chapter 20. Client-Side PHP
    Chapter 21. PEAR
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele