Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.