问题
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.
回答1:
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);
回答2:
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
回答3:
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:
- flock — Portable advisory file locking
- parse_ini_file — Parse a configuration file
- json_encode — Returns the JSON representation of a value
- var_export — Outputs or returns a parsable string representation of a variable
Demos:
- Array approach
- JSON appraoch
回答4:
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.
来源:https://stackoverflow.com/questions/25558820/how-do-i-use-php-to-edit-a-variable-in-another-php-script