Customizing and retrieving a COOKIE using CodeIgniter returning a random letter

I am sure this is 100% wrong, so if someone can fix me. It would be very grateful. But on the "index" page, the $venuedeets variable returns a random capital letter currently C.

in events - I need to set the domain, if so, how can I set it as base_url, you can also add custom values ​​and attach them to variables like 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.

+4
source share
2 answers

The problem is that you misunderstand how the CI get / set cookie (*) works:

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

The moment you extract a cookie by passing its name, return only its contents , because this is the actual, real content of the cookie. Again, this is not a serialized array.

So, to 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 mistake: $cookie2 NOT an array, but STRING : the contents of the cookie. If you var_dump($cookie2) , you will get:

 string(5) "Hello" 

The problem of the "random value" that you are facing is most likely due to the fact that when you try to access the index "value", php prints the index (string) to an integer (0) and extracts the 0 index of the string. In the case of β€œHello”, you will receive β€œH” - plus a nice warning if you have the correct error lever to display it. See for yourself.


(*) If you are interested, here is how to access the cookie (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 this is how it 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 to create:

 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); } 
+8
source
 $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']; 
-2
source

Source: https://habr.com/ru/post/1486073/


All Articles