Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cookies for 20 min and check if they are expired

Tags:

php

cookies

I was thinking of a system that will allow users to post only 1 article per 20 min. I don't use a member system so I thought I could set a cookie for 20 min. And when user posts something check if cookie is set if yes show message like "Only 1 post per 20 min allowed" if it is not set than put stuff in database.

I'm relatively new to php and don't know how to set cookies, I tried looking at php.net manual on cookies, but it was too confusing for me. So can you please show how to set a secure cookie for 20 min and check if it is or is not set. Maybe you have Better suggestions that will work instead of cookies etc.

Thank You.

like image 554
Ilja Avatar asked Dec 02 '22 00:12

Ilja


2 Answers

See these functions:

  • setCookie
  • get cookie value

To set a cookie for 20 min you can do this:

setcookie("postedArticle", true, time() + (60 * 20)); // 60 seconds ( 1 minute) * 20 = 20 minutes

Check if cookie is set:

if(isset($_COOKIE['postedArticle']) && $_COOKIE['postedArticle'] == true)
{ 
    // IS SET and has a true value
}    
like image 122
Niels Avatar answered Jan 19 '23 01:01

Niels


Using cookies for that purpose makes no sense.
If it's registered users you are talking about, you have to store such information on the server side.
But if it's anonymous users, you can't prevent them from posting every second. To clear cookies from the browser is a matter of pressing just one button.

like image 32
Your Common Sense Avatar answered Jan 18 '23 23:01

Your Common Sense