Php 使用CodeIgniter返回随机字母设置和获取COOKIE
我相信这是100%的错误,所以如果有人能纠正我,将不胜感激。 但在“索引”页面上,变量Php 使用CodeIgniter返回随机字母设置和获取COOKIE,php,codeigniter,cookies,codeigniter-2,Php,Codeigniter,Cookies,Codeigniter 2,我相信这是100%的错误,所以如果有人能纠正我,将不胜感激。 但在“索引”页面上,变量$venuedeets返回一个随机大写字母,当前为C 在事件上函数-我是否需要设置域,如果需要,我如何将其设置为基本url,还可以添加自定义值并将其附加到变量,如venuename=>$venue $cookie = array( 'name' => 'venue_details', 'value' => 'Hello', 'expire' => time()+86500, 'path'
$venuedeets
返回一个随机大写字母,当前为C
在事件上函数-我是否需要设置域,如果需要,我如何将其设置为基本url,还可以添加自定义值并将其附加到变量,如venuename=>$venue
$cookie = array(
'name' => 'venue_details',
'value' => 'Hello',
'expire' => time()+86500,
'path' => '/',
);
$this->input->set_cookie($cookie);
索引
$this->load->helper('cookie');
$this->input->cookie('venue_details', TRUE);
$cookie2 = get_cookie('venue_details');
$data['venuedeets'] = $cookie2['value'];
谢谢。问题是您误解了CI的get/set 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'];
这是您的错误:$cookie2
不是数组,而是字符串:cookie的内容。
如果你var\u dump($cookie2)
,事实上,你会得到:
string(5) "Hello"
您遇到的“随机值”问题很可能是因为,当尝试访问“值”索引时,php将索引(字符串)类型转换为整数(0),并获取字符串的0索引。在“Hello”的情况下,如果您有适当的错误杠杆来显示它,您将得到“H”-加上一个很好的警告
(*)如果您感到好奇,以下是访问cookie的方式(在system/core/Input.php中):
下面是如何检索的:
/**
* 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];
}
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);
}
以及如何创建:
/**
* 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];
}
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);
}
尝试使用$cookie2
的var_dump来查看它的实际设置。string(5)“hello”
如何读取@andrewsii在这种情况下,您可以直接执行赋值-$data['venuedeets']=$cookie2代码>感谢您的回复,但是,我仍然不明白它是否返回为字符串(5)“您好”
我如何阅读它?$cookie2=get_cookie(“场馆详细信息”)$数据['venuedeets']=$cookie2代码>是我所尝试的这并不能回答问题。您基本上只是复制了与问题中完全相同的代码,并添加了一些注释。