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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.