The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - PHP TO CREATE A CONFIG or properties file
Discuss PHP TO CREATE A CONFIG or properties file in the PHP Development forum on Dev Shed. PHP TO CREATE A CONFIG or properties file PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 22nd, 2013, 05:01 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 1
Time spent in forums: 3 m 25 sec
Reputation Power: 0
|
|
|
PHP-General - PHP TO CREATE A CONFIG or properties file
Hi All,
I am new to PHP , i am in need of a PHP program which can accept input through form and write that into a config or properties file as key value pair
THanks in advance
Regards,
Sandeep
|

January 22nd, 2013, 07:54 AM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
And what have you tried so far? We don't do free work here, this is a help board.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

January 22nd, 2013, 09:08 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 313
  
Time spent in forums: 3 Days 15 h 4 m 26 sec
Reputation Power: 5
|
|
|
Does this have to be a physical file? Have you worked with MySQL before? Perhaps a database may also be an option for you?
|

January 22nd, 2013, 10:51 AM
|
 |
Square Peg in a Round Hole
|
|
Join Date: Oct 2007
Location: North Yorkshire, UK
|
|
Quote: | Does this have to be a physical file? Have you worked with MySQL before? Perhaps a database may also be an option for you? |
Such a file might store connection details to a database, and you could achieve such output using var_export()
you might do something like:
PHP Code:
//
//build from user supplied input - remember to sanitise first!!!!!
$config = array(
'username'=>'username',
'password'=>'password',
'host'=>'localhost',
'dbname'=>'mydatabase'
);
//build string using var_export
$c = '<?php $config = '.var_export($config,1).";";
//write to file
file_put_contents("config.inc.php",$c);
which would write
Code:
<?php $config = array(
'username'=>'username',
'password'=>'password',
'host'=>'localhost',
'dbname'=>'mydatabase'
);
to the file "config.inc.php"
some other code would then include config.inc.php and $config would be available as expected
Last edited by Northie : January 22nd, 2013 at 10:57 AM.
|

January 22nd, 2013, 12:01 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Also consider alternative file formats like JSON or XML. Both are very easy to read and write.
|

January 22nd, 2013, 05:24 PM
|
 |
Square Peg in a Round Hole
|
|
Join Date: Oct 2007
Location: North Yorkshire, UK
|
|
Quote: | Originally Posted by requinix Also consider alternative file formats like JSON or XML. Both are very easy to read and write. |
But for a php application I would always recommend storing such data as raw php - it will execute an awful lot quicker as there is no overhead in decoding the data (XML or json).
The caveat is always security and making sure that executable code which does something is not stored. So validate your data for type and sanitise for use.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|