note 31107 added to function.addslashes

From: Date: Wed, 09 Apr 2003 23:46:52 +0000
Subject: note 31107 added to function.addslashes
Groups: php.notes 
Request: Send a blank email to php-notes+get-46433@lists.php.net to get a copy of this message
re: problem with mcrypt, addslashes and mysql Here is my solution to the problem of characters from mcrypt creating issues with mysql calls (due to characters which aren't cleaned up by addslashes). Solution: simply convert your encryption string to hex, then back to binary when you are ready to decrypt. ie. $encrypted = addslashes($string); $encrypted = bin2hex($encrypted); ... then: $decrypted = hex2bin($encrypted); $decrypted = stripslashes($decrypted); where hex2bin() is: function hex2bin($hexdata) { $bindata=""; for ($i=0;$i<strlen($hexdata);$i+=2) { $bindata.=chr(hexdec(substr($hexdata,$i,2))); } return $bindata; } One word of caution: this will increase the length of your initial data string, so you will need to increase the field length for your mysql database. Cheers, Phil www.internetprojectmanagers.com PS. I knew that I'd eventually be able to give something back to the site! -- http://www.php.net/manual/en/function.addslashes.php http://master.php.net/manage/user-notes.php?action=edit+31107 http://master.php.net/manage/user-notes.php?action=delete+31107 http://master.php.net/manage/user-notes.php?action=reject+31107

« previous php.notes (#46433) next »