The call to md5(uniqid(rand(),
true)) generates a unique, difficult-to-predict, 32-byte value, which represents the cart ID.
?– Note If you??™re interested to know the details about generating the cart ID, here they are. The md5()
function uses the Message-Digest algorithm 5 (MD5) to calculate the hash value of a value it receives as
a parameter. The hash value is 32 characters long. The uniqid() function returns a unique identifier based
on the current time in microseconds; its first parameter is the prefix to be appended to its generated value,
in this case, the rand() function that returns a pseudo-random value between 0 and RAND_MAX, which is
platform dependent. If the second parameter of uniqid() is true, uniqid() adds an additional combined
linear congruential generator (combined LCG) entropy at the end of the return value, which should make the
results even ???more unique.???
In short, uniquid(rand(), true) generates a ???very unique??? value, which is passed through md5() to ensure
that it becomes a random sequence of characters that is 32 characters long.
The SetCartId method is used only by the GetCartId() method that returns the cart ID. GetCartId() first
checks to see whether $_mCartId has been set, and if not, it calls SetCartId() before returning the value of
$_mCartId:
// Returns the current visitor's cart id
public static function GetCartId()
CHAPTER 12 ?– CREATING YOUR OWN SHOPPING CART 377
{
// Ensure we have a cart id for the current visitor
if (!isset (self::$_mCartId))
self::SetCartId();
return self::$_mCartId;
}
This value is saved to the cart_id field in the shopping_cart table, which is a CHAR(32) field especially to fit
the value returned by the md5() function.
Pages:
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487