Page 1 of 1

Is this a good way to store multiple values in a cookie?

Posted: Sat Sep 26, 2009 9:05 pm
by gimpact
Hello,

I have a login script, when the user logs in a cookie is created with email ID encrypted. If cookie cant be created then a session is created to store the information. I have two types of user in my site. So along with encrypted email id, i also want to store the information about the type of user. Here is what I am doing. Please let me know is this good? or is there any other better way?

Code: Select all

// Create cookie and store login information
 
            $cookie_name = "userlogin"; // Cookie name
            $loginkey = md5($email);    // Encrypted login key
            $userType = "candidate";    // User type
            $cookie_data = $loginkey."+|+".$userType;   // Cookie data        
            $cookie_time = time()+60*60*60;         // 1 hour
            $cookie_path = "/";                     // All subfolders
            $cookie_domain = "domain.com";  // All subdomains
 
            setcookie($cookie_name, $cookie_data, $cookie_time, $cookie_path, $cookie_domain);
            
            // When cookie creation unsuccessfull
            if(!setcookie){
                //Store login data in session
                session_start();
                $_SESSION['login_key'] = md5($email);
                $_SESSION['auth'] = 'candidate';
                $_SESSION['first_name'] = $getAccount3['first_name'];
            }
Here is how I am trying to retrieve the values for the $cookie_data

Code: Select all

list($loginkey,$author) = split("+|+",$cookie_data);
I dint try this yet, so I have no idea if this will really work but first i would like to know if this is a good way to do what I am trying to do.

Thank you,

Re: Is this a good way to store multiple values in a cookie?

Posted: Sat Sep 26, 2009 10:11 pm
by josh
serialize( array( $val1, $val2, $val3 ) )

session is always more secure and you don't have to serialize ( PHP does it behind the scenes )

Re: Is this a good way to store multiple values in a cookie?

Posted: Sat Sep 26, 2009 10:19 pm
by gimpact
Thank you,