br3nn4n Posted October 13, 2008 Share Posted October 13, 2008 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 https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/ Share on other sites More sharing options...
aximbigfan Posted October 13, 2008 Share Posted October 13, 2008 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 https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-663653 Share on other sites More sharing options...
br3nn4n Posted October 13, 2008 Author Share Posted October 13, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-663668 Share on other sites More sharing options...
aximbigfan Posted October 13, 2008 Share Posted October 13, 2008 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 https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-663671 Share on other sites More sharing options...
br3nn4n Posted October 13, 2008 Author Share Posted October 13, 2008 And file_put_contents saves it? Or not? still trying to figure out how you'd tell it which variable to replace >.< Link to comment https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-663719 Share on other sites More sharing options...
aximbigfan Posted October 13, 2008 Share Posted October 13, 2008 file_put_contents saves it, yes. It will not replace any vars. Instead of $month, and $year, you will use $array['year'], etc. Chris Link to comment https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-664524 Share on other sites More sharing options...
DarkWater Posted October 13, 2008 Share Posted October 13, 2008 They use a database for settings. Link to comment https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-664527 Share on other sites More sharing options...
br3nn4n Posted October 14, 2008 Author Share Posted October 14, 2008 Alright. I see what you mean. I might just use a database, might be easier, you're right. I'm gonna try editing the file first using serialize though! Link to comment https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-664606 Share on other sites More sharing options...
br3nn4n Posted October 15, 2008 Author Share Posted October 15, 2008 Oh, one more thing to help clarify. Serialize only works with arrays? Link to comment https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-666506 Share on other sites More sharing options...
aximbigfan Posted October 19, 2008 Share Posted October 19, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-668912 Share on other sites More sharing options...
br3nn4n Posted October 19, 2008 Author Share Posted October 19, 2008 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 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 https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-668980 Share on other sites More sharing options...
aximbigfan Posted October 20, 2008 Share Posted October 20, 2008 You mean $month = 'june', right? Then you will do something like this $output = serialize($month); $month = serialize($output); Chris Link to comment https://forums.phpfreaks.com/topic/128142-editing-variables-in-external-php-files-please-read-much-simpler-question-than-it-sounds/#findComment-669711 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.