SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unique identifier

    ok, i am looking for your (yes you!) opinion on this method of determining a unique visitor. i am just looking for an alternative to session variables and/or cookies. what i need is to generate an order_id for each user that is unique (never will one be the same, ever!), and then all their file uploads will be associated with this order_id.

    so what i did was create an order info table that had an auto_increment order_id field and another field for the date (to seconds)... and this is my script to generate a unique order_id.

    PHP Code:
    //get time
    $order_time date("n:d:Y G:i:s");

    //set order_id
    $sql "INSERT INTO order_info (order_time, checked) VALUES ('$order_time', 'yes')";
    mysql_query($sql,$conn) or die("could not create order id ".mysql_error());

    //get order_id
    $sql "SELECT order_id FROM order_info WHERE order_time = '$order_time'";
    $r mysql_fetch_row(mysql_query($sql,$conn)) or die("could not select order id ".mysql_error());
    $order_id $r[0]; 
    the only problem i can think of is that if two users sign on to the system at the exact same second then this system has failed (but that is easy to check for). what are your opinions to this system and do you have any suggestions. all comments will be appreciated.

  2. #2
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you should start reading mysql documentation first before proceeding further. Because mysql does have last insert id function which returns the last insert id (as name says) for the last query. And it is safe method because no 2 person gets the same value.
    http://www.php.net/manual/en/functio...-insert-id.php

  3. #3
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the heads up bro, i'll definetely look into that.

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here's another way to generate a unique id ...
    PHP Code:
    /* Create a Unique 32-character ID */
    $uniqueid md5 (uniqid (rand)); 
    ... not saying it's any better - just another way.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks siteguru, with that statment alone, there is no assurance that a previously generated id will resurface itself, correct? i apologize for my ignorance beforehand, i am not familiar with the md functions.

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    uniqid() returns a prefixed unique identifier based on the current time in microseconds. The prefix can be useful for instance if you generate identifiers simultaneously on several hosts that might happen to generate the identifier at the same microsecond. Prefix can be up to 114 characters long.

    If called without the optional min, max arguments rand() returns a pseudo-random value between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use rand (5, 15).
    In older versions of PHP, you had to seed the random number generator before use with srand(). Since 4.2.0 this is no longer necessary.

    md5() calculates the MD5 hash of str using the RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash. The hash is a 32-character hexadecimal number.

    That's the official explanation.
    Basically it means it is virtually impossible to get two identical results.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks bro! much appreciated.

  8. #8
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what would be your suggestions to keeping that unique identifier throughout a users "session" , am thinking about letting them navigate throughout the site via forms to allow for POST data, but what if they hit the "Back" button on the browser?

  9. #9
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Store the unique id as a session variable ...
    PHP Code:
    // (Re-)Start the session handler (this must be on all pages using sessions)
    session_start();

    // Set the session variable (e.g. after login)
    $_SESSION['unique'] = "the unique id value";

    // To check for it
    if ( empty ($_SESSION['unique']) !== false ) {
      
    // The session variable is empty

    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

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