How to stop random number from changing everytime

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
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

How to stop random number from changing everytime

Post by Tokunbo »

hello guys,

I'm trying to make a cart. I have a couple of pull-down menus, categorized into group of items etc.

The process is like this:
for example, on form-1 there is a pull-down menu, a user can select an item and click the update-cart button. Cart gets updated. A user can still select another item and update the same cart. The same goes for other pull down menus/forms. Selections can be made and cart will be updated. Things are working ok, and my DB is getting updated correctly. The contents of the cart are being displayed to the user for the purpose of editing / deleting an item previously added to the cart.

When I try to simulate being 1-user, things work fine. If I try using the tool from two different computers(Im running a webserver on my LAN), I can see the contents of the DB(cart) "ordered" from computer-1 on computer-2 eventhough Ive not ordered anything from computer-2.

So I thought about having a uniqueID from( a random-number and date )to be used to differentiate simultaneous users. This value gets updated into my table and the specific orders mapped to the UniqueID should get displayed only to the user.

I tried to include a unique key/value with code below:

Code: Select all

<?php


//require statements

//generate random order-filename

$OrderNo = rand(0,500);

$OrderDate = date("FjY");

$Key= "$OrderDate$OrderNo";

?>
//html code comes next
Note: $key is stored/Posted (into my DB/table) as a hidden input-type from my form whenever the submit button is clicked. The format of $Key is like this: November52011343. Problem is, for every click of the form, a new random number is generated, thus my goal is not achieved.

questions:
1) How do I make sure the random number is not re-run for every click of the submit button?
2) is the above the best approach considering my description.
3) are sessions applicable here considering the form is posting to the same page?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to stop random number from changing everytime

Post by Celauran »

I would use either session data or cookies to store shopping cart info, only writing to the database once the purchase has been made.
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: How to stop random number from changing everytime

Post by Tokunbo »

@Celauran
thanks for the info. My challenge actually has been how to store the session data, recall them to be displayed in a cart.

Presently, I am storing the cart info into the DB, then displaying them.

pls advise.
Post Reply