You are here: Re: [PHP] best way to save program prefs to a file? « PHP « IT news, forums, messages
Re: [PHP] best way to save program prefs to a file?

Posted by Edward Vermillion on 09/27/05 17:48

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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация