1

I have a file called config.php that contains only variables.

<?php

//General
$domain = "http://www.nine9gag.com"; //Write the url of your website. EX: http://www.nine9gag.com

$waitMsg = "Wait..."; //The message between 2 images.
$buttonText = "Next Image"; //Default: Next Image

This is a short piece from entire code.

And i want to create a php script that change the variables with fresh data from a form and save this file.

Wich is the best way to do it? Thanks!

kraYz
  • 587
  • 1
  • 4
  • 11
  • 3
    Generally it's a bad idea to make PHP scripts editable in any way. Any reason you don't want to store the settings in a database? – Hamish Aug 29 '13 at 22:36
  • u should use mysql instead, but what do u want to do? – Michał Szałapski Aug 29 '13 at 22:37
  • 1
    what did you try so far? if you're using SQL it's probably best to store the variables there. If you must use a flat file like this you can use fopen http://php.net/manual/en/function.fopen.php and fclose. – Joe T Aug 29 '13 at 22:37
  • Because this script does not use DB at all and i don't want to create a DB connection for 14,15 variables. – kraYz Aug 29 '13 at 22:38
  • Write them to a textfile. – putvande Aug 29 '13 at 22:39
  • 1
    `file_put_contents` is easier then the fopen/fwrite/fclose routine. As for getting the data into the right format, I’d suggest to use an array as data structure instead of single variables, and using `var_export` to get it in form of valid PHP code. – CBroe Aug 29 '13 at 22:39
  • 3
    Try using a settings file [`.ini`](http://php.net/manual/en/function.parse-ini-file.php), [`.json`](http://php.net/manual/en/function.json-decode.php), [`unserialize`](http://php.net/manual/en/function.unserialize.php), or even a plain text file delimited by newlines. – Dave Chen Aug 29 '13 at 22:41
  • As others have said, you should consider a non-PHP storage format. But sometimes PHP is a practical choice. In that case, store an array to the file using `var_export()`. – Frank Farmer Aug 29 '13 at 22:45
  • My website it's like 9gag with funny contents (images,videos) and each "gag"'s details are storred in .txt files like: http://www.nine9gag.com/save/lol-8LUq.txt . It's ok or i have to use DB instead? – kraYz Aug 29 '13 at 22:45
  • Don't let your code modify itself, this is how SkyNet happens! – Sammitch Aug 29 '13 at 22:46
  • @DaveChen Please write that as an answer (and maybe add XML as an option). The key point is "save to a file" - but so far we seem to be accumulating a separate answer for every possible file format. – IMSoP Aug 29 '13 at 22:49
  • Use an array for your variables and `var_export()` if keeping it in a more-or-less readable php format is your primary concern. (It's not completely infeasible to have editable .php source; but the effort outweighs the benefits. And it's certainly not advisable for data files as you intend.) – mario Aug 29 '13 at 22:50

2 Answers2

1

The best way to do this is to store your data in an database. If that is not an option you should try to store this data in xml file.

Database

The good thing of a database is that its real secure. Without password and username you cant connect to it. You can even configure it in a way that just your server can connect to it.

XML

A nice clean way to store small information. Its an strict language so its can be used multi-platform. You can use your own attributes and names. To only bad thing is that you need to secure it your self with chmod or with security rules.

S.Visser
  • 4,645
  • 1
  • 22
  • 43
  • My website it's like 9gag with funny contents (images,videos) and each "gag"'s details are storred in .txt files like: nine9gag.com/save/lol-8LUq.txt . It's ok or i have to use DB instead? – kraYz Aug 29 '13 at 22:48
  • If you can. Please use a database. – Dave Chen Aug 29 '13 at 22:49
  • 1
    I would suggest you block direct access to .xml files when you go for them, by adding something like this to your .htaccess: ` Order allow,deny Deny from all ` – Pevara Aug 29 '13 at 22:49
  • For such an website you should use a database. W3schools wrote a great article about using mysql (a database engine) in combination with php. Maybe you can use that to start (http://www.w3schools.com/php/php_mysql_intro.asp) – S.Visser Aug 29 '13 at 22:52
0

U can use fread and fwrite to store variables in file. U can use json_encode. Save

$json = '{"variable1": 12345,"variable2":6789}';
fwrite($handle,$json,filesize());

Load

$json = json_encode($json);

$variable1 =  $obj->{'variable1'};12345
$variable2 =  $obj->{'variable2'}; 6789

just add works code to read and write

  • json encode is less efficient than a simple serialize, and fread/fwrite is probably less desirable than `file_get_contents()`, `file_put_contents()` – CD001 Aug 29 '13 at 23:02