CodeIgniter Forums
Set cookie not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Set cookie not working (/showthread.php?tid=40399)



Set cookie not working - El Forum - 04-07-2011

[eluser]crimsun[/eluser]
Hello Friends,

I used the remember me functionality in my project while logged in site but its not store the cookies as we want to store only username and password i hereby paste my whole code please review that and suggest me any solution.

From controller
Code:
$this->admin_model->addRemerberme($insertData,$expire);

from one of the model
Code:
$this->auth_model->setAdminCookie('admin_uname',$insertData['admin_username'], $expire);

from one of the model
Code:
function setAdminCookie($name='',$value ='',$expire = '',$domain='',$path = '/',$prefix ='')
        {
                $domain=admin_url();
                // echo $domain; echo $value;
                $cookie = array('name'   =>$name,  'value'  => $value,  'expire' => $expire,    'domain' => $domain,     'path'   => $path,  'prefix' => $prefix, );
                set_cookie($cookie);
        }//End


This is from session library class
Code:
function _set_cookie($cookie_data = NULL)
        {
                // print_r($cookie_data);
                // echo "asdfasdf".$cookie_data;
        if (is_null($cookie_data))
        {
                //echo "asdfasdf".$cookie_data;
            $cookie_data = $this->userdata;
                //echo "asdfasdf".$cookie_data;exit;
        }

        // Serialize the userdata for the cookie
        $cookie_data = $this->_serialize($cookie_data);

        if ($this->sess_encrypt_cookie == TRUE)
        {
            $cookie_data = $this->CI->encrypt->encode($cookie_data);
        }
        else
        {
            // if encryption is not used, we provide an md5 hash to prevent userside tampering
            $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
        }

        // Set the cookie
        setcookie(
                    $this->sess_cookie_name,
                    $cookie_data,
                    $this->sess_expiration + time(),
                    $this->cookie_path,
                    $this->cookie_domain,
                    0
                );
    }

Please suggest me solution thanks in advance.


Set cookie not working - El Forum - 04-07-2011

[eluser]toopay[/eluser]
For convinience, you can put your code inside 'code' tag :
[ code ] Your Code [ / code ]
(remove any space)


Set cookie not working - El Forum - 04-07-2011

[eluser]InsiteFX[/eluser]
I am still working on this, it still needs protection and encryption!
But the functions do work! It is using native php cookies not CI.
It does use the CI serialize and unserialize methods.
Code:
// --------------------------------------------------------------------

    /**
     * fx_set_cookie()
     *
     * Description:
     *
     * @access    public
     * @return    void
     */
    public function fx_set_cookie($name, $data, $expire, $path, $domain)
    {
        $value = $this->_fx_serialize($data);
        setcookie($name, $value, $expire, $path, $domain);
    }

    // --------------------------------------------------------------------

    /**
     * fx_get_cookie()
     *
     * Description:
     *
     * @access    public
     * @return    void
     */
    public function fx_get_cookie($name)
    {
        $data = $_COOKIE[$name];
        return $cookie_data = $this->_fx_unserialize($data);
    }

    // --------------------------------------------------------------------

    /**
     * fx_delete_cookie()
     *
     * Description:
     *
     * @access    public
     * @return    void
     */
    public function fx_delete_cookie($name, $data, $expire, $path, $domain)
    {
        $value = $this->_fx_serialize($data);
        setcookie($name, $value, $expire, $path, $domain);
    }

    // --------------------------------------------------------------------

    /**
     * fx_serialize()
     *
     * Serialize an array
     *
     * This function first converts any slashes found in the array to a temporary
     * marker, so when it gets unserialized the slashes will be preserved
     *
     * @access    private
     * @param    array
     * @return    string
     */
    function _fx_serialize($data)
    {
        if (is_array($data))
        {
            foreach ($data as $key => $val)
            {
                if (is_string($val))
                {
                    $data[$key] = str_replace('\\', '{{slash}}', $val);
                }
            }
        }
        else
        {
            if (is_string($data))
            {
                $data = str_replace('\\', '{{slash}}', $data);
            }
        }

        return serialize($data);
    }

    // --------------------------------------------------------------------

    /**
     * fx_unserialize()
     *
     * This function unserializes a data string, then converts any
     * temporary slash markers back to actual slashes
     *
     * @access    private
     * @param    array
     * @return    string
     */
    function _fx_unserialize($data)
    {
        $data = @unserialize(strip_slashes($data));

        if (is_array($data))
        {
            foreach ($data as $key => $val)
            {
                if (is_string($val))
                {
                    $data[$key] = str_replace('{{slash}}', '\\', $val);
                }
            }

            return $data;
        }

        return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data;
    }

InsiteFX


Set cookie not working - El Forum - 04-07-2011

[eluser]InsiteFX[/eluser]
And here is how to use them.
fx_set_cookie:
Code:
// set cookie
// set expire time
$days = 30;
$expire = time() + 60 * 60 * 24 * $days;
$name = 'user_login';

$data = array(
    'user'    => 'guest',
    'user_id' => '2010'
);

$this->fx_set_cookie($name, $data, $expire, $path, $domain);

fx_delete_cookie:
Code:
// delete cookie!
$expire = time() - 60;
$this->fx_delete_cookie($name, $data, $expire, $path, $domain);

fx_get_cookie:
Code:
// get cookie -
$temp = $this->fx_get_cookie($name);

$user    = $temp['user'];
$user_id = $temp['user_id'];

InsiteFX