Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Make your PHP code portable

Rated 4.21 (Ratings: 7) (Add your rating)

Log in to add a comment
(9 comments so far)

Want more?

 
Picture of Martin Tsachev

Martin Tsachev

Member info | Full bio

User since: June 26, 2001

Last login: March 29, 2006

Articles written: 6

There's a lot of PHP code available for free on the net but also not all of it can run on your system as it is. This article shows you how to distribute a setup required by your program to run.

Why should I care

People are different and they use different PHP configurations so if you want to run a script on your system you have two choices:

  • set your global configuration to something that allows the code to run
  • set a configuration specific to the program

If you go for the first one you're in trouble if you try to run two programs which require completely different setups. So the only choice seems to be the second one.

Well then how do I do this

There are four different ways to set a PHP configuration option:

  • through php.ini
  • through httpd.conf
  • through a .htaccess file
  • or directly in PHP code

The first and second ones are only available to the system administrator of a website so you generally have to avoid them. Yes you may have access to those files but think about the other people that may be using the code - do they have access to those files? Usually not.

The third method — via a .htaccess file is available only on Apache (and maybe on the NCSA httpd, I'm not sure about that) but since Apache is the most popular webserver that is usually not a problem. The other thing that you should know about .htaccess files is that they may be disabled by the webmaster. If this is the case your only chance is to use a globally included PHP file which configures the system.

Setting the options with PHP code is the most portable way to configure a system. The only problem is the accessible options are less than available to a .htaccess file.

I'll go for a .htaccess file

The syntax for setting an option is:

php_value name value
php_flag name on or off

Example:

php_flag register_globals off
php_value arg_separator.output &

The example turns off register_globals and sets the value of arg_separator.output to & which is preferred rather than the default &.

Note: you can also set boolean options with the php_value directive, the string will be converted to boolean before assignment.

I'll go for PHP code

There's only one function for setting an option and it is ini_set() it is also aliased as ini_alter(). There is also a function that restores the original value and it is ini_restore().

How to use these:

ini_set('display_errors', false);
ini_set('arg_separator.input', ';');
...
ini_restore('display_errors');

Note: in the first call to ini_set() you can also use any type of variable it will be converted to boolean before setting the option.

Very nice but I don't know my configuration

I've got something for you too. This is a script that will detect common settings that may prevent your code from executing properly on a different system.

<?php

	define('boolean', 1);
	define('string', 2);
	define('integer', 3);

	$php_ini_all = Array(
		Array(string, 'arg_separator.input'),
		Array(string, 'arg_separator.output'),
		Array(boolean, 'display_errors'),
		Array(boolean, 'display_startup_errors'),
		Array(boolean, 'magic_quotes_runtime'),
		Array(integer, 'error_reporting'),
		Array(string, 'variables_order'),
		Array(string, 'gpc_order')
	);
		
	$php_ini_perdir = Array(
		Array(boolean, 'asp_tags'),
		Array(boolean, 'magic_quotes_gpc'),
		Array(string, 'output_buffering'),
		Array(boolean, 'register_globals'),
		Array(boolean, 'short_open_tag')
	);
?>

<?php
	function display_php_conf() {
		foreach ( $GLOBALS['php_ini_all'] as $option ) {
			$value = ini_get($option[1]);
			echo "ini_set('$option[1]', '$value');\n";
		}
	}

	function display_htaccess_conf() {
		global $php_ini_all, $php_ini_perdir;

		foreach ( array_merge($php_ini_all, $php_ini_perdir) as $option ) {
			$value = ini_get($option[1]);
			
			if ( $option[0] == boolean ) {
				$value = $value ? 'on' : 'off';
				$directive = 'php_flag';
			} else {
				$directive = 'php_value';
			}

			echo "$directive $option[1] $value\n";
		}
	}
?>

First we define these common options in two arrays for the different access levels, the first one for options available to both PHP code and .htaccess files, and the second only for .htaccess files. You may need to modify that in case you use non-default settings for other options too.

The two function display_php_conf() and display_htaccess_conf() print the PHP code and .htaccess directives for your config respectively.

Show me the config

<?php

	header('Content-type: text/plain');

	echo "PHP settings:\n";
	display_php_conf();

	echo "\n.htaccess settings:\n";
	display_htaccess_conf();
?>

This will just call the functions defined above so you can easily copy & paste it to a file or pipe it if you are using the CGI version of PHP.

Example output from the script

PHP settings:
ini_set('arg_separator.input', ';&');
ini_set('arg_separator.output', ';');
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
ini_set('magic_quotes_runtime', '');
ini_set('error_reporting', '2047');
ini_set('variables_order', 'GPCS');
ini_set('gpc_order', 'GPC');

.htaccess settings:
php_value arg_separator.input ;&
php_value arg_separator.output ;
php_flag display_errors on
php_flag display_startup_errors on
php_flag magic_quotes_runtime off
php_value error_reporting 2047
php_value variables_order GPCS
php_value gpc_order GPC
php_flag asp_tags off
php_flag magic_quotes_gpc off
php_value output_buffering 4096
php_flag register_globals off
php_flag short_open_tag off

Some general considerations

The above code didn't mention track_vars even a single time because as of PHP 4.0.3 it is always on. The example output was run on PHP 4.2.1 and ini_get() returns FALSE for track_vars as if it does not even exist. If you have an earlier version of PHP it is recommended to set it to true and disable register_globals for security and performance issues.

If you want to find even more information you can always count on the PHP manual.

Martin Tsachev started using computers in 1992, programming Basic and has since then developed a great passion for them.

Nowadays he runs mtdev - a web site with highlight on PHP.

That was a good insight

Submitted by ghurtado on May 28, 2002 - 11:48.

Good article. I would suggest a few editorial edits (grammar and literary style) to make it easier to read. One of the first headings is "Well then how do I do this" and it should have been written "Well, then how do I do this?".

login or register to post comments

Sorry ghurtado

Submitted by Martin Tsachev on May 29, 2002 - 06:07.

I'm sorry I'm not that good on literary style. If you think it's hard to understand can you identify a particular section or was it the whole article you found difficult to understand( or just read)?

login or register to post comments

No, not at all

Submitted by ghurtado on May 29, 2002 - 17:44.

Shaggy, it wasn't all that hard to understand, just a couple of details here and there that could have improved it. I didn't mean to critize your style at all, but I would pay a little more attention to using commas. A good rule of thumb is that, whenever you pause for a second when reading a sentence out loud, you should probably include a comma. Like in "If this is the case your only chance is to use a globally included PHP file which configures the system", I would have added "If this is the case, your only chance is to use a globally included PHP file which configures the system". Other than that, it is mostly well written.

login or register to post comments

hmmm...

Submitted by jesteruk on May 30, 2002 - 15:56.

ghurtado, Shaggy is bulgarian, I don't think it's fair to pull him up for his grammar, the tutorial was very well-written if you take into account the fact that English isn't his first language. I'm sure he'd be able to do the same thing to you if you wrote an article in Bulgarian. Cut him a little slack.

I enjoyed the article Shaggy, very nice, keep up the good work.

-J

login or register to post comments

Keep one thing in mind

Submitted by ghurtado on May 30, 2002 - 18:01.

Like I stated, it wasn't my intention tu put him down for his grammar at all, I just gave him a couple of suggestions, and I don't think he took me the wrong way. English isn't my first language either (I am from Spain, born and raised, and I only moved to the US less than 4 years ago), which is why I would appreciate someone else doing the same for me. He didn't seem to take offense, but if anyone thinks I was rude in pointing that out, I apologize.

login or register to post comments

That's ok

Submitted by jesteruk on May 30, 2002 - 20:35.

I didn't think you paticularly rude, I just found your comment rather pedantic. Shaggy spent time writing a nice article, which wasn't supposed to teach you correct English, or be a good example of English, it's the technicalities; the content; the recommendations that are important. Not whether or not he missed a few commas or constructed a few sentences badly.

I felt picking up on his English was irrelevant and over-critical, is all. Sometimes authors are so pre-occupied with being "correct" and "proper", in the end their article ends up so long, boring and long-winded I just hit the "x" to get rid of it.

-J

login or register to post comments

grammar

Submitted by aardvark on May 30, 2002 - 20:49.

It's worth noting that editing for grammar and punctuation is also handled by the evolt.org admin team / editors / whatever we're called.

You should blame me and the rest of us for letting it go without the edits, not the author.

login or register to post comments

re: grammar

Submitted by garrett on May 31, 2002 - 02:59.

Yeah... my bad. Aard's making me stand in the corner flagellating myself with O'Reilly books.

G.

login or register to post comments

re: grammar

Submitted by Martin Tsachev on May 31, 2002 - 04:49.

OK, there's something I missed there's something evol.org editors missed but the point of the article is to teach you PHP not grammar. Remember also they're not getting paid for that, and after all only my name is displayed, they're not getting even fame for what they do. Anyway it's nice to have your article published the next day after you posted it, rather than wait for a month or so.

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.