note 31107 modified in function.addslashes by philip

From: Date: Tue, 20 Jul 2004 17:15:16 +0000
Subject: note 31107 modified in function.addslashes by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-73709@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 PS. I knew that I'd eventually be able to give something back to the site! --was-- 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://php.net/manual/en/function.addslashes.php

« previous php.notes (#73709) next »