PHP cookies

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Hi Guys,

I'm trying to implement cookies into my website but it doesn't seem to recognise that I've set the cookie and gives me a new one everytime.

PHP:
<?php
   if(isset($_COOKIE['shoppingcart_id'])) {
      echo "COOKIE IS SET";
   }
   else {
      echo "COOKIE IS NOT SET";
      
      //generate a random 10 character code
      $shoppingcart_id = generateId();

     //get the current date
     $today = date("Y-m-d");

     //get the users ip
     $user_ip = $_SERVER['REMOTE_ADDR'];

     //set the lifetime of the cookie
     $expire = time()+(60*60*24*7);

     //set the domain for which the cookie is valid
     $dom = $_SERVER['SERVER_NAME'];
     if($dom[0] == "w"){
        //truncate the first three characters
	$domain = substr($dom, 3);
     }
     else {
        //add point
	$domain = ".".$dom;
     }

     //finally send the cookie to the user agent
     setcookie("shoppingcart_id", $shoppingcart_id, $expire, "/", $domain)
   }

It always goes via the not set route?
 
Last edited:
sorry not sure what you mean, can you give an example?

Edit: Hmm just found that If upload the same code to my webserver and run it from there it works?!?!?!
 
Last edited:
Right ok, what counts as a session though? If a user puts something in their cart, browses away from the site briefly and then comes back will the session still be valid?
 
Back
Top Bottom