user3931279
user3931279

Reputation:

How do i use PHP to edit a variable in another PHP script?

I am currently in the progress of making a basic PHP app/page, However, I have run into a problem. I have needed to make a script which will edit a variable in another script and set it to something.

For example, One script would say:

$test = "username";

And the other script would set that variable to something else.

So, Once the PHP script's (index.php) page is loaded, The PHP script will change the first script (variables.php) to:

$test = "username20";

However, I do not know how to do this. Is there any possible way that I could do this?

EDIT:

I need a basic auto-editor like this because the file is a configuration file (So basically, I need the text inside to be there permanently, Meaning that a "Session" will not work.).

Thank you if you can help.

Upvotes: 2

Views: 4434

Answers (4)

PeeHaa
PeeHaa

Reputation: 72729

As I see it you have several options. As you stated this is going to be some configuration file so most likely you will have several different configuration options. Although in your example you have separated values imho it makes more sense to have some collection instead. And you config file may looks something as follows:

<?php

$config = array(
    'dbname' => 'my_awesome_db',
    'dbhost' => 'localhost',
    'dbuser' => 'notroot',
    'dbpass' => 'secret',
);

Using PHP you could change the above array and write it back to the file:

<?php

require __DIR__ . '/config.php';

$config['dbhost'] = '10.1.0.1';

file_put_contents(__DIR__ . '/config.php', '<?php $config = ' . var_export($config, true) . ';');

Another way to go is to use the JSON format:

{
    "foo": "bar",
    "baz":"qux"
}

Using PHP you could change the above JSON and write it back to the file:

<?php

$config = json_decode(file_get_contents(__DIR__ . '/config.php'), true);

$config['dbhost'] = '10.1.0.1';

file_put_contents(__DIR__ . '/config.php', json_encode($config, JSON_PRETTY_PRINT));

Above are just two options. You could also write to an ini file, database or whatever format you like.

Some notes though:

  • if the file is going to contain sensitive information you have to make sure it is placed outside of your document root to prevent making it accidentally public (e.g. server misconfiguration or human error)
  • if concurrency is going to be a problem you would either have to look into a locking mechanism or use a database to let it handle it for you
  • Normal rules apply if any of this is ever going to get or has been in contact in any way with any form of user input
  • Although there is nothing inherently wrong about having a config file like this. It is not a silver bullet and should only be used when it makes sense. For some things you would be better of using a database depending on the use case.

Related reads:

Demos:

Upvotes: 2

Lior
Lior

Reputation: 6051

You should use fopen to open a file, read the file with fread to search the wanted variable and then write with fwrite.

$fn = "variables.php"; 
$file = fopen($fn, "w+"); 
$size = filesize($fn); 
$text = fread($file, $size); 
fwrite($file, 'text'); 
fclose($file); 

Upvotes: 1

Evan Carslake
Evan Carslake

Reputation: 2359

It depends on what you are wanting. If you are creating some kind of "control panel" for yourself only; you would want to hardcode the password and use a session/ cookie to stay "logged in."

If you are wanting multiple people to use it, even a member system, you will need a database.

There are text file based databases out there, but I am thinking smaller would be better.

UPDATE:

You can't really do that, well, you could but it would be a extremely bad practice.

If what you want edited is not sensitive information, you can write/ read/ edit a regular file (like info.txt, or info.dat)

Another update If you insist on writing a .php variable, you technically can...

Using file functions to access a (ex: data.php) file, you can literally write: $user_password = "blah";

Then include data.php in your main file, and $user_password will be defined. If you want to edit it, use file functions to update the data.php file.

Upvotes: -2

derdida
derdida

Reputation: 14904

Better use Session Variables. They are valid in all your PHP Scripts when the Session is loaded.

http://php.net/manual/en/book.session.php

Upvotes: 4

Related Questions