php_king Posted April 24, 2012 Share Posted April 24, 2012 Hello all, I have a slight problem with my coding on a login script. The login script basically encrypts the users username, password and ID in a cookie with this function: function encrypt($text){ return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))); } and then when I try to retrieve the same value after setting with this function: function decrypt($text) { return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))); } It first gives me this output: t¨‘žÃŽ^kè¦Ü=yà¸ìŸ2Œ£· _¢Ó But when I refresh the page, it gives me the correct output that I wanted. From the looks of it, it looks like it's compressed. I cannot find anything on google about it and I feel lost as to what to do. Help me plz Quote Link to comment https://forums.phpfreaks.com/topic/261557-mcrypt-session-problem/ Share on other sites More sharing options...
Psycho Posted April 24, 2012 Share Posted April 24, 2012 There is no reason to store that information in a cookie. In any event, you say that the correct value is displayed after you refresh the page. If you are trying to set the cookie AND retrieve it on the same page load that is your problem. When you set a value in a cookie, you cannot retrieve it until the next page load. http://php.net/manual/en/function.setcookie.php Common Pitfalls: Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE); If that is your problem then there really is no problem. There is no reason to unencrypt the value you just encrypted since you have the original value in that same instance of that script. Quote Link to comment https://forums.phpfreaks.com/topic/261557-mcrypt-session-problem/#findComment-1340278 Share on other sites More sharing options...
php_king Posted April 24, 2012 Author Share Posted April 24, 2012 Thanks, that was my problem. I forced the page to refresh Quote Link to comment https://forums.phpfreaks.com/topic/261557-mcrypt-session-problem/#findComment-1340286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.