hw4m
hw4m

Reputation: 432

PHP how to update variable in php-file

How do i update a variable in a php file and save it to disk? I'm trying to make a small CMS, in one file, no database, and i'd like to be able to update the config variables, I've made a settings page and when i update i'd like the variables to updated in the php-file. Below will update the variable, but of course not save it to the php-file, so how do i do that?

<?php 
$USER = "user";
if (isset($_POST["s"]) ) { $USER = $_POST['USER']; }
?>
<html><body> 
Current User: <?php echo $USER; ?>
<form method="post" action="<?php echo $_SERVER["SCRIPT_NAME"]; ?>">
New User<input type="text" name="USER" value="<?php echo $USER; ?>"/>
<input type="submit" class="button" name="s" value="Update" />
</form>
</body></html>

I don't know if i'm missing the obvious? I was thinking of using something like this:

$newcms = file_get_contents(index.php)
str_replace(old_value, new_value, $newcms)
file_puts_contents(index.php, $newcms)

But it doesn't seem like the right solution...

Upvotes: 0

Views: 2972

Answers (3)

T.Todua
T.Todua

Reputation: 56557

for example, you have YOURFILE.php, and inside it you have $targetvariable='hi jenny';

if you want to change that variable, then use this:

<?php
$fl='YOURFILE.php'; 
        /*read operation ->*/ $tmp = fopen($fl, "r");   $content=fread($tmp,filesize($fl)); fclose($tmp);

// here goes your update
$content = preg_replace('/\$targetvariable=\"(.*?)\";/', '$targetvariable="hi Greg";', $content);
        /*write operation ->*/ $tmp =fopen($fl, "w");    fwrite($tmp, $content);    fclose($tmp);
?>

Upvotes: 0

prodigitalson
prodigitalson

Reputation: 60413

The easiest way is to serialize them to disk and then load them so you might have something like this:

<?php 

  $configPath = 'path/to/config.file';
  $config = array(
    'user' => 'user',
  );

  if(file_exists($configPath)) {
     $configData = file_get_contents($configPath);
     $config = array_merge($defaults, unserialize($configData));
  }

  if(isset($_POST['s']) {
      // PLEASE SANTIZE USER INPUT
      $config['user'] = $_POST['USER'];

      // save it to disk
      // Youll want to add some error detection messagin if you cant save
      file_put_contents($configPath, serialize($config));
  }
?>

<html><body> 
Current User: <?php echo $config['user'; ?>
<form method="post" action="<?php echo $_SERVER["SCRIPT_NAME"]; ?>">
New User<input type="text" name="USER" value="<?php echo $config['user']; ?>"/>
<input type="submit" class="button" name="s" value="Update" />
</form>
</body></html>

This approach uses PHP's native serialization format which isnt very human readable. If you want to be able to manually update configuration or more easily inspect it you might want to use a different format like JSON, YAML, or XML. JSON will probably be nearly as fast and really easy to work with by using json_encode/json_decode instead of serialize/unserialize. XML will be slower and more cumbersome. YAML is pretty easy to work with too but youll need an external library like sfYaml.

Additionally i wouldnt just do this at the top of a script, id probably make a class of it or a series of functions at the least.

Upvotes: 2

iDifferent
iDifferent

Reputation: 2210

As a better approach, you can have a separate file just for the settings and include that file in the PHP file. Then you can change and save its values as you want instead of modifying the PHP file itself.

Upvotes: 1

Related Questions