Not fetching cookies "sometimes' Help Please

Discussion in 'PHP' started by jacka, Dec 4, 2008.

  1. #1
    Hi

    I use cookies in my website to store customers items selected price and description in one page and use that information (from database) in next pages to access various details such as total cost (by adding the price of items bought), etc.

    This system works most of the time, however every now and then I get customers telling me that the total is zero,which means it can not fetch the cookies, hence it does not know which items the customer has selected in their shopping cart to total the cost.

    I use a standard function call:
    
    
    <?php
    
    	// This page contains the connection routine for the
    	// database as well as getting the ID of the cart, etc
    
    	$dbServer = "xxxxxk";
    	$dbUser = "yyyyyy";
    	$dbPass = "zzzzzz";
    	$dbName = "fffffffffffffff";
    
    	function ConnectToDb($server, $user, $pass, $database)
    	{
    		// Connect to the database and return
    		// true/false depending on whether or
    		// not a connection could be made.
    		
    		$s = @mysql_connect($server, $user, $pass);
    		$d = @mysql_select_db($database, $s);
    		
    		if(!$s || !$d)
    			return false;
    		else
    			return true;
    	}
    	
    	function GetCartId()
    	{
    		// This function will generate an encrypted string and
    		// will set it as a cookie using set_cookie. This will
    		// also be used as the cookieId field in the cart table
    		
    		if(isset($_COOKIE["cartId"]))
    		{
    			return $_COOKIE["cartId"];
    		}
    		else
    		{
    			// There is no cookie set. We will set the cookie
    			// and return the value of the users session ID
    			
    			session_start();
    			setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
    			return session_id();
    		}
    	}
    
    ?>
    Code (markup):

    As this works 90% of the time, I suppose what I am really asking is:
    What possible situations can cause a computer not to store cookies, so I can advise my customers to remedy the situation.

    Thx for your time.
    :confused::confused:
     
    jacka, Dec 4, 2008 IP
  2. yuyujoke

    yuyujoke Guest

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    A cookie is set with the following code: setcookie(name, value, expiration)

    <?php
    $Month = 2592000 + time();
    //this adds 30 days to the current time
    setcookie(AboutVisit, date("F jS - g:i a"), $Month);
    ?>

    The above code sets a cookie in the visitor's browser called "AboutVisit". The cookie sets the value to the current date, and set's the expiration to be be in 30 days (2592000 = 60 seconds * 60 mins * 24 hours * 30 days.)

    Now let's retrieve the cookie.

    <?php
    if(isset($_COOKIE['AboutVisit']))
    {
    $last = $_COOKIE['AboutVisit'];
    echo "Welcome back! <br> You last visited on ". $last;
    }
    else
    {
    echo "Welcome to our site!";
    }
    ?>

    This code first checks if the cookie exists. If it does, it welcomes the user back and tells them when they last visited. If they are new, it skips this and prints a generic welcome message.

    TIP: If you are calling a cooking on the same page you plan to set one - be sure you retrieve it first, before you overwrite it!
     
    yuyujoke, Dec 4, 2008 IP
  3. yuyujoke

    yuyujoke Guest

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    A cookie is set with the following code: setcookie(name, value, expiration)

    <?php
    $Month = 2592000 + time();
    //this adds 30 days to the current time
    setcookie(AboutVisit, date("F jS - g:i a"), $Month);
    ?>

    The above code sets a cookie in the visitor's browser called "AboutVisit". The cookie sets the value to the current date, and set's the expiration to be be in 30 days (2592000 = 60 seconds * 60 mins * 24 hours * 30 days.)

    Now let's retrieve the cookie.

    <?php
    if(isset($_COOKIE['AboutVisit']))
    {
    $last = $_COOKIE['AboutVisit'];
    echo "Welcome back! <br> You last visited on ". $last;
    }
    else
    {
    echo "Welcome to our site!";
    }
    ?>

    This code first checks if the cookie exists. If it does, it welcomes the user back and tells them when they last visited. If they are new, it skips this and prints a generic welcome message.

    TIP: If you are calling a cooking on the same page you plan to set one - be sure you retrieve it first, before you overwrite it!
     
    yuyujoke, Dec 4, 2008 IP