Archive for the tag 'PHP'

SBDavid

How to force php files to use CLI

How to force php files to use CLI

To force php files to use CLI (on the assumption your default php version uses suPHP) create an .htaccess file in your public_html directory, with the following:

<FilesMatch “\.php$”>
AddHandler application/x-httpd-php .php
</FilesMatch>

As of version 4.3.0, PHP supports a new SAPI type (Server Application Programming Interface) named CLI which means Command Line Interface. As the name implies, this SAPI type main focus is on developing shell (or desktop as well) applications with PHP. There are quite a few differences between the CLI SAPI and other SAPIs which are explained in this chapter. It’s worth mentioning that CLI and CGI are different SAPI’s although they do share many of the same behaviors.

The list of command line options provided by the PHP binary can be queried anytime by running PHP with the -h switch

More: http://in2.php.net/manual/en/features.commandline.php

SBDavid

Increasing PHP Allowed memory size

Some times we get the following errors Fatal error: Allowed memory size of 123456 bytes exhausted

Php is setup is to limit memory usage per process. If we require more, this limit can be increased.

Edit

/usr/local/lib/php.ini

and set:

memory_limit = 8M ; Maximum amount of memory a script may consume (8MB)

to a higher value, like 20M. Save, exit, then restart apache.

Source: http://directadmin.com/

PHP as a CGI script with suexec

For hosts running PHP as a CGI script with suexec you may be able to put these directives in a php.ini file in your website root directory.

upload_max_filesize = 10M
post_max_size = 20M

Add the below to your .htaccess file in your web root directory.

php_value upload_max_filesize 10M
php_value post_max_size 20M

The PHP documentation states that the memory_limit setting also affects file uploading.
Generally speaking, memory_limit should be larger than post_max_size.

Depending on your host, this can be done in a number of places with the most likely being php.ini or .htaccess depending on your hosting situation.

Add for example:

memory_limit = 16M to your php.ini file (recommended, if you have access)

With root access, you can use the sed util in Linux/Unix based systems, in order to increace the memory for 64M. Don’t forget to properly locate you php.ini file!

sed -i ’s/memory_limit = 16M/memory_limit = 64M/’ /etc/php5/apache2/php.ini
SBDavid

When PHP safe_mode is on

When PHP safe_mode is on

When safe_mode is on, PHP checks to see if the owner of the current script matches the owner of the file to be operated on by a file function or its directory. For example:

-rw-rw-r– 1 buddies buddies 43 Nov 1 19:20 passwd.php
-rw-r–r– 1 root root 1116 Nov 26 18:01 /etc/passwd

Running passwd.php: results in this error when safe mode is enabled:

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/passwd.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 UID checking.

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):

php_admin_value open_basedir /docroot

If you run the same script.php with this open_basedir setting then this is the result:

Warning: open_basedir restriction in effect. File is in wrong directory in /docroot/passwd.php on line 2

You can change the time zone within a php script. Here is a sample code:

With the php tags enter the following

putenv(”TZ=Europe/Amsterdam”);

You can set the timezone in .htaccess as well. Add the line:

SetEnv TZ America/Indianapolis

List of timezones is here: http://us.php.net/manual/en/timezones.php

« Prev - Next »