Cookies do not print after they are created on the same page

I created a "Test" with the name "cookie" with the value "one" with the following code on the page index.php

 <?php
 setcookie('test','one');
 echo $_COOKIE['test'];
 ?>

As soon as I open the index.php page, the cookie does not print, and as soon as I refresh the page, the cookie prints (it prints because of the cookie already created)

My question is: a cookie is created when I launch the first page of index.php and when I print a cookie it should be printed, then

for what reason does the cookie not get print the first time it starts itself?

+3
source share
4 answers

My question is: a cookie is created when I launch the index.php page for the first time myself, and as soon as I print the cookie, it should be printed, then

, cookie index.php.

cookie ?

, cookie , $_COOKIE $HTTP_COOKIE_VARS.

Source

, .

:

  • , cookie. , cookie , , cookie cookie. expire. cookie print_r($_COOKIE);.
  • cookie , . value - FALSE, setcookie, cookie . "" .
  • cookie FALSE cookie, . 0 FALSE 1 TRUE.
  • cookie PHP , . explode(), cookie . serialize() , .
0
setcookie('test','one');

http , , cookie.

Set-Cookie: test=one ...


cookie , , , :
Cookie: name=value; ...

- $_COOKIE, .

, , cookie, cookie, .

0

. :
setcookie ('test', 'one') β†’ index.php, cookie PHP script SET , PHP script , cookie .

, , cookie SET, , [ cookie ], READ PHP , .

0

What have you done? Created a cookie and printed it on one page, cookies could not be printed on the same page because the user loaded the next page. You can try to print the cookie on the next page.

  <?php
  echo $_COOKIE['test'];
  ?>

And you can save cookie on

$a=$_COOKIE['test'];

The purpose of storing it in the $ a variable is that the cookie value will remain the same even if the cookie is not set, but the value of $ a should not be changed.

0
source

All Articles