0

I'm trying this code:

<?php
    $cookie_name = "idmarket";
    $coolie_value = "1";

    if (!isset($_COOKIE[$cookie_name])){
        echo "Cookie wasn't set";
        setcookie($cookie_name,$coolie_value,time() + 86400);
        echo "Now cookie set : " . $_COOKIE[$cookie_name];
    } else {
        echo "Cookie have been set : ".$_COOKIE[$cookie_name];
    }
?>

I want to do something if the cookie is not set and do nothing if it's set. but when I try to test if the cookie now is set and echo $_COOKIE[$cookie_name] I get Notice: Undefined index: idmarket instead.

Edwin
  • 1,135
  • 2
  • 16
  • 24
Artin GH
  • 546
  • 2
  • 10
  • 21
  • 2
    From the manual (emphasis mine): "*Once the cookies have been set, they can be accessed on the **next page load** with the $_COOKIE array. Cookie values may also exist in $_REQUEST.*" - this means that you cannot access it directly after setting it - you need to reload first. – Qirel Aug 15 '17 at 14:20
  • @RajdeepPaul this is what I get : `array (size=2) '_ga' => string 'GA1.1.1600023990.1498488379' (length=27) 'PHPSESSID' => string 'knsh102nhreq9qk7obgkfjgf22' (length=26)` – Artin GH Aug 15 '17 at 14:21
  • @Qirel Oops good point. I was guessing that.. thank you – Artin GH Aug 15 '17 at 14:23

0 Answers0