Hi all.
Why would the below always evaluate TRUE and generate a new $cartid every page load??

<?

 /**
    * Generate a Cart ID
    */
function generateCart() { 
      if(!isset($_COOKIE['cartid'])) {
        $cartid = md5(uniqid(rand())); 
     setcookie("cartid", $cartid, time() + 14400); 
 } else { 
     $cartid = $_COOKIE['cartid']; 
 } 
return $cartid;
}


?>

    Possibly because you never actually call the generateCart() function?

      I do within my class file. What is wrongπŸ™

        function startSession(){
            global $database;  //The database connection
            session_start();   //Tell PHP to start the session
      
        /* Determine if user is logged in */
        $this->logged_in = $this->checkLogin();
      
        /**
         * Set guest value to users not logged in, and update
         * active guests table accordingly.
         */
        if(!$this->logged_in){
           $this->username = $_SESSION['username'] = GUEST_NAME;
           $this->userlevel = GUEST_LEVEL;
      
           $this->cartid =  $_SESSION['cartid'] = $this->generateCart();
           $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
      
        }
        /* Update users last active timestamp */
        else{
           $database->addActiveUser($this->username, $this->time);
        }
      

        Almost 300 posts here and you still don't use the

         tags? *sigh* (I've edited them into your latest post.)

          Thanks NogDog. See anything wrong?

          Tracy

            I don't see anything obviously wrong, though I probably am not aware of all the logic and data flow that might affect things (such as whether or not various if conditions will evaluate to true or false.

            However, might it not be easier to store the cart ID in a session variable, rather than creating a separate cookie for it (in addition to the session cookie already being used)?

              An easy way way to test your function

              And notice my correct use of full tag: <?php
              You are a LAZY one, I suppose πŸ˜‰
              See my signature!

              <?php
              
              /**
                  * Generate a Cart ID
                  */
              function generateCart() {
              
                // test
                echo '<pre>'. print_r($_COOKIE['cartid'], true) .'</pre>';
              
                if(!isset($_COOKIE['cartid'])) {
                     $cartid = md5(uniqid(rand()));
                     setcookie("cartid", $cartid, time() + 14400);
                } else {
                     $cartid = $_COOKIE['cartid'];
                }
               return $cartid;
              }
              
              ?>
                Write a Reply...