D
D
Delp2014-05-29 22:58:05
PHP
Delp, 2014-05-29 22:58:05

How to work with cookies in php and check them

The crux of the problem is this is the code

<?php
if($_COOKIE["token"]==false){
$token = md5(uniqid());	
setcookie("token", $token,time()+1209600);
echo "true";
}else{
print_r($_COOKIE["token"]);
}
?>

It is necessary that when the user enters the site for the first time, cookies are created and a unique code is written there, and if the second time, then get data from the cookie. All this on localhost. Works fine in safari but not in other browsers. I enabled cookies but it doesn't work, an error is displayed
Notice: Undefined index: token in /Applications/MAMP/htdocs/mg/lib/cart.php on line 2
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP /htdocs/mg/lib/cart.php:1) in /Applications/MAMP/htdocs/mg/lib/cart.php on line 4
true
Explain how to do it right.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
maddog670, 2014-05-29
@Delp

if(!isset($_COOKIE["token"])){
  $token = md5(uniqid());	
  setcookie("token", $token, time()+60*60*24*30);
}else{
  print_r($_COOKIE["token"]);
}

M
Mykola, 2014-05-29
@iSensetivity

Remove echo "true";

A
amidaniram, 2014-05-29
@amidaniram

Maybe write if(!isset($_COOKIE["token"])){ instead of if($_COOKIE["token"]==false){

I
Igor Alexandrovich, 2014-05-30
@tyanigor

Via the super global array $_COOKIE. Use the search, it's elementary, there are thousands of examples on the Internet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question