Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 浏览器退出时正在销毁Cookie_Php_Cookies_Session_Setcookie - Fatal编程技术网

Php 浏览器退出时正在销毁Cookie

Php 浏览器退出时正在销毁Cookie,php,cookies,session,setcookie,Php,Cookies,Session,Setcookie,我目前正在我的网站上玩cookie,我做的第一件事是检查用户是否有cookie,如果他们没有,我会显示一个包含3个选项的菜单,如果他们单击它会创建cookie,但是如果退出浏览器cookie会被销毁,这是我的代码 function createRandomId() { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime() * 1000000); $i = 0; $uniqu

我目前正在我的网站上玩cookie,我做的第一件事是检查用户是否有cookie,如果他们没有,我会显示一个包含3个选项的菜单,如果他们单击它会创建cookie,但是如果退出浏览器cookie会被销毁,这是我的代码

function createRandomId() {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime() * 1000000);
    $i = 0;
    $unique = '';
    while ($i <= 7) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $unique = $unique.$tmp;
        $i++;
    }
    return md5($unique);
}

function index() {
    // $data is the array of data that is passed to views, setup it up
    $data = array();
    // We need to setup the cookie that will be used site, this will be used to cross reference
    // The user with the options they have selected, to do this we first need to load the session model
    // Check if the user has a cookie already, if they it means they have been to the site in the last 30 days.
    if(!isset($_COOKIE['bangUser'])) {
        // Get createRandomId() method and return a unique ID for the user
        $unique = '';
        // Setting the cookie, name = bangUser, the cookie will expire after 30 days
        setcookie("bangUser", $unique, time() + (60*60*24*30));
        $data['firstTime'] = TRUE;
    } else {
        $data['notFirstTime'] = TRUE;
    }

    // Load the view and send the data from it.
    $this->load->view('base/index', $data); 
}


function createCookie() {
    // Function gets called when the user clicks yes on the firstTime menu.
    // The purpose of this function is to create a cookie for the user.
    // First we'll give them a unique ID
    $unique = $this->createRandomId();
    // With the unique ID now available we can set our cookie doing the same function as before
    setcookie("bangUser", $unique, time() + (60*60*24*30));
    // Now that the cookie is set we can do a 100% check, check that cookie is set and if it is redirect to
    // to the homepage
    if(isset($_COOKIE['bangUser'])) {
        redirect('welcome');
    }
}
函数createRandomId(){
$chars=“abcdefghijkmnopqrstuvxyzo23456789”;
srand((双)微时间()*1000000);
$i=0;
$unique='';
而($i load->view($base/index',$data);
}
函数createCookie(){
//当用户在第一次菜单上单击yes时,函数被调用。
//此函数的目的是为用户创建cookie。
//首先,我们将给他们一个唯一的ID
$unique=$this->createRandomId();
//现在有了唯一的ID,我们可以设置cookie执行与以前相同的功能
setcookie(“bangUser”,$unique,time()+(60*60*24*30));
//现在cookie已经设置好,我们可以进行100%检查,检查cookie是否已设置,以及它是否重定向到
//浏览网页
如果(isset($_COOKIE['bangUser'])){
重定向(“欢迎”);
}
}

基本上,index()函数会进行检查,createCookie会创建一个新的cookie,有人会看到任何问题吗?

您需要将setcookie($path)的第四个参数设置为您网站的绝对路径。例如:

setcookie("bangUser", $unique, time() + (60*60*24*30), "/");

在createCookie函数中,调用setCookie不会立即将值添加到$\u COOKIE superglobal中-此数组仅在发出请求时保存当前的COOKIE(但您仍可以在数组中存储新的COOKIE值)


此外,如果您希望在浏览器退出时销毁会话cookie,请为过期时间指定null。或者,只使用PHP的内置会话。

您应该只使用
uniqid()
而不是创建自己的、较慢的
createrandomid()
函数。