Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting and getting COOKIE with CodeIgniter returning random letter

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 $venuedeets returns a random capitalized letter currently C.

on events function - 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.

$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.

like image 206
Purgatory Avatar asked Mar 23 '23 14:03

Purgatory


1 Answers

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

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.

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

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: $cookie2 is NOT an array, but a STRING : the cookie's content. If you var_dump($cookie2), in fact, you get:

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.


(*) If you're curious, here's how the cookie is accessed (in 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);
}
like image 91
Damien Pirsy Avatar answered Apr 05 '23 22:04

Damien Pirsy