Welcome Guest, Not a member yet? Register   Sign In
Cookies doesn't work...?
#1

[eluser]Warz[/eluser]
EDIT: Go to the bottom of the page, I am now able to create cookie, but not delete.

Hi,

Why doesn't this work?
Code:
// Set Cookie
            if ($this->input->post('rememberme') == 1) // If user selected Remember Me
            {
                echo "You arrived.";
                $cookie = array(
                   'name'   => 'password',
                   'value'  => ''.sha1($this->input->post('password')).'',
                   'expire' => '86500',
                   'domain' => '.localhost',
                   'path'   => '/',
                   'prefix' => 'smx_',
                );

                set_cookie($cookie);
                echo get_cookie('smx_password');
                
            }

Output:

Quote:You arrived.
#2

[eluser]Narkboy[/eluser]
What are you expecting?

Do you actually get a cookie? Try searching for it on your comp - that will tell you if anything was placed, then you can work out what is not happening.

/B
#3

[eluser]Warz[/eluser]
Well I'm expecting it to output my cookie... I checked on my computer and it's not created as far as I can tell. I don't understand why is that..?
#4

[eluser]WanWizard[/eluser]
Check up on the way cookies work. You can't get a cookie before it is set (i.e. send to the browser by the server), the get_cookie() therefore only works from the next page request.

Other than that, the set_cookie() call looks ok, so the cookie should be created.
#5

[eluser]Warz[/eluser]
Solution:
Code:
// Set Cookie
            if ($this->input->post('rememberme') == 1) // If user selected Remember Me
            {
                echo "You arrived.";
                $cookie = array(
                   'name'   => 'password',
                   'value'  => ''.sha1($this->input->post('password')).'',
                   'expire' => '86500',
                   //'domain' => '.localhost',
                   'path'   => '/',
                   'prefix' => 'smx_',
                );

                set_cookie($cookie);
                echo get_cookie('smx_password');
                
            }
I had to remove 'domain' => '.localhost',

guess it only works with real domains?
#6

[eluser]WanWizard[/eluser]
You must be using Internet Exploder.

According to the cookie spec, domain names must contain dots to be considered valid. However, Firefox, Opera and Safari don't have a problem with cookies for localhost, but IE does...
#7

[eluser]Warz[/eluser]
Nope, I was using firefox...
#8

[eluser]Warz[/eluser]
Ok the set cookie works, but now I can't get cookies deleted now. As you might notice, I have tried pretty much everything... (the cookies are named ci_userid and ci_password)
Code:
/**
     * Logout
     */
    function index()
    {
        $this->load->helper('cookie');
        $this->session->sess_destroy();  
        delete_cookie('userid', '', '/', 'ci_');
        delete_cookie('password', '', '/', 'ci_');
        //setcookie('ci_userid', '', time() - 42000,'/');
        //setcookie('ci_userid', '', time()-10124, '/','localhost');
        //setcookie('ci_password', '', time()-10124, '/','localhost');
        //setcookie(name,value,expire,path,domain,secure)
        #delete_cookie('userid',false,'/','ci_');
        #delete_cookie('password',false,'/','ci_');
        #delete_cookie('ci_userid');
        #delete_cookie('ci_password');
        #delete_cookie('userid','localhost','/','ci_');
        #delete_cookie('password','localhost','/','ci_');
        $data['main_content'] = 'logout_successful';
        $this->load->view('includes/template',$data);
    }
#9

[eluser]WanWizard[/eluser]
Let me ask you a silly question: as you are clearly using the CI session class, why bother with using your own cookies when the session class does all this for you? And better?
#10

[eluser]Warz[/eluser]
You're right, I guess I should just change sess_expiration and work with that. Thanks Smile




Theme © iAndrew 2016 - Forum software by © MyBB