Jump to content

mysql_real_escape_string() VS stripslashes() VS htmlentities


robert_gsfame

Recommended Posts

I really confused with three of this function. Hope anyone can explain and give me some helps..

 

Let say i have this query

sprintf("INSERT INTO table(column1)VALUES('%s')",myql_real_escape_string($value));

 

And let say user put this as the value          "aaa", then it will come like this inside my database \"aaa\"

 

When retrieving the data i use stripslashes() to remove those slashes and i will have this

"aaa" correctly displayed...and i will use htmlentities(stripslashes($value)) to retrieve the data into my textbox

 

Is this correct and safe?? i really confused with all of this...

Link to comment
Share on other sites

Yes, thats how you would use to display it (whether in text box or on the page).

But if its actual html content, you want to avoid htmlentities, as that will convert charcters into an equivalent metacharacter;

i.e: & becomes &, < becomes < etc

 

so it really depends on the use of the field that dictates what functions to use.

 

 

Link to comment
Share on other sites

You need to use stripslashes when and only when gpc_magic_quotes is on. Other than that run mysql_real_escape_string when inserting into database, and no further processing is required when retrieving data (apart from htmlentities that is).

 

if(get_magic_quotes_gpc()) {
  $variable =  stripslashes($_POST['variable']);
}
$variable = mysql_real_escape_string($variable);
$sql = "... $variable ...";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.