PHP - How To Make Configuration Settings Available To All Scripts
I have a configuration file, and I use parse_ini_file() to parse it into an array at the initial entry point of my script.
I would like the settings to be available to all downstream scripts/methods/functions. Note that my intent is not to change them outside of the configuration file, and ideally they will be readonly, however, if not, I suppose that is okay and I will just be sure not to modify them.
I've read about dependency injection, and while this mostly makes sense, it seems like a lot of script/troubleshooting for little if any value for this scenario.
I've read that global variables are bad (couples your objects to some "god" object, cannot unit test, difficult to troubleshoot, difficult to work with multiple developers, potential conflicts with other classes, namespace pollution), but maybe not for this case?
Another option is defining a bunch of constants to the appropriate values, but this seems redundant.
Or maybe some static class with all the values?
What would be the best way to make configuration settings available to all PHP scripts, functions, and methods?
Similar TutorialsEvery now and then, I experience some strange behavior and eventually trace it back to parse_ini_file's scanner_mode being applicable to some parameter but not another, and am looking for alternatives. Below are several of my thoughts. How do you store configuration settings? Do you use different approaches based on the application, and if so what criteria governs which you use? Keep with a ini file with parse_ini_file. Obviously, not working for me. An ini file but with a class dedicated to ensuring the data is appropriate. Seems like too much duplicating content which will result in errors. YAML. Don't think I want to. XML. Not as readable. In a database. Maybe, but might be harder to manage. Hardcode an array in PHP. Probably not. JSON. I like the idea, but feel comments are important in a config file, and am considering the following: Add extra valid JSON properties which contain comments. Don't like the idea. Use JavaScript's JSON.stringify. Too much mixing technologies. Add comments to the JSON and then strip them using a 3rd party parser such as https://json5.org/ or a little regex. My main issue is inability to auto-format, but this seems viable. Any other ideas?Thanks!
Greetings. First time poster looking for some PHP help if it can be provided, because I'm confused as hell. First off, I'll explain my situation. I'm the IT technician of a small business tasked with ensuring the business' computers and website are running in top shape. I originally joined as a Data Entry clerk to post products to the business' website, but got promoted when they original IT technician left town to pursue his PhD. Before he left, he taught me a few things to prepare watching over the website as my new duty, including some PHP and MySQL. Now, a few months after he's left, I have the glorious task of trying to move our website to a new host because we've apparently outgrown our original host's capabilities. And being based on PHP with a MySQL database, it's turning out to be OH-so-fun. So far, I've managed to download our entire website from the old host and given it to our new host to put up as a testing site, to ensure everything works before we switch hosts. And for the most part, a lot of it has come together nicely, but there's still work needed to be done. And thus we come to the problem I hope someone here can help with. Our website has two back-end Admin sections, an older one that used to handle everything, and a newer one that's sleeker but still reads off a lot of data from the older one to work properly. Normally, when you access those sections, you need to log in first. Now, in the testing site, when I access those areas, the header of each of those sections now reads <?= $row[title] ?> and if I attempt to log in as normal, it just flashes back to the login page with the fields now cleared of what I typed in. When I asked about this to see what the problem might be, the new host said it might "at least two misconfigurations on the new server relative to the old server". So, I've been trying to Google for some help with PHP Configuration Functions, but I don't know if what I'm reading will help me fix the problem. Would anyone here happen to know what they might mean in this case, because my knowledge of PHP is fairly limited beyond what I was taught to keep the website in order without considering a host change... Thanks to those who help or can direct me to a place that helps! Dear All, Hi Guys, I'm a bit new to php coding, so dont really know what the best way to code this up. I want to make a router configuration page, such as it shows the link status (ip/gatewatway etc) and also have input selections such as to turn on/off wifi / dhcp etc. Ideally I would like the data stored and written to a text file as the php server is running on a linux arm embedded system. At present I have been able to read back from a text file the corresponding values for each of the status information (these are stored on a seperate line), but I'm having difficulty writing and reading the input selection boxes, such as when the dhcp is disabled I need it to write to a file that its been disabled and refresh the page to allow entry of the static ip box and then when the apply button is pressed the value is written to the file. This may be out of my depth, hopefully someone could give me some pointers. I feel that the text file might not be the best way of storing and recalling values but I don't know of another way. Many Thanks Tim hi, i have a configuration file with this content for example: test = "100"; teste2 = "300"; now need change the test from 100 to 300, i just want use something like this: function ( "test" , 300 ); how can i do that? thanks for help Does any know how to set SMTP Mail Configuration using the gmail account? Hi ,
My site gives a blank page when i click on an article due to insufficient memory. I tried making changes to the php.ini file but the changes are not getting reflected.
Please advice if a server restart is needed for this, how do i go about it?
Thanks in advance
Hi, I don't know if this is even possible, but I need to copy configuration of existing server to my localhost (using XAMPP on windows). I only have full output of phpinfo() function from that server (I don't have access to php.ini), and I would like to change my localhost server to behave exactly like remote one.
Is this even possible? Hi, I've been googling for a while now for an example of how to read, edit and write a configuration file. This part was easy to find... <?php $icon_settings = parse_ini_file("test.desktop"); print_r($icon_settings); ?> ...the above code works great, but how do I edit and write back a configuration file like this... Code: [Select] #!/usr/bin/env xdg-open [Desktop Entry] Version=1.0 Name=Firefox Web Browser Comment=Browse the World Wide Web GenericName=Web Browser Exec=firefox %u Terminal=false X-MultipleArgs=false Type=Application Icon=firefox # ... and so on back to the drive? Thanks. Hey Guys, First Post! Yay I have a problemo, I feel really stupid about having to ask this question. But basically I can't get a new line when I echo output in a script I am writing for my website. Here is the code: echo $newsentry; echo '_______ Read More..'; I tried '\n' I tried using " (double quotes) I have used ' (single quotes) I have positioned the Line Feed '\n' at the begining of an output, and at the end. Neither made any difference. I tried using carriage return '\r' too. Nothing. I haven't coded in PHP for a while, well nothing basic (I know that sounds rediculas but it has been mostly Apps etc, Client Scripts blah blah blah, but I haven't had to echo with a new line for a few years. Probably the last website which I wrote with my friend. So I checked the PHP manual, and I am clearly missing something when I am reading it, I understood it to mean (which as you can see from my code, is what I am currently doing), that you could just somehow type: echo "some string and it carries on to the second line"; However as I first thought, this makes no difference. Could this be a configuration on the Box that hosts this Site?? Sorry for sounding so stupid guys, I appreciate your help. -Craig This topic has been moved to PHP Installation & Configuration. http://www.phpfreaks.com/forums/index.php?topic=326567.0 How do I setup my PHP code and server to accept custom domains on my program like tumblr does? http://www.tumblr.com/docs/en/custom_domains I know the domain owner needs to do the A name part but what needs to be done my side? Cheers in advance Where do PHP configuration settings (e.g. Error-Handling Levels) exist? I believe they are in a file somewhere?! Thanks, Debbie what I'm trying to do is to setup a page called settings.php that the admin runs to order to turn option in the web site on or off. I don't know how to do this since I never done this before. My code is below. <link href="mainframe.css" rel="stylesheet" type="text/css"> <?php require_once '../config.php'; $db = new DbConnector(); $db->DbConnector(); if(isset($_GET['submit'])) { $db->query('UPDATE '.SETTING_TABLE.' SET option='.$_POST['banner'].' WHERE setname='''); echo 'Process complete'; } $result = $db->query('SELECT * FROM '.SETTINGS_TABLE); echo '<table border="1">'; echo '<tr>'; echo '<form action="settings.php" methed="post">'; while($row = $db->fetchArray($result)) { echo '<tr>'; echo '<td>'.$row['setname'].'</td><td><select size="1" name="'.$row['setname']'">'; echo '<option value="on">on</option>'; echo '<option value="off">off</option>'; echo '</td>'; echo "</tr>"; } echo '<td><input type="submit" value="Submit" name="submit">'; echo '</td>'; echo "</tr>"; echo "</form>"; echo "</table>"; /* */ ?> <h2>Settings</h2> <a href = "admin.php">Admin Panel</a> Hello, I'm working on a project and at some point ill need to save user specific settings ex: show/hide email etc... configuration file is not the case i thin because there are specific to every user, database will be the choice but what practice(logic) is the best to save these settings? Thank you! I'm kinda new to PHP programming and I'm working on a large project at my university. Currently I'm working on adding as many choices to the Admin account so he could customize the site to his needs. All I wanted to know is what is the customary way in most sites to save these customization settings? Should I just put them in the database? Or maybe save them in a file (XML or some other kind)? Been scouring Google to find some information but didn't get anything useful so hopefully someone here could help me figure this out. This topic has been moved to Linux. http://www.phpfreaks.com/forums/index.php?topic=313175.0 Hi,
I'm not sure if this is possible, however I'm sure somebody will be able to point me in the right direction. Relativley new to php here.
I would like to write a small app that will change my IP/Subnet Mask/Gateway depending on a selection made from a dropdown list. I will have a tota; of 50 different IP settings & drop downs (each drop down will represent a different location)
I have yet to start writing the app, just on paper so far - can this be done?
I have seen examples of batch scripts like the one below that work, would I need to incorporate something like this into my php?
netsh int ipv4 set address name="Local Area Connection" source=static address=10.127.86.25 mask=255.255.255.240 gateway=10.127.86.30This is just a personal project, I'm trying to further my knowledge! Thanks J so I've been putting all of my php scripts to exe to protect some of the code I have written, say I write a simple loop script like below <?php $REPEAT = 1000; $ECHO = "CLUEL3SS"; for($cwb=1; $cwb!=$REPEAT; $cwb++) { echo "#$cwb: $ECHO\n"; } sleep(99999999); ?> Now how can I set the variable $REPEAT when the script is in .exe form and I cannot edit the code I have tried $REPEAT = fgets(STDIN); That doesnt seem to work though when I have 5+ variables to assign and I have an array set like like $Variable[] = 12345799; $Variable[] = 34324555; $Variable[] = 34289789; $Variable[] = 32899090; How can I use a text file to set these options? maybe have the variables assigned in a text file like Code: [Select] ; this is a comment, this will be ignored REPEAT : 1000 ; this is another comment, ignored also... ECHO : CLUEL3SS ; comment Variable : 3423423 Variable : 2903890 Variable : 3948903 Variable : 9823900 How would I have the php script read the text file in that format to set the variables? I have been working on this awhile (the perfect PHP error handling for my coding style). I have tried to set it up as: Code: [Select] <?php error_reporting(E_ALL & E_STRICT & ~(E_NOTICE)); ?> I want this to show me E_ALL errors, E_STRICT errors, and NOT show me E_NOTICE. Is this the right way to set that up, or am I doing something wrong. I haven't used E_STRICT before but I am anxious to see what kind of errors that have it showing since I haven't messed with it before. Thanks again. |