Jump to content

[SOLVED] Writting configuration files?


aximbigfan

Recommended Posts

Hi all, this is my first post here. I have recently started to learn PHP. The biggest app I have made is a log viewer/manipulator, about 500 lines.

 

Anyway,

 

Anyone know how to write configuration files in PHP?

 

I have though of a few ways, but non work really well.

 

Basically, I just want to write variables and values, to be used by a PHP app for its configuration.

 

For example

 

have a webui where the user says that they want "white" to be a background color, and "blue" be the text color. So they click on submit, and the PHP script opens config.php, and writes the following to it...

 

$bgcolor = "white";

$textcolor = "blue";

 

 

Chris

Link to comment
https://forums.phpfreaks.com/topic/89139-solved-writting-configuration-files/
Share on other sites

User configurable options are easier to store in a database, and its simple to achieve.

 

settings (table)

settings (
  id INT AUTO_INCREMENT,
  key TEXT,
  val TEXT
);

 

Then to insert bgcolor and textcolor.

 

INSERT INTO settings (key,val) VALUES ('bgcolor','white');
INSERT INTO settings (key,val) VALUES ('textcolor','blue');

 

To retrieve the bgcolor...

 

SELECT val FROM settings WHERE key = 'bgcolor';

Take a look at fwrite then.

 

Simple example.

 

<?php

  $str = "\$bgcolor = \"white\";\n\$textcolor = \"blue\";";

  if ($f = fopen('config.php','a')) {
    if (fwrite($f,$str)) {
      echo "config saved";
    } else {
      echo "failed to write config";
    }
    fclose($f);
  } else {
    echo "failed to open file";
  }

?>

Take a look at fwrite then.

 

Simple example.

 

<?php

  $str = "\$bgcolor = \"white\";\n\$textcolor = \"blue\";";

  if ($f = fopen('config.php','a')) {
    if (fwrite($f,$str)) {
      echo "config saved";
    } else {
      echo "failed to write config";
    }
    fclose($f);
  } else {
    echo "failed to open file";
  }

?>

 

So, it's that easy?

 

I have used fwrite() before, but I wasn't sure how I would escape variables.

 

 

Thanks a whole lot!!!

 

Topic solved.

 

Chris

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.