Re: best way to save program prefs to a file?
| From: | Edward Vermillion | Date: | Tue, 27 Sep 2005 14:48:53 +0000 |
| Subject: | Re: best way to save program prefs to a file? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-223281@lists.php.net to get a copy of this message | ||
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.