Results 1 to 8 of 8
  1. #1
    Join Date
    May 2002
    Location
    Durham - UK
    Posts
    450

    Question 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

  2. #2
    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

  3. #3
    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

  4. #4
    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($fpfilesize ("num.inc"));
    $num++; // $num +1
    fputs($fp$num);
    fclose($fp); 
    Your new number is in $num and the file has already been updated and saved...
    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/");
    ?>

  5. #5
    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($fplock_ex); // gain exclusive lock for writing

    $num fread($fpfilesize ("num.inc"));

    $num++; // $num +1

    fputs($fp$num);

    flock($fplock_un); // release lock

    fclose($fp); 
    justin 'at' abrogo.com
    http://www.abrogo.com
    Shared Unix Hosting

  6. #6
    Originally posted by michaeln
    permissions should be 777
    michaeln
    Dr. Colin Percival, FreeBSD Security Officer
    Online backups for the truly paranoid: http://www.tarsnap.com/

  7. #7
    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,
    Michael
    Last edited by michaeln; 10-04-2002 at 11:29 AM.
    <?
    header("Location: http://www.hostevolve.com/");
    ?>

  8. #8
    Originally posted by michaeln
    As for the permissions it can be 646 as php runs as nobody.
    Yes, well, that's the problem. On shared webspace, php should *not* be running as user nobody.
    Dr. Colin Percival, FreeBSD Security Officer
    Online backups for the truly paranoid: http://www.tarsnap.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •