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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gimpact
Forum Commoner
Posts: 65
Joined: Tue Jun 16, 2009 11:08 pm

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

Post 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,
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

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

Post 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 )
gimpact
Forum Commoner
Posts: 65
Joined: Tue Jun 16, 2009 11:08 pm

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

Post by gimpact »

Thank you,
Post Reply