Forum Moderators: coopster

Message Too Old, No Replies

Rewriting only the value of a variable on a file

         

lucesei

9:43 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



Basically, I have a page, call it "entries.php", I use to display entries from my visitors. I have set a certain number of entries to display per page, example:

$Display = 10;

I store this variable on the same page, and I want to keep it like this.

I've put a simple input field on the page, to change the number of entries displayed, according to my needs, and that is no problem:


<form name="changedisplay" action="admin.php?page=viewlog" method="post">
Choose entries per page: <input name="DisplayValue" type="text" value="$Display" size="10" maxlength="3">
<input type="submit" name="Change" value="Change">
</form>

and then:

if (isset($_POST['Change'])) {
$Display = $_POST['DisplayValue'];
} else {
$Display = 10;
}

And then keeping the new $Display value throughout the pages using $_GET.

But obviously, once I leave the page, the value of $Display goes back to the default one. What I want to do is to store permanently the new value, by overwriting the default one.

How can I do this, without having to overwrite the whole page but only the value of the single variable? Is it possible?
I know I can do this simply (and I know how to do it) by moving the $Display variable to a file outside of "entries.php", but it's a pity for only 1 configuration variable. And I'm also curious...

Looking forward to some good news, I thank you for the help

HughMungus

10:04 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm a relative newbie at php but what about a cookie?

coopster

11:25 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, lucesei!

HughMungus is spot on. Each user might have a different value, so setting a cookie [php.net] is probably the best option.

lucesei

12:51 am on Jul 30, 2004 (gmt 0)

10+ Year Member



A cookie is a good idea, but not exactly what I am looking for. Maybe I have not set my question correctly: I need to find a way of setting a new default value for a variable that's stored inside a page. An easier example:
say I have a settings page, settings.php

<?php
variable1 = 10;
variable2 = 33;
variable3 = 178;
?>

Now I have a page (call it "settings_admin.php") with which I can control the settings through a form (I'm not writing the code down, but you know what I mean).
I want to change the value of variable2 (say from 33 to 112), WITHOUT HAVING TO REWRITE the rest of the content of settings.php (don't ask me why). In other words, some code that, upon submitting the new value of variable2:

if (isset($_POST['submit'] {
$handle = fopen("settings.php", "r+") //is r+ correct?
//then find the "variable2"
//then change the value from 33 to 112
fclose ($handle);
}

All this without touching variable1 and variable3. Is this possible?

Thanks a lot for your help

coopster

12:46 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



So you want to open and modify an existing script on-the-fly? Sure, you can do that.

1) Read the file contents into a variable.
2) Find and replace the specific value.
3) Overwrite the existing file with the new contents.