Results 1 to 8 of 8
Thread: Unique order number
-
10-03-2002, 12:28 PM #1Web Hosting Evangelist
- Join Date
- May 2002
- Location
- Durham - UK
- Posts
- 450
Unique order number
Hi
I have a problem. I am currently using the following code to generate a unique order number which I add to each order made that works fine:
$order_id = (substr(uniqid (""), 2, 7));
This works fine and creates a unique code with both numbers and letters. But I would like to create numbers only and after looking around and checking out the PHP manual I cant seem to find a solution.
Any help would be appreciated.Barry
UK Based Freelance PHP Developer
PHP/SQL/Ajax/HTML5 - Contact for Quote
-
10-03-2002, 01:37 PM #2Junior Guru
- Join Date
- Sep 2002
- Location
- Dallas, TX
- Posts
- 205
If all you want are numbers, then why not just store a persistent value somewhere (n) then increment it each time you need a new number?
justin 'at' abrogo.com
http://www.abrogo.com
Shared Unix Hosting
-
10-03-2002, 10:33 PM #3WHT Addict
- Join Date
- Mar 2002
- Posts
- 123
Well... if you are using a DB, you can have it create a new ID number for each order.
Or you could do some variations on rand().www.atlanticrail.com
-
10-04-2002, 07:12 AM #4Web Hosting Master
- Join Date
- Nov 2001
- Posts
- 857
I agree with cortices.
If all you want is each order to have its own unique number then create a file, say num.inc, and put an arbitrary number in there. Something like 1438.
Your order script calls the file, adds one to that number and that is your new number for the present client. Then you save that new number to the same file..
file: num.inc
permissions should be 777
PHP Code:$fp = fopen("num.inc", "w+");
$num = fread($fp, filesize ("num.inc"));
$num++; // $num +1
fputs($fp, $num);
fclose($fp);
This assumes two things. One that your file is 777 or the directory it is in is 777. Two, the only thing stored in the file is that number.
Regards,
Michael<?
header("Location: http://www.hostevolve.com/");
?>
-
10-04-2002, 08:01 AM #5Junior Guru
- Join Date
- Sep 2002
- Location
- Dallas, TX
- Posts
- 205
If you do use files, you may want to consider making use of flock() to ensure there aren't any data integrity issues.
If two people hit your order form at the exact same second, the possiblity exists that the requests could read the same value and increment it, producing a non-unique number.
PHP Code:$fp = fopen("num.inc", "w+");
flock($fp, lock_ex); // gain exclusive lock for writing
$num = fread($fp, filesize ("num.inc"));
$num++; // $num +1
fputs($fp, $num);
flock($fp, lock_un); // release lock
fclose($fp);
justin 'at' abrogo.com
http://www.abrogo.com
Shared Unix Hosting
-
10-04-2002, 08:19 AM #6Web Hosting Master
- Join Date
- Jan 2001
- Posts
- 2,605
Originally posted by michaeln
permissions should be 777Dr. Colin Percival, FreeBSD Security Officer
Online backups for the truly paranoid: http://www.tarsnap.com/
-
10-04-2002, 11:05 AM #7Web Hosting Master
- Join Date
- Nov 2001
- Posts
- 857
Yeah I forgot to flock the file...
As for the permissions it can be 646 as php runs as nobody. You could always chown the file to nobody and chmod it to 644.
Still, the first way anyone has read right access and the second way anyone with php has readright access.
Really the best way to do it is to keep the number in a database and encrypt database username and password in a text file...
Regards,
MichaelLast edited by michaeln; 10-04-2002 at 11:29 AM.
<?
header("Location: http://www.hostevolve.com/");
?>
-
10-05-2002, 04:47 AM #8Web Hosting Master
- Join Date
- Jan 2001
- Posts
- 2,605
Originally posted by michaeln
As for the permissions it can be 646 as php runs as nobody.
Dr. Colin Percival, FreeBSD Security Officer
Online backups for the truly paranoid: http://www.tarsnap.com/