Showing posts with label PHP ini_get. Show all posts
Showing posts with label PHP ini_get. Show all posts

Thursday, 20 September 2018

PHP Quick Tip: Check if URL aware fopen wrappers are enabled

PHP supports opening remote URLs (e.g. http://www.example.com/something.htmnl) using the fopen and similar functions. It is possible to disable this function in the PHP configuration so this quick tip shows how to check if it is possible to open remote URLs.

The configuration option

The configuration option that enables or disables opening remote URLs is "allow_url_fopen" and will appear in the configuration file like this:
allow_url_fopen = On
Note that this setting can only be set in the main php.ini configuration file so if your hosting provider has disabled it then you will be able to open remote URLs with fopen etc.

Test if it is enabled

To test if opening remote URLs is enabled do this:
if( ini_get('allow_url_fopen') ) {
    // it's enabled, so do something
}
else {
    // it's not enabled, so do something else
}
If opening remote URLs is not enabled, then you will need to enable this option in the main php.ini configuration or see if your hosting provider will do this for you. Otherwise you will need to use CURL to download files, assuming that CURL is available.

Related posts:

Wednesday, 12 September 2018

PHP's ini_get_all function

PHP has a large number of configuration options which can be set in the php.ini file, Apache virtualhost settings, .htaccess and with the ini_set function. This post looks at the ini_get_all function which returns an array containing all the possible configuration options, what their global and local script value are and where they can be modified.

Sample code:

$options = ini_get_all();
print_r($options);

Example output:

Array
(
    [allow_call_time_pass_reference] => Array
        (
            [global_value] => 1
            [local_value] => 1
            [access] => 6
        )

    [allow_url_fopen] => Array
        (
            [global_value] => 1
            [local_value] => 1
            [access] => 4
        )

    ... more configuration options ...

    [zlib.output_handler] => Array
        (
            [global_value] => 
            [local_value] => 
            [access] => 7
        )

)

Accessing the values

The associative array that is returned contains a key name which is the configuration option name with a sub-array containing the keys 'global_value', 'local_value' and 'access'. If you wanted to find out the local value for 'allow_url_fopen' for example, you could do something like this:
if($options['allow_url_fopen']['local_value'] == 1) {
    // do something
}
else {
    // do something else
}
Of course, you could also do the above using ini_get like this if all you needed was the local value:
if(ini_get('allow_url_fopen') == 1) {
    // do something
}
else {
    // do something else
}

Global value vs local value

The global_value contains the value that is set in the php.ini file. The local_value contains the modified value (if modified) in the <virtualhost>, .htaccess file or ini_set().

Access

Access uses the following PHP constants:
PHP_INI_USER1Entry can be set in user scripts
PHP_INI_PERDIR2Entry can be set in php.ini, .htaccess or httpd.conf
PHP_INI_SYSTEM4Entry can be set in php.ini or httpd.conf
PHP_INI_ALL7Entry can be set anywhere
The value can also be 6 i.e. PHP_INI_PERDIR + PHP_INI_SYSTEM.

Related posts:

Saturday, 4 October 2014

feof in PHP

feof — Tests for end-of-file on a file pointer
Syntax:

bool feof ( resource $handle )
Tests for end-of-file on a file pointer.

Parameters:

handle
The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).

Return values: Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE.



Warning
If a connection opened by fsockopen() wasn't closed by the server, feof() will hang. To workaround this, see below example:
Example #1 Handling timeouts with feof()
<?php
function safe_feof($fp, &$start = NULL) {
 $start = microtime(true);

 return feof($fp);
}

/* Assuming $fp is previously opened by fsockopen() */

$start = NULL;
$timeout = ini_get('default_socket_timeout');

while(!safe_feof($fp, $start) && (microtime(true) - $start) < $timeout)
{
 /* Handle */
}
?>
Warning
If the passed file pointer is not valid you may get an infinite loop, because feof() fails to return TRUE.
Example #2 feof() example with an invalid file pointer
<?php
// if file can not be read or doesn't exist fopen function returns FALSE
$file = @fopen("no_such_file", "r");

// FALSE from fopen will issue warning and result in infinite loop here
while (!feof($file)) {
}

fclose($file);
?>