note 31107 modified in function.addslashes by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 13:56:33 +0000 |
| Subject: | note 31107 modified in function.addslashes by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80010@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.
<?php
// 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
PS. I knew that I'd eventually be able to give something back to the site!
http://php.net/manual/en/function.addslashes.php