2

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.

16
  • You simply want to write to a file in php? Commented Aug 28, 2014 at 22:12
  • You could do it but it wouldn't work well, be very insecure, etc. Most server environments have read-only access to PHP scripts to prevent this sort of thing. Commented Aug 28, 2014 at 22:12
  • 2
    Don't do that, store it encrypted on a database Commented Aug 28, 2014 at 22:12
  • @PeeHaa I'm not sure. I need the script to simple edit one variable, Not the entire page. Commented Aug 28, 2014 at 22:12
  • 1
    @MarkieJonesWTF What is it in specific what you are trying to do? What kind of variable are you storing. Is it something like a config thing? Will there be more information in the script you are trying to write into? What is the reason it is in the script in the first place. I get the feeling a lot of people are jumping right in without having an idea what it is you are trying to do. Can't really blame them, because neither do I. Commented Aug 28, 2014 at 22:20

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

5 Comments

For a password? Shouldn't it last longer than a session?
He was not asking for storing a password. Only a certain value. For a passwort i would better store it encrypted into a database.
@derdida, The example he gave was a password.
Yes, but he wrote that this is just a example. he was asking for a simple variable. If he need a solution for storing a password he should ask again, or edit the question.
@developerwjk you're only assuming that because it has "password" in the string's value. See his comment
2

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:

Comments

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); 

10 Comments

I was not downvoting, but i guess he will only set another value and not directly edit one PHP-File.
@derdida That's what he asked for.
What if two users write to the file at once? This is why databases were invented.
@developerwjk Yes, This answer is slightly incomplete. There is no example, Only a link to a description of fopen which says nothing about fread and fwrite.
But comments can be. I would suggest something a little easier than manipulating files on your own. pear.php.net/manual/en/package.configuration.config.intro.php
|
-2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.