Welcome Guest, Not a member yet? Register   Sign In
set_cookie not setting cookie
#11

[eluser]internut[/eluser]
i'm able to grab the cookie, just not set it. I'm grabbing it using. Maybe it does have something to do with the order of things. I'm working in an admin area right now but if you want to reply with the XSS clean code that'd be cool for future reference for me and others.

Grabbing cookie via CI using:

Code:
$var = get_cookie('var');
#12

[eluser]Jim Higgins[/eluser]
Well, the xss clean is built into a few of the Code Igniter functions/classes. So, depending on how you end up setting and getting your cookie, you can run it several different ways. Here are some examples from the User Guide...

Code:
// Example 1
$this->input->cookie('cookie_name', TRUE); // <-- the TRUE optional parameter means you want CI to run xss_clean on it

// Example 2
$data = $this->input->xss_clean($data); // <-- here you could pass a variable containing your cookie data

// Example 3
$my_cookie = get_cookie('some_cookie', TRUE); // <-- the TRUE optional parameter means you want CI to run xss_clean on it

So, as you can see there are a few ways to run xss clean. You can also set CI to run it automatically globally throughout the site...

If you want the filter to run automatically every time it encounters POST or COOKIE data you can enable it by opening your application/config/config.php file and setting this:

Code:
$config['global_xss_filtering'] = TRUE;


There is some info on xss clean in the CI documentation in a few places...

http://ellislab.com/codeigniter/user-gui...input.html
http://ellislab.com/codeigniter/user-gui...urity.html
http://ellislab.com/codeigniter/user-gui...elper.html
#13

[eluser]internut[/eluser]
good stuff Jim!

i'm drowning in pagination right now.
#14

[eluser]shallway[/eluser]
To me the CI set_cookie function works fine.

Did you pass the correct unix time stamp as the to the $expire parameter?

Here is from php.net
Quote:The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

if you simply do this set_cookie( 'name', 'value', 86500, '', '' ) the cookie will not be set, because 86500 is NOT a valid unix time stamp. A valid unix time stamp should always be a 10 digits number. In other words if you do this :
Quote:set_cookie( 'name', 'value', time()+86500, '', '' );
it should work.

The problem is the PHP setcookie function seems not validating the number to be a 'valid' unix time stamp, it only checks if what's pass as the 'expire' parameter is a long number or not. That means you won't get any error message when you pass 86500 into it.

Hope this helps.
#15

[eluser]shallway[/eluser]
besides whats in the CI documentation, this
Quote:$cookie = array(
'name' => 'The Cookie Name',
'value' => 'The Value',
'expire' => '86500',
'domain' => '.some-domain.com',
'path' => '/',
'prefix' => 'myprefix_',
);

set_cookie($cookie);

is wrong. It should be time()+86500 NOT 86500. If you followed this example, the cookie will never be set.
#16

[eluser]shallway[/eluser]
oh, sorry I think I missed the definition of set_cookie, 86500 is actually fine.
#17

[eluser]Derek Allard[/eluser]
The function already adds time() to what you set.

Code:
$expire = time() + $expire;

Do you still feel this is a documentation bug shallway?
#18

[eluser]zbrox[/eluser]
I'm having the same issue and PHP's native implementation is not working for me. Anybody else experiencing these woes?
#19

[eluser]dootzky[/eluser]
actually, I think that CI will allow you to enter both combinations: with or without "time()+" as prefix

take a look under the hoode, in cookie_helper.php:
Code:
if ( ! is_numeric($expire))
        {
            $expire = time() - 86500;
        }
        else
        {
            if ($expire > 0)
            {
                $expire = time() + $expire;
            }
            else
            {
                $expire = 0;
            }
        }
#20

[eluser]zbrox[/eluser]
I figured it out. At least in my case it was a table with the wrong encoding. It was not UTF8 but latin and I was trying to save cyrillic chars in it. After changing the encoding, everything's fine.




Theme © iAndrew 2016 - Forum software by © MyBB