01hanstu Posted December 30, 2008 Share Posted December 30, 2008 Hi, Hope you can help. I want to know how i can make my own installation script so that once all of the details are filled in they are saved into a config file and then opened when requested i.e. the hostname, username, password for the sql database ... Help will be appreciated Stuart Quote Link to comment Share on other sites More sharing options...
Absorbator Posted December 30, 2008 Share Posted December 30, 2008 As far as I know, it is not impossible to create a file and then fill it with PHP code, so I think this may help <?php $fileHandle=fopen("config.php", "w"); $stringToWrite="<?php \$param1=$value1; \$param2=$value2; \$param3=$value3; ?>"; fwrite($fileHandle, $stringToWrite); fclose($fileHandle); /*Assuming that your data is - $value1, $value2 and $value3 Of course you may have any variable you want. The slash ("\") states as escaping character */ ?> Then when you have to use this file just include it <?php include "config.php";?> and use the variables you have already set (but here you are using $param not $value) Hope this helps ---------------- Now playing: Timbaland - Way I Are (Feat. Keri Hilson & D.O.E.) via FoxyTunes Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 30, 2008 Share Posted December 30, 2008 You could use JSON or XML or YAML for this. That would probably be simpler than a PHP file. <?php // write $config['hostname'] = 'pterodactyl.com'; $config['username'] = 'iheartdinosaurs'; $config['password'] = 'rawr'; $fp = fopen('config.json', 'w'); fwrite($fp, json_encode($config)); fclose($fp); // read $config = json_decode(file_get_contents('config.json')); print_r($config); ?> Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 30, 2008 Share Posted December 30, 2008 And here is a class that you can use for YAML: http://spyc.sourceforge.net/ YAML is nice because it is a very human readable markup. Several PHP frameworks use this for configuration. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 30, 2008 Share Posted December 30, 2008 Also I just realized in my previous example that it makes more sense to use serialize/unserialize than json_encode/json_decode Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.