php 使用返回随机字母的 CodeIgniter 设置和获取 COOKIE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17093275/
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-25 12:14:38  来源:igfitidea点击:

Setting and getting COOKIE with CodeIgniter returning random letter

phpcodeignitercookiescodeigniter-2

提问by Purgatory

I am sure this is 100% wrong so if someone could correct me It would be greatly appreciated. But on the 'index' page it the variable $venuedeetsreturns a random capitalized letter currently C.

我确信这是 100% 错误的,所以如果有人能纠正我,将不胜感激。但是在“索引”页面上,该变量$venuedeets返回一个当前为 C 的随机大写字母。

on eventsfunction - do I need to set the domain, if so how do I set it as base_url, also is it possible to add custom values and attach them to variables such as venuename => $venue.

关于事件函数 - 我是否需要设置域,如果需要,我如何将其设置为 base_url,是否可以添加自定义值并将它们附加到诸如venuename => $venue.

$cookie = array(
'name' => 'venue_details',
'value' => 'Hello',
'expire' => time()+86500,
'path'   => '/',
);
$this->input->set_cookie($cookie);

index

指数

$this->load->helper('cookie');

$this->input->cookie('venue_details', TRUE);
$cookie2 = get_cookie('venue_details');
$data['venuedeets'] = $cookie2['value'];

Thanks.

谢谢。

回答by Damien Pirsy

The problem is you're misunderstanding how the CI's get/set cookie works (*):

问题是您误解了 CI 的获取/设置 cookie 的工作原理 (*):

when you set a cookie (either using $this->input->set_cookie()or the equivalent helper function) you pass it an array, but internally that array is used to assign the properties that create the cookie. You're not just serializing an array into a file, you're creating a cookie with a name, set an expiration, and provide some content.

当您设置 cookie(使用$this->input->set_cookie()或等效的辅助函数)时,您向它传递一个数组,但在内部该数组用于分配创建 cookie 的属性。您不只是将数组序列化为文件,而是创建一个带有名称的 cookie、设置过期时间并提供一些内容。

The moment you retrieve the cookie, by passing its name, you get backonly its content, because that's the actual,real content of the cookie. Again, it's not a serialized array.

在您检索 cookie 的那一刻,通过传递其名称,您只会返回其内容,因为那是 cookie 的实际内容。同样,它不是序列化数组。

So, coming at your code:

所以,来到你的代码:

$this->load->helper('cookie');
$this->input->cookie('venue_details', TRUE);
// this line is useless: the method returns a cookie, filtered for XSS, but you're 
// assigning it to nothing
$cookie2 = get_cookie('venue_details');
// this your "real" cookie access method
$data['venuedeets'] = $cookie2['value'];

Here is your error: $cookie2is NOT an array, but a STRING: the cookie's content. If you var_dump($cookie2), in fact, you get:

这是您的错误:$cookie2不是数组,而是STRING:cookie 的内容。如果你var_dump($cookie2),其实,您可以:

string(5) "Hello" 

The "random value" problem you encounter is most likely due to the fact that, when trying to access the 'value' index, php typecasts the index (a string) to an integer (0), and fetches the 0 index of the string. In the case of "Hello", you would get "H" - plus a nice Warning if you have the proper error lever for showing it. See it for yourself.

您遇到的“随机值”问题很可能是因为在尝试访问“值”索引时,php 将索引(字符串)类型转换为整数(0),并获取字符串的 0 索引. 在“你好”的情况下,你会得到“H” - 如果你有适当的错误杠杆来显示它,再加上一个很好的警告。自己看吧。



(*)If you're curious, here's how the cookie is accessed(in system/core/Input.php):

(*)如果您好奇,这里是访问 cookie 的方式(在 system/core/Input.php 中):

/**
* Fetch an item from the COOKIE array
*
* @access   public
* @param    string
* @param    bool
* @return   string
*/
function cookie($index = '', $xss_clean = FALSE)
{
    return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
}

And here's how is retrieved:

这是如何检索的:

/**
 * Fetch from array
 *
 * This is a helper function to retrieve values from global arrays
 *
 * @access  private
 * @param   array
 * @param   string
 * @param   bool
 * @return  string
 */
function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
{
    if ( ! isset($array[$index]))
    {
        return FALSE;
    }

    if ($xss_clean === TRUE)
    {
        return $this->security->xss_clean($array[$index]);
    }

    return $array[$index];
}

And how's created:

以及如何创建:

function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE)
{
    if (is_array($name))
    {
        // always leave 'name' in last place, as the loop will break otherwise, due to $$item
        foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'name') as $item)
        {
            if (isset($name[$item]))
            {
                $$item = $name[$item];
            }
        }
    }

    if ($prefix == '' AND config_item('cookie_prefix') != '')
    {
        $prefix = config_item('cookie_prefix');
    }
    if ($domain == '' AND config_item('cookie_domain') != '')
    {
        $domain = config_item('cookie_domain');
    }
    if ($path == '/' AND config_item('cookie_path') != '/')
    {
        $path = config_item('cookie_path');
    }
    if ($secure == FALSE AND config_item('cookie_secure') != FALSE)
    {
        $secure = config_item('cookie_secure');
    }

    if ( ! is_numeric($expire))
    {
        $expire = time() - 86500;
    }
    else
    {
        $expire = ($expire > 0) ? time() + $expire : 0;
    }

    setcookie($prefix.$name, $value, $expire, $path, $domain, $secure);
}

回答by skumar

$this->load->helper('cookie');
$this->input->cookie('venue_details', TRUE);
// this line is useless: the method returns a cookie, filtered for XSS, but you're 
// assigning it to nothing
$cookie2 = get_cookie('venue_details');
// this your "real" cookie access method
$data['venuedeets'] = $cookie2['value'];