English 中文(简体)
Fastest way to store easily editable config data in PHP?
原标题:

What is the fastest way to store config data in PHP so that it is easily changeable (via PHP)? First I thought about having config.php file, but I can t edit it on fly with PHP, at least not very simply? Then I thought about having XML files, but parsing them for each HTTP request is overwhelming. So, I thought about INI files, but then I figured that INI files are restricted to int/string values. In the end, I have come to the conclusion that JSON encoded file is the best:

$config[ database ][ host ] = ...;
$config[ another ][ something ] = ...;
...
json_encode($config);

Since JSON can store arrays, I can create quite complex configurations with it, and it parses faster than INI files.

My question: did I miss something or is there a better way to do this?

最佳回答

Serialize is a better option than JSON for storing PHP variables.

I like to use var_export for saving config file, and using include for loading config info. This makes it easy to save config data progmatically AND makes the data easy to read/write for a person as well:

config.php:

return array(
  var1 =>  value1 ,
  var2 =>  value2 ,
);

test.php:

$config = include  config.php ;
$config[ var2 ]=  value3 ;
file_put_contents( config.php ,  <?php return   . var_export($config, true) .  ; ?> );

Updated config.php now contains the following data:

return array(
  var1 =>  value1 ,
  var2 =>  value3 ,
);
问题回答

You should use serialize as opposed to json_encode:

http://docs.php.net/serialize

The way I store configuration is to put some variables in an external .php file and then when I want to use those files, I say:

<?php include("fileName"); ?>

And that would allow you to share configuration information across many pages. I am not, however, sure that this is the most efficient method, but it seemed to be the easiest to me.

Whilst it s most likely overkill for what you re after, but what I tend to do is store the config data in a database using a PHP class to control the changes to the name/value pairs within. (i.e.: There s no direct DB access from outside this class.)

When a call is made to the PHP config class to change a value, this then writes out a standard PHP include file with all of various values defined on it.

As such, there s no load time performance hit when reading the config data and all config data can be changed within the database via a CMS module.

I am pretty sure you are correct about the int/string values and yes JSON is a way but serialize and unserializing a string will be faster for speed optimization:

This link will help you:

http://docs.php.net/serialize

I use a database with a table called config or settings. The table has two columns:

name [varchar(255)]
value [varchar(255)]

This allows the easy storage of int s, float s, and short strings.

Then I create two functions, GetSetting and SetSetting. These store a row and retrieve a row, respectively. I find that this simplifies the storing of values tremendously.

If you use require instead of include for the config file you will gain a little bit of performance. You can do this if you know that the config file will always be in place, or when you have your own mechanism for checking if it exists.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签