I'm having issues setting my cookie, I check to see if the cookie is set if not it sets it, problem is it sets it every time! If I echo the result from setcookie it's 1 so I don't understand what's going on?
Code: Select all
<?php
if(isset($_COOKIE['shoppingcart_id'])) {
echo "COOKIE IS SET";
}
else {
//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)
echo "COOKIE IS NOT SET";
}