P
P
Para_Lapok2021-05-24 06:52:13
PHP
Para_Lapok, 2021-05-24 06:52:13

How to deal with cookies using php?

Good afternoon.
I have a button on my site that causes the roulette wheel to appear. In the appearing block with a roulette there is an input type="submit" button, which sends data via ajax and is processed in the send.php file.
The problem is this: after clicking on this button, which spins the roulette wheel and sends data, the button that calls the roulette wheel for 30 days disappears. I decided to do this: in the send.php file, I create a cookie:

$cookie_date = date('d.m.Y');
setcookie("cookie_wheel", $cookie_date, time() + 9331200000);

With a lifespan of 30 days. In the file where the button code is written, I set the condition:
<? if(!isset($_COOKIE['cookie_wheel'])){ ?>
<div style="height: 50px;" class="for_hidden"></div>
<div class="main for_hidden">
  <div class="first">
    <span>
      <img src="..." width="30">
    </span>
  </div>
  <div class="second">
    <span><b>Испытай свою удачу - получи подарок</b></span>
  </div>
  <div class="third">
    <span>
      <svg>...</svg>
    </span>
  </div>
</div>
<? }
else { } ?>

The appearance of the wheel generates jQuery, but I think it's useless now, there is no problem with this. And here I track the cookie in the console:
I go to the site - there is no cookie. I refresh the page - there is no cookie. I spin the roulette, I refresh the page - there is a cookie. But despite the fact that there is a cookie, the button to call the roulette still appears ...
I have never worked with cookies, I just started now and maybe I don’t quite understand something, hence the difficulties. Tell me, please, how can this be solved?

PS: I informed the customer that if the user himself clears the cookie in the browser history, the roulette wheel will reappear. I got the answer that this is not a problem.

PSS: I tried replacing isset with empty in the condition - it didn't help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Para_Lapok, 2021-05-28
@Para_Lapok

In general, I solved the problem with the help of js.

A
Andrey Nassonov, 2021-05-24
@AndryuhaPro

Hello.
You seem to have an error in your code.
The function setcookie()does not take a value The second parameter y is the lifetime of the cookie - the function does notdate('d.m.Y')
setcookie()time()date()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question