aeonsky Posted April 10, 2009 Share Posted April 10, 2009 Hello! I'm building a web application based on flatfile system. All the settings come from settings.php - just text in there. SETTINGS.php login_required:0; redirect_enable:1; And I need the fastest and most efficient way to get a specified setting title and its value. Here is what I have. class Settings { public function get_settings_value($title) { if(preg_match("/{$title}:1/", file_get_contents("other/settings.php"))) return true; else return false; } } Can anyone think up of something that may be faster/more efficient than my code? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/ Share on other sites More sharing options...
xtopolis Posted April 10, 2009 Share Posted April 10, 2009 Load and parse the settings page once, store the values in a session variable. Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/#findComment-806020 Share on other sites More sharing options...
aeonsky Posted April 10, 2009 Author Share Posted April 10, 2009 Load and parse the settings page once, store the values in a session variable. Oh that actually will work much better. And as I look through my code now, it seems that file_get_contents() has to be gotten several times during code execution. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/#findComment-806041 Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2009 Share Posted April 10, 2009 Your settings.php file should be php code (with <?php and ?> tags) that either sets variables/array or defined constants. There are two reasons - 1) By making it php code, no one can browse to it and see your settings (browse to your existing file and see what you get), 2) You can simply include/require the file and all the settings will be parsed by php and exist as variables/array or defined constants. Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/#findComment-806043 Share on other sites More sharing options...
aeonsky Posted April 10, 2009 Author Share Posted April 10, 2009 Your settings.php file should be php code (with <?php and ?> tags) that either sets variables/array or defined constants. There are two reasons - 1) By making it php code, no one can browse to it and see your settings (browse to your existing file and see what you get), 2) You can simply include/require the file and all the settings will be parsed by php and exist as variables/array or defined constants. 1) The are in php tags. 2) Forgot to mention. I want an admin panel, so it would probably be hard to change the variables from the admin panel. Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/#findComment-806056 Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2009 Share Posted April 10, 2009 1) Post the actual contents to get the quickest solution, 2) That does not matter, an admin panel can write variables/array or defined constants to a setting file easier than what you are doing now. Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/#findComment-806066 Share on other sites More sharing options...
aeonsky Posted April 10, 2009 Author Share Posted April 10, 2009 Full settings.php <?PHP login_required:1; redirect_enable:1; ?> I'm thinking on how to set all of these values into a session now... 1. Ok, preg_match_all settings.php into $array. 2. Keep exploding $array and setting into a session in a foreach loop. Something like this? preg_match_all("/{$title}:1/", file_get_contents("other/settings.php"), $matches); foreach ($matches as $value) { $data = explode(":", $value); $_SESSION[$data[0]] = $data[1]; //Will this work? } Anyone think of a better way? Thanks for the time. Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/#findComment-806141 Share on other sites More sharing options...
aeonsky Posted April 10, 2009 Author Share Posted April 10, 2009 There were obvious mistakes in my code above... Tested and works... preg_match_all("/[a-z]+:[0-9]/", file_get_contents("other/settings.php"), $matches); foreach ($matches as $value) { foreach($value as $data) { $exp = explode(":", $data); $_SESSION[$exp[0]] = $exp[1]; } } So... 1. Put above in the function. 2. Call the function at the start of the pages that I need to check settings on. 3. Check session data for settings. Any other suggestions? P.S. -> couldn't modify previous post. Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/#findComment-806151 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 the code for settings is kinda wrong. it shud be more like: <?php die('Unauthorized Access'); ?> login_required:0; redirect_enable:1; cuz if put it into the php code section, depending on the php.ini. u can display or log those errors, so no need doing that. and does settings.php need to be in that format? it looks like u are doing manual editing of the file itself. when u can make it dynamic if u make it dynamic, u can use serialize/unserialize, and a webform to update the file. so u can do an include file instead of parsing the file. Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/#findComment-806153 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 This is wut I was talking about a bit earlier. instead of parsing the file, u can include it. <?php function config_load() { if(!file_exists('settings.php')) { $config=array('login_required'=>1,'redirect_enabled'=>1); $temp=serialize($config); $content="<?php\nif(!defined('loader')) die('Unauthorized access');\n\$config=unserialize('{$temp}');\n?>"; file_put_contents('settings.php',$content); } else { define('loader',1); include('settings.php'); } return $config; } $config=config_load(); print_r($config); ?> Kinda some fun code to play with Good luck Note: Because the config array was simple (numerics), I did not need to encode them with special functions, if u do need to do this, u may want to use something like urlencode or similar text based encoders shud work fine Quote Link to comment https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/#findComment-806162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.