Tuesday, December 02, 2008

php_flag settings when PHP runs as CGI (.htaccess / register_globals / php.ini)

A lot of people got puzzled because .htaccess does not work when PHP is running as CGI under Apache. Searching on the internet for a potential solution does not give an easy way to solve the problem.

The easiest is to create a php.ini file inside the folder where you want to set the flags in and voila.

For example, to set register_globals off, create a php.ini with the following
register_globals = Off 
Upload this file to that folder and try now phpinfo() to see the local settings for register_globals. Yes it is off.
This setting in important to get rid of the warning you got from Joomla or Moodle.

PHP is really an amazing language.

4 comments:

azamharir said...

salam Bilal,

I tried your idea but it didn't work for me. i have php runing as CGI, i use Jooml so i set session.use_cookies to true. but in the same time, i must use set it to false for some other web sites.
i want to use the original php.ini with joomla and the local (with session.use_cookies = 0) with the other web sites.
if i put my local php.ini in a web site folder no thing change, phpinfo()shows that.
please, can you explain more.

Mohamed from morocco.

Bilal Abdul Kader said...

Salam Mohamed,
I am not sure about the settings of your server but it worked on mine.

Andrew said...

Unlike .htaccess files that affect the working directory AND all subdirectories, php.ini files ONLY affect working directories when PHP is running as CGI.

Thus if you have to create a php.ini for every directory you want your PHP settings to apply to.

Andrew said...

Also, I've developed in environments that execute PHP as CGI using suEXEC, which *will* look at .htaccess file, although I'm not sure if you can override php setting from within .htaccess at that point.