Re: best way to save program prefs to a file?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



A.J. Brown wrote:
Are you wanting the preferences to be real-time changeable? For example, user preferences that can be modified then saved? If so, just store them in an array, then serialize the array and save it to a file. Read the file at every page load.

[code]

//save the settings
$user_settings['setting1'] = 'foo';
$user_settings['setting2'] = 'bar';

$fh = fopen('user_settings.dat');
$serialized = serialize($user_settings);
fwrite ($fh, $serialized, strlen($serialized));
fclose($fh);

//reload the settings
$user_settings = unserialize(file_get_contents('user_settings.dat'));


[/code]


Hope this helps.


I may be showing my ignorance here, but why bother to serialize the array? Why not just write it out to a php file, then all you have to do is include the file when you need it and it's ready to go?

<psudocode alert>

$setingsFile = "<?php\n\n";
foreach($userSettings as $key => $val)
{
    $settingsFile .= "$userSettings[$key] = $val\n";
}
$settingsFile .= "\n?>";

$fh = fopen('/path/to/settingsFile.php', 'w');
fwrite($fh, $settingsFile); // with error handling of course...
fclose($fh);

</psudocode>

Then in your script, include '/path/to/settingsFile.php'; and you're ready to use $userSettings and any changes get written back to the file.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux