php 如何将 cookie 设置为 20 分钟并检查它们是否已过期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8589500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:02:36  来源:igfitidea点击:

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

phpcookies

提问by Ilja

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.

我正在考虑一个允许用户每 20 分钟只发布 1 篇文章的系统。我不使用会员系统,所以我想我可以设置一个 20 分钟的 cookie。当用户发布某些内容时,请检查是否设置了 cookie,如果是,则显示“每 20 分钟仅允许 1 个帖子”之类的消息,如果未设置,则将内容放入数据库。

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.

我对 php 比较陌生,不知道如何设置 cookie,我尝试查看有关 cookie 的 php.net 手册,但这对我来说太混乱了。那么您能否展示如何设置 20 分钟的安全 cookie 并检查它是否已设置。也许您有更好的建议可以代替 cookie 等。

Thank You.

谢谢你。

回答by Niels

See these functions:

查看这些函数:

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

要将 cookie 设置为 20 分钟,您可以执行以下操作:

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

Check if cookie is set:

检查是否设置了 cookie:

if(isset($_COOKIE['postedArticle']) && $_COOKIE['postedArticle'] == true)
{ 
    // IS SET and has a true value
}    

回答by Your Common Sense

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.

为此目的使用 cookie 毫无意义。
如果您说的是注册用户,则必须将此类信息存储在服务器端。
但如果是匿名用户,你不能阻止他们每秒发帖。从浏览器中清除 cookie 只需按一个按钮。

回答by PRYM

As pointed out by others, cookie method to do this kind of job is useless. So encrypting is also a waste of resources here.

正如其他人所指出的,cookie 方法来做这种工作是没有用的。所以加密在这里也是一种资源浪费。

You should insert CAPTCHAvalidation if you want to prevent spams

如果您想防止垃圾邮件,您应该插入CAPTCHA验证

For what you are trying to do here is the ready to use code.

对于您在这里尝试做的是准备好使用的代码。

I have included encryption of cookie values so anyone can't change the value of the cookie.

我已经包含了 cookie 值的加密,因此任何人都无法更改 cookie 的值。

But still they can just delete the cookie which a normal user won't do if they see some encrypted values in them.

但是他们仍然可以删除普通用户如果在其中看到一些加密值就不会删除的 cookie。

<?php
$cookiename="yourcookiename";
$mysalt="secret salt";
function encrypt($text, $salt) 
{ 
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))); 
} 

function decrypt($text, $salt) 
{ 
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))); 
}

$read="false";
if(isset($_COOKIE[$cookiename]))
    {$read=decrypt($_COOKIE[$cookiename], $mysalt);
    }
if($read=='true')
{
//your stuff
setcookie($cookiename, encrypt("true", $mysalt), time()+20*60);
}
else {
//can't post
}

?>

回答by Sonal Khunt

set like

设置像

setcookie("TestCookie", $value, time()+1200);

check after 20 min if it will expire than it work else not..

20 分钟后检查它是否会过期而不是工作,否则不会..