Doesn't it just work for me? Or you, too?

<? if(!isset($_COOKIE['name'])) { setcookie ('name','server'); } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Куки</title> </head> <body> <? echo "HELLO! ".$_COOKIE['name']; ?> </body> </html> 

I can not understand anything. Yesterday the main site went, copied the code, pasted on another page - and all the best! Does not work. Today I decided to just protest on a clean sheet, so to speak, from the zero page. And does not go! Maybe correct me, maybe what I'm doing wrong?

Update

There are no spaces. He stands in the top corner. Code formatted, put the <?php tag. But still the same mistake. If it works for you, then something is wrong with me.

I ask then to help parse the actions step by step. From the beginning of the script. I just do not understand what headings are sent? Where are they going? I used to work with ordinary formulas in Excel, and so there was such a thing - test the formula step by step. Is there something similar here? If there is, give a link to the program that does it, maybe she can help me figure out which headers I send before the code?

  • one
    Here, it seems, everything is correct. Probably, somewhere before the php code there are gaps, maybe you have the directive short_open_tag turned off. Open php code tag <? Php, and not <? PS Format the code. - mountpoint
  • @ zaryad68gmail, To format the code, select it with the mouse and click on the {} button of the editor. In the code, unused and meaningless fragments are also better removed. - Alex Krass
  • Updated the question - Edward
  • @ zaryad68gmail, fill in ALL your code somewhere and show us. This piece is not enough to identify the problem. And also, try this, although this will not change the weather: >> <? Php echo isset ($ _ COOKIE ['name'])? "HELLO!". $ _ COOKIE ['name']: ''; ?> - mountpoint
  • one
    imat kalatit, how much noise because of the boom - mountpoint

2 answers 2

You also give the wrong behavior of the application and errors, preferably literally, but we do not know anything about them. In addition to the description of the error, type

Cannot send session cookie - headers already sent by ...

They usually accurately indicate the location of the error and other important information. Also check the encoding of the file, whether some " UTF-8 с BOM " is installed there, which spoils everything (when working in notepad it is observed).

  • @Alex Krass, Thanks turned out to be "UTF-8 with BOM". I will continue to take a closer look - Edward

The fact is that $ _COOKIE ['name'] will not exist immediately after setcookie ('name', 'server'); And the next time you reload the page, it will be

  • I think the author has taken this into account and updated the page several times. Although, who knows ... - mountpoint
  • I know this, but it is not installed, because it gives an error - Eduardrst