Click to See Complete Forum and Search --> : how to validate if a cookie does not exist?


terry81
09-27-2004, 01:42 AM
Hi there,

i have the follow codes that do not work (obviously is wrong). I need help with it.

<?php
$cookieValue = $_COOKIE["product"];
if ($cookieValue == ""){
setcookie("product", 0);
print "new cookie made";
}
else{
setcookie("mowidgetproduct",$cookieValue);
print "Old cookie retained";
}
?>

The error is that whenever cookie named Product is not found, I will get index error. What is the correct way to check if a cookie has already exist and if not, create a new one and if yes, retain the old value?

Jona
09-27-2004, 01:52 AM
<?php
$cookieValue = @$_COOKIE["product"];
if (empty($cookieValue)){
setcookie("product", "zero", time()+3600);
print "New cookie made.\n";
} else {
setcookie("product", $cookieValue, time()+3600);
print "Old cookie retained.\n";
}
?>


...or...



<?php
if (!isset($_COOKIE["product"])){
setcookie("product", "zero", time()+3600);
print "New cookie made.\n";
} else {
setcookie("product", $_COOKIE["product"], time()+3600);
print "Old cookie retained.\n";
}
?>

terry81
09-27-2004, 02:03 AM
THANKS ALOT JONA!

I knew you will come to my rescue! I try to look for code snippets to validate if a cookie exist or not but cant find it on the net and i have only 2 php textbks with me.

Thanks. :D

terry81
09-27-2004, 02:52 AM
Btw Jona,

Why is it that I cant read Perl made cookie with PHP codes ... i just dont get it. I try to create a perl cookie with the same name as the php cookie and try to override it but cant.

Anyway guess no one will implement 2 different scripts in 1 project..well i am doing it for my assignment so well...

Jona
09-27-2004, 10:12 AM
When you set a cookie in Perl, it is set on the client's machine and therefore readable and writable by any language on the same domain. You can use PHP, JavaScript, ASP, and so on.

terry81
09-28-2004, 02:31 AM
Hi Jona,

I have actually asked my fellow students about this cookie read by perl and php problem...one of them said they solved it by clearing away the cookie manually in the Temporary Internet Files folder.

She said somehow or rather the browser seems to retrieve through the cache or probably xitami(web server in our personal pc) is at fault.

Well...:o

Jona
09-28-2004, 12:56 PM
Get Apache. :p