Page 1 of 1

setting semi-permanent variables?

Posted: Wed Aug 04, 2010 1:46 pm
by Faithe
I'm unsure what to title this problem, quite frankly.

Currently, my system is set up that users update the content of several different "blocks" of information, which then updates the entry stored in the database. How can I just set a variable through a form, eliminating the need for a database entry altogether? I can set a variable through POST, but after the POST information is cleared, the variable clears too.

Or, is there a different way to approach this problem? I feel like I am relying far too much on database queries.

Re: setting semi-permanent variables?

Posted: Wed Aug 04, 2010 2:22 pm
by Christopher
Maybe something like:

Code: Select all

<?php
$age = isset($_POST['age']) ? intval($_POST['age']) : '';     // remember to filter all input and escape all output
?>
<input type="text" name="age" value="<?php echo htmlspecialchars($age); ?>" size="2" />

Re: setting semi-permanent variables?

Posted: Wed Aug 04, 2010 2:34 pm
by Faithe
This doesn't "save" the variable content, though.
Once the page is refreshed, I need the variable information to remain instead of clearing.

I think what I'm suggesting is impossible, as it would almost require it to physically change the contents of the file it's accessing.

Right?

Re: setting semi-permanent variables?

Posted: Wed Aug 04, 2010 2:41 pm
by Bruno De Barros
Use sessions if you want the stuff to stay there until the browser is closed. If you want it to stay longer, change the session lifetime. More on this here: http://php.net/manual/en/ref.session.php

With sessions, just run session_start() at the beginning of your script, and when a user submits something through POST, add it to your $_SESSION, and that's it. :)

Re: setting semi-permanent variables?

Posted: Wed Aug 04, 2010 3:05 pm
by Faithe
I don't understand how that would work. I was always under the impression sessions involved the user's side of things.

Won't they expire eventually?

Re: setting semi-permanent variables?

Posted: Thu Aug 05, 2010 3:52 am
by Christopher
If you want the values stored permanently then you need a datasource like a database to save them to. You can use sessions or pass hidden form fields to maintain values through a set of forms -- until the values are saved to a database.

Re: setting semi-permanent variables?

Posted: Thu Aug 05, 2010 4:55 am
by Bruno De Barros
Session data is kept on the server, the only thing the user keeps is the session in ID, usually in a cookie. It will expire, but you can set the lifetime to something like a a year or more, then the data will stay there for that period of time. I don't see why you want to avoid databases that much, but sessions are the only type of storage that I can think of that seems to fit your needs.