如何在PHP中使用rand函数设置随机cookie值 [英] How to set random cookie value using rand function in PHP

查看:224
本文介绍了如何在PHP中使用rand函数设置随机cookie值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PHP中使用rand函数设置随机cookie值

How to set random cookie value using rand function in PHP

我不希望在页面刷新后更改值(一旦分配了它)。直到cookie销毁

I don't want to change the value while page refreshed when it once assigned.. until cookie destroy

我的代码如下

<?php 
    global $random; 
    $random= rand(0, 9999999); 
    if(!isset($_COOKIE[$random])) {
        setcookie('user_cookie',$random, time() + (1), "/"); echo $_COOKIE[$random];
    }
    else {
        echo "Cookie '" . $_COOKIE[$random] . "' is set!<br>"; 
    }
    exit(); 
?> 


推荐答案

if(!isset($_COOKIE['lg'])) {
    setcookie('lg', rand(1,10000), time() + (86400 * 30), "/"); // 86400 = 1 day
}
echo $_COOKIE['lg'];

您可以检查是否未设置,然后进行设置。

You can check if it is not set then set it.

这篇关于如何在PHP中使用rand函数设置随机cookie值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆