0

General Query: Is there a way to read a variable from another PHP file, without running the file (as the variable had been set at another point in time, using a third PHP file), and without the use of a database?

Specific Instance: I created a webpage that my boss wants to change daily. He doesn't want to access the raw HTML and CSS coding, as any mistake will lead to headaches on my end. So I thought I might create a small portal that will allow him to change the specific string data within the webpage each day, limiting his access to the code.

The framework I conceived would work like this:

  • index.php is the page that is viewable by everyone on the internet. When it is rendered, I'd like it to request the $Category variable that was saved previously in another file.
  • edit.php is a webpage that displays a simple form with two input boxes: Category and Password. When the submit button is clicked, it POSTs the data to the file below.
  • configuration.php is the file that edit.php hands the form information off to. Once the form information has been handed off, it checks to see if the crypt() version of the input password matches, and then stores the Category information in a variable called $Category.

Now, I need a way for index.php to read the previously saved value of $Category within the configuration.php file, without running it, and without using a database (as I do not have access to one). I don't think I can use session values, because this value should be the same for all users and all sessions, until it is changed again by my boss. Is there any way to implement this, or should I be using a different method?

3 Answers3

1

Consider just writing the data to a file instead, and reading it from there.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Thanks @Amber . Would I be able to do the same thing by [setting up a variable in a class](http://stackoverflow.com/questions/834491/create-superglobal-variables-in-php)? Which would be faster/less CPU load on the server? – Nicholas Ryan Bowers Aug 11 '12 at 22:47
  • Variables are generally not intended to persist between requests. If you're using APC, you can theoretically [store data there](http://www.php.net/manual/en/function.apc-add.php), but that's really not a good way to persist things since there's no guarantee on how long it will live for. – Amber Aug 11 '12 at 22:50
0

Your site should have a private area where you can upload files and they are not served as part of the site but you can read them from your PHP code. Then your boss can simply upload a new file with the appropriate value in it.

Neil
  • 54,642
  • 8
  • 60
  • 72
0

So instead of writing the data to a file of it's own to have and store, I decided to dynamically overwrite the original file that needed the information, whenever a script was run (and thus the variable changed).

When the submit button on edit.php is hit, it hands the information off to configuration.php, which rewrites index.php by the time it is done running. Below is a portion of the code I used in case anybody would like to implement something similar, without having to write the data to a completely separate file.

// Read the contents of the file you want to change, into a variable
$indexContents = file_get_contents('index.php');

// Edit the contents with srt_replace or preg_replace
$indexContents = preg_replace('#<h2>(.*?)\?</h2>#i', '<h2>'.$title.'?</h2>', $indexContents);
$indexContents = preg_replace('#best-(.*?)"#', $url.'"', $indexContents);

// Write the contents of the variable back to the same file name, overwriting it
file_put_contents('index.php', $indexContents);

echo '<h1>Success!</h1>';</code>