使用CodeIgniter设置并获取COOKIE返回随机字母

Pur*_*ory 4 php cookies codeigniter codeigniter-2

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

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

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

指数

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

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

谢谢.

Dam*_*rsy 8

问题是你误解了CI的获取/设置cookie如何工作(*):

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

在您检索cookie的那一刻,通过传递其名称,您只能获得其内容,因为这是cookie的实际内容.同样,它不是序列化数组.

所以,来你的代码:

$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'];
Run Code Online (Sandbox Code Playgroud)

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

string(5) "Hello" 
Run Code Online (Sandbox Code Playgroud)

您遇到的"随机值"问题很可能是由于以下事实:当尝试访问'value'索引时,php将索引(字符串)强制转换为整数(0),并获取字符串的0索引.在"你好"的情况下,你会得到"H" - 加上一个很好的警告,如果你有适当的错误杠杆显示它.亲自看看吧.


(*)如果你很好奇,这里是访问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);
}
Run Code Online (Sandbox Code Playgroud)

以下是检索方式:

/**
 * 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];
}
Run Code Online (Sandbox Code Playgroud)

以及如何创建:

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);
}
Run Code Online (Sandbox Code Playgroud)