Jump to content

Recommended Posts

Hey, I have this strange problem I never got before with other sites. The webserver I am using has magic_quotes enabled but I use a function for securing data anyway and this is in the function:

 

if(get_magic_quotes_gpc() != 1){
	$var = addslashes($var);
}

 

When I post data via a form and I use my function on the data it comes out with backslashes in front of certain characters and that is perfect but then when I do a query to insert the data into the database the data gets inserted with no backslashes.

 

For example lets say I just posted the variable "name" and I retrieved it on the other side like this:

 

$name = secure_var($_POST['name']);

 

If I echoed the variable "$name" on the next line of code it would have have backslashes in front of certain characters and then on the very next line of code if I inserted the variable "$name" into the database it would get added to the databse but without the blackslashes. What could be wrong? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/185613-data-not-escaped-in-the-database/
Share on other sites

the data gets inserted with no backslashes

 

That's the way it is supposed to be. The \ are only present in the query string so that the special characters don't break the SQL syntax.

 

If you had a case where the \ were present in the database, that would mean that you double-escaped the data.

u need to use myhsql_real_escape_string before u put it into the db hone

 

Yeah but if I echo a variable and let's say it outputs:

 

I\'m really tired

 

and then on the next line of code I insert it into the database and when I go and look in the database it has:

 

Im really tired

 

The backslash is gone. Why could this be happening? It should put in exactly what is in the variable .

Thanks for the fast replies. But when I used to add data into the database it would always have a "\" before certain characters and then when I retrieved the data and wanted to display it I would use stripslashes(). What is stripslashes for if the data comes out with all the slashes stripped already?

 

Yeah but if I echo a variable and let's say it outputs:

 

I\'m really tired

 

and then on the next line of code I insert it into the database and when I go and look in the database it has:

 

Im really tired

 

The backslash is gone. Why could this be happening? It should put in exactly what is in the variable .

 

I'm pretty sure it's:

I'm really tired

 

that's the way it's supposed to work. There's no point in storing escape characters in database.

 

Thanks for the fast replies. But when I used to add data into the database it would always have a "\" before certain characters and then when I retrieved the data and wanted to display it I would use stripslashes(). What is stripslashes for if the data comes out with all the slashes stripped already?

 

That was the wrong way of doing the escaping.

Why do I always see people using stripslashes() on data that comes out of the database all the time? This is so weird I always thought the data was stored with the slashes in it and then when displaying it you use stripslashes() to clean it up and make it look correct for output.

Why do I always see people using stripslashes() on data that comes out of the database all the time? This is so weird I always thought the data was stored with the slashes in it and then when displaying it you use stripslashes() to clean it up and make it look correct for output.

 

I don't know why you ALWAYS see it. Perhaps you're ONLY looking at code created by people who do it wrong way.

Think logically: what would be the point of storing these slashes just to strip them?

I must have always been running it twice before on other sites or else magic_quotes was enabled. So since the data is stored without the slashes that means when I do a query and I get some data from the database I can display it straight away without the need of using stripslashes()? Also when data is escaped and you put it into ANY sort of sql query (UPDATE, INSERT, SELECT, etc) and that sql query was to be shown in plain text the escaping would be gone? Thanks for this help.

Why do I always see people using stripslashes() on data that comes out of the database all the time?

 

There would be two reasons for that -

 

1) The data was double-escaped going into the database and now the extra \ needs to be removed, or

2) magic_quotes_runtime is ON and the data is being automatically escaped when it is retrieved from the database.

So when magic_quotes is turned on also does the same as stripslashes() when you get data from the database? Should I have a function which checks if magic_quotes is on and if it is it does not perform stripslashes on the data and if its turned off it should perform stripslashes on the data? That is just for if magic_quotes is ever turned off on the server or if the website changes hosting.

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.