PHP not read cookies

Hello,
I installed web server LAMP using pattern. But I have got problem: although cookies are set, php can not read it (After "Cookie is " is emptiness). What else I have to do to fix it. This is program code:

echo('<html>
<head>
<script>
document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC";');
echo('</script>
</head>');
<body>
$a1=$_COOKIE"username"];
$a=122;
ECHO('Cookie is: '. $a1);

I doubt this will help you :(, but I use

setcookie(..... 

(before I start the heading) and

isset($_COOKIE ....

Maybe not the same usage you are after. But in any case this works.

This is what I wanted to do:
measure screen width with java script (succeed ), write it in cookie using java script also (succeed), and then read cookie with PHP and set string value to have cookie-value (failed).

My question is how PHP to read cookie? I knew and used:

 $a=$_COOKIE'cookie_name']; 

with single quotes inside of brackets. I also tried (cos I saw somewhere) double quotes, but nothing works here.

Have you any idea what to do to make this works?

Thank you for answering my questions.

I am not sure I completely follow what you are doing, but one sets a cookie at the serving of the HTML file from server to browser), they then are (when allowed) stored by the browser. On next request from the browser to the same server they are uploaded to the server.

It looks to me that you expect the cookies to be available to the server at the moment they are set at the browser. This is not so IMHO, a HTTP request from the browser following the setting will make them available to the server script.

It is localhost where I do these scripts. Javascript sets cookies correctly and they are stored in browser, but PHP can not read it from browser and place it in stroing. I typed:


$a = $_COOKIE'username']; #this is correct
echo('Cookie is ' . $a); #result is: 'Cookie is '

and according written in comment - string $a is not written. I suppose PHP did not read cookie.

What is wrong so PHP can not read cookie from browser?

I will try again. Yous show all in one piece of script, but that will not work.

  1. the browser sends a HTTP request to the server, it requests for script.php and also sends cookies it might have for the domain;
  2. on the server script.php can check if there are any of the cookies it needs and use their values;
  3. script.php will set some cookies before it starts creating (and thus sending) a HTML file;
  4. on sending back the HTML created by script.php it will also send in the HTTP header the cookies then defined;
  5. the cookies will arrive at the browser which will store them ready for use when sending a new HTTP request and then we are back at point 1.

Thus when at point 3 the script creates a cookie date=2020-02-08,14:03, that is send at point 4 and stored at point 5 in the client.
When the browser the asks something new (going to point 1), then and only then the server can see the cookie date=2020-02-08,14:03, but understand that in the mean time it may be days later. The browser will have the date cookie from the last execution of script.php before it now asks again from the server.

In other words, setting a cookie for sending to the browser will not make it available using $_COOKIE’username’] in the same execution of that script. This cookie will only be available on a next execution of the PHP script (or another PHP script on the same server).

I’ve never heard of cookies used in this way, and my possibly inadequate Internet Search skills didn’t turn up any examples or documentation on using cookies to store and provide device display information.

Can you provide a reference?

Else,
I imagine that the following options are possible…

  • Manipulate the page layout client-side.
  • Use AJAX to pass the data from your Js to the Server for processing

TSU

Thank you for AJAX. I will try. This what I will explain worked in LAMP on Leap 15.0. And more: I wanted to write HOST where I have domain, to fix this on my domain, because it worked on localhost (LAMP).

This is what situation really is (my question were just example):

In *.inc.php file I “nested” javascript that measure screen width. Then, same javascript set this width in cookie. At the end, *.inc.php file that contains css structure use that cookie as data for screen-width.
*inc.php files I mentioned were called in php scripts with command require (not include, because reqzire are “stronger”).

And that is why I want php to communicate with javascript in order I want to.

Thank you all for your effort and advises.