note 88803 added to function.setcookie

From: Date: Sun, 08 Feb 2009 21:03:43 +0000
Subject: note 88803 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-150393@lists.php.net to get a copy of this message
When working with encryption and setcookie( ) the decryption might fail in an seemingly random manner, making the cookie seem corrupt. This is due to the fact that php url-encodes the data before sending it to the client, and may therefore effectively be changing the ciphertext. A decryption of a changed ciphertext will render the plaintext unreadable. Due to the fact that this error only occurs when the ciphertext acctually contains characters needing encoding can make it very hard to identify. One way to solve this problem, is to implement the url-encoding in the script, by using urlencode( ) and urldecode( ). Prior to setting the cookie, use urlencode( ): <?php $ciphertext = $myEncryptionObject->encrypt( $plaintext ); $safeCiphertext = urlencode( $ciphertext ); setcookie( "myCookie", $safeCiphertext, 0, "", "", false, true); ?> And then using urldecode( ) prior to decryption of the ciphertext: <?php $safeData = $_COOKIE[ "myCookie" ]; $ciphertext = urldecode( $safeData ); $plaintext = $myEncryptionObject->decrypt( $ciphertext ); ?> ---- Server IP: 80.67.2.2 Probable Submitter: 92.244.28.41 ---- Manual Page -- http://www.php.net/manual/en/function.setcookie.php Edit -- https://master.php.net/note/edit/88803 Del: integrated -- https://master.php.net/note/delete/88803/integrated Del: useless -- https://master.php.net/note/delete/88803/useless Del: bad code -- https://master.php.net/note/delete/88803/bad+code Del: spam -- https://master.php.net/note/delete/88803/spam Del: non-english -- https://master.php.net/note/delete/88803/non-english Del: in docs -- https://master.php.net/note/delete/88803/in+docs Del: other reasons-- https://master.php.net/note/delete/88803 Reject -- https://master.php.net/note/reject/88803 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150393) next »