setting semi-permanent variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Faithe
Forum Commoner
Posts: 33
Joined: Tue Jul 12, 2005 3:26 pm
Location: WA

setting semi-permanent variables?

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: setting semi-permanent variables?

Post 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" />
(#10850)
Faithe
Forum Commoner
Posts: 33
Joined: Tue Jul 12, 2005 3:26 pm
Location: WA

Re: setting semi-permanent variables?

Post 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?
Bruno De Barros
Forum Commoner
Posts: 82
Joined: Mon May 12, 2008 8:41 am
Location: Ireland

Re: setting semi-permanent variables?

Post 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. :)
Faithe
Forum Commoner
Posts: 33
Joined: Tue Jul 12, 2005 3:26 pm
Location: WA

Re: setting semi-permanent variables?

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: setting semi-permanent variables?

Post 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.
(#10850)
Bruno De Barros
Forum Commoner
Posts: 82
Joined: Mon May 12, 2008 8:41 am
Location: Ireland

Re: setting semi-permanent variables?

Post 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.
Post Reply