cmor Posted June 19, 2010 Share Posted June 19, 2010 I'm trying to input Session variables into a mysql database. Everything inputs fine, but I am trying to make it more secure by filtering values through mysqli_real_escape_string(). However, it appears that most(all?) of the special characters are already escaped in the Session variables. Therefore when I pass it through mysqli_real_escape_string() I get double escaped characters. For example, If the user inputs 's Chris as their first name. I get the following output from the below code: echo $_SESSION['firstname']; echo '<br />'; echo mysqli_real_escape_string($db,$_SESSION['firstname']); Output: \'s Chris \\\'s Chris How do I overcome this problem and still make sure all my entries are secure? Link to comment https://forums.phpfreaks.com/topic/205281-session-variables-and-mysqli_real_escape_string-double-escapes-characters/ Share on other sites More sharing options...
swisse Posted June 19, 2010 Share Posted June 19, 2010 Try: $firstname = get_magic_quotes_gpc() ? mysqli_real_escape_string($db, trim(stripslashes($_POST['firstname']))) : trim($_POST['firstname']); before you input your data in your table. Link to comment https://forums.phpfreaks.com/topic/205281-session-variables-and-mysqli_real_escape_string-double-escapes-characters/#findComment-1074478 Share on other sites More sharing options...
cmor Posted June 19, 2010 Author Share Posted June 19, 2010 Try: $firstname = get_magic_quotes_gpc() ? mysqli_real_escape_string($db, trim(stripslashes($_POST['firstname']))) : trim($_POST['firstname']); before you input your data in your table. Thanks for the reply. That does solve the immediate problem. But if magic_quotes is turned off wouldn't I then be entering unfiltered data? Would this not be a better implementation: $firstname = get_magic_quotes_gpc() ? mysqli_real_escape_string($db, trim(stripslashes($_POST['firstname']))) :mysqli_real_escape_string($db, trim( $_POST['firstname'])); Link to comment https://forums.phpfreaks.com/topic/205281-session-variables-and-mysqli_real_escape_string-double-escapes-characters/#findComment-1074482 Share on other sites More sharing options...
swisse Posted June 20, 2010 Share Posted June 20, 2010 What you suggested is a better solution and to filter other special characters too. $firstname = get_magic_quotes_gpc() ? mysqli_real_escape_string($db, trim(stripslashes($_POST['firstname']))) : mysqli_real_escape_string($db, trim( $_POST['firstname'])); Link to comment https://forums.phpfreaks.com/topic/205281-session-variables-and-mysqli_real_escape_string-double-escapes-characters/#findComment-1074485 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.