-2

I am aware that I can use sessions and GET/ POST method but I would like to achieve this using cookies. My code on page1.php is :

$_COOKIE['varname'] = $id;

and on page2.php is:

$id = $_COOKIE['varname'];

I get the following notice on my browser: Undefined index $id What is the problem with my code?

user3192304
  • 337
  • 1
  • 3
  • 10
  • [**This...**](http://php.net/manual/en/function.error-reporting.php) is your friend today. – Funk Forty Niner Jun 05 '16 at 15:05
  • 1
    You are not seting the cookie. – Andreas Jun 05 '16 at 15:07
  • I'm not sure that is correct Fred. I do not see this as an duplicate of that page you linked to as it has nothing with the actual error he is doing here. It's not about the cookie is empty, it's about that he doesn't set it correct. – Andreas Jun 05 '16 at 15:13

2 Answers2

2

Try using setcookie('varname', $id) then

if (isset($_COOKIE['varname']){ echo $_COOKIE['varname']; }
dashred
  • 131
  • 1
  • 12
1

To set a cookie you need to use setcookie(). And it has to be done prior to any output.

setcookie("mycookie", "myvalue" , $validtime); // validtime is a integer.
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • 1
    correct, except for the capitalization of `Setcookie()`, I tried to edit it myself, but it complains about edits must be at least 5 characters. – Jenny T-Type Jun 05 '16 at 15:28
  • @JennyT-Type Sorry about that. I wrote it on my phone and it keeps giving me capital letters at new setences. The day I build my own phone, I will make sure it's programming safe ;-) – Andreas Jun 05 '16 at 15:32
  • 1
    I totally hear you, this cursed auto-spelling/anti-programmer feature gets on my nerves- If you come with your own phone systems, please let your buddies here on SO know ;) – Jenny T-Type Jun 05 '16 at 15:37
  • @JennyT-Type Haha!! Will do! ;-) the best advice I can give is to buy a phone with qwerty keyboard. I bought my first qwerty phone in early 2000~ish and it's getting harder and harder to find them now. People at work keep teasing me for it. – Andreas Jun 05 '16 at 15:46
  • 1
    lol, I remember having one of those on my hands when I bough it for my mom, 2 months later I get a full-multi-touch for her, the qwerty phone, a motorolla if I remember correctly. ended on my hands again until I lost it about 2 years ago u_u . I remember things like: can I use your sci calc... ejem! phone plz. I have to call Bill Gates. or something like that. – Jenny T-Type Jun 05 '16 at 16:36