Jump to content

Editing variables in external PHP files? Please read, MUCH simpler question than it sounds!


br3nn4n

Recommended Posts

I have an admin page that works great. I built it myself, it's a custom CMS type thing.

 

All content displayed is decided upon off of two variables, $month and $year. It grabs data from the database depending on what those two variables say. Those variables are kept in a PHP file, named say, config.inc.php .

 

The problem? Everytime I want to change the site content I have to manually edit config.ing.php! I want to be able to change those variables through my admin page.

 

Now, here's my question. Do people do this? Because I know programmers write setup pages and whole big admin panels for sites, to enable features and turn on CAPTCHAs, etc. But how?

 

Because the only way of doing this seems to be to regex the file to FIND the variable. I can do that, but it doesn't seem like that's how people do this.

 

You know what I mean? If you don't please just let me know. I can clarify. This has been bugging me for a while now.

 

Thanks! :)

You mean you want to have a settings file?

 

What I do is serialize an array of the settings, then write the serialized array top a file.

 

To read the settings, I just file_get_content's it, the unserialize it.

 

Settings can be changed with something liek this:

$config_array[$item] = $value;

 

Then serialize and write the new array.

 

Chris

Yes, a config file! :) Alright, I think I get it. I only learned of serialize under 5 mins ago, so maybe that's my problem! Haha

 

So if I have a file with 2 variables, $month and $year. You're saying, get_file_contents on that, serialize it, (search for and replace the variable I want to change?), unserialize and save?

 

As you see, I still don't get how I'd access the specific variable and change its value, the right way. Sorry I learn fast but I'm still working on fully understanding how to use serialize :P

No,

 

The serialization will be in a special format, when it is stored in the file. You need ot create some inital settings, serialize them, and store them

 

Here is an example of making a setting:

 

$array = unserialize(file_get_contents('file.img'));

$array['month'] = 1234;
$array['year']   = 2424;

file_put_contents(serialize($array));

//READ
$array = unserialize(file_get_contents('file.img'));
echo $array['month'];
echo $array['year'];

Chris

 

No, you can serialize variables, arrays, objects, etc.

 

when what ever it is is unserialize is will be EXACTLY like it was before it went in.

 

for example (int)42

will come out as (int)42

 

Chris

Alright, so then if I serialize:

 

$month=june

 

how do I change that variable?

Would it just be:

 

if (isset($_POST['newmonth'])) {
$month=$_POST['newmonth'];
}
//serialize, file_put_contents..

?

 

Seems too easy :P

 

I have a working version right now, works perfectly, but uses an array. I'm more just interested in learning what I can and can't do since I'm not yet a PHP pro :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.