SESSION value increases when browser is reloaded

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
uniballer
Forum Newbie
Posts: 7
Joined: Tue May 31, 2005 9:20 pm

SESSION value increases when browser is reloaded

Post by uniballer »

I have a major issue and don't really know how to get around it.

What I am trying to do is this:

A user selects a value from a drop down list and then enters a quantity in a text field and then press an add button. The result is then transferred into a textarea box in the format of
'item xxxx (2)' without the quotes.

At the same time this happens a DB search is done to find out how many points a certain item is worth and is then added to a session variable. Many items can be added to to the textarea and thusly increasing the value of the session variable.

Then I can submit the form that updates my DB with the items and quantities and the total number of points accumulated (session variable)

All the above stuff works great, however I recently found out that if I click the refresh button or hit F5 before i submit the form, the session variable increments by the value of the last points value.

This is obviously unwanted and I need to know how I can achieve this with PHP prefferably, but am willing to try some javascript.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you could potentially use another session var to detech whether or not to increment the value

Code: Select all

if ($_SESSION['checked'])
{
    //do the calcs
}
else
{
    //use your previously done calcs
}

$_SESSION['checked'] = isset($_SESSION['checked']) ? $_SESSION['checked'] : TRUE;
You must make sure you delete the session when you are viewing different products though.
uniballer
Forum Newbie
Posts: 7
Joined: Tue May 31, 2005 9:20 pm

Post by uniballer »

can I unset or delete a specific session variable, cause I have other sessions going on at the same time.

Also, I use the current value of the session to update the value of the variable, so I don't know if I can delete the session, even if I could delete a specific session variable.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

unset($_SESSION['checked']);
uniballer
Forum Newbie
Posts: 7
Joined: Tue May 31, 2005 9:20 pm

Post by uniballer »

OK, I just realized that I already use that at the submission part of my script, and that works.

But I can't really use that in the middle of the script as I use the value of the Session Variable to update the variable again when another item is added.

I hope I am explaing myself correctly.

Here is how I currently populate the Session variable:

Code: Select all

$_SESSION['points'] = (($x_pts['points'])*($_POST['spkrs_txt'])) + $_SESSION['points'];
Post Reply