Jump to content

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/153414-faster-version-of-this-code/
Share on other sites

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!

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.

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.

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.

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.

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.

 

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.