Hi all,
I have come across a very common problem, or should I say, I am in a dilemma about something very common. That’s about the use of addslashes(), stripslashes & mysql_real_escape_string . Now I have a couple of forms. The first one takes user input, validates it & if everything is all right, takes them to the second form, which is the Confirmation form. As soon as they confirm that the data on this form is right, the info is posted to another page, which inserts the data in Mysql Database. I am using PHP to do this. I am using a function & passing this data through that function before it gets added.
function sql_quote_no( $value )
{
if( get_magic_quotes_gpc() )
{
$value = stripslashes( $value );
}
if( function_exists( "mysql_real_escape_string" ) )
{
$value = mysql_real_escape_string( $value );
}
else
{
$value = addslashes( $value );
}
return $value;
}
The above is the function that I use. Now say I have entered the following message initially in the form:
a’;DELETE FROM table_a WHERE m_id = 1""
I want to allow my visitors to use " & ’ in the form & even show it out the same way when I pull info from the DB, but want to maintain it secure in the process. If I echo out the message after passing it through the function above & just before inserting it into the DB, it shows me the following:
a’;DELETE FROM table_a WHERE m_id = 1""
But when I visit my database, I see the message in the DB as:
a’;DELETE FROM table_a WHERE m_id = 1""
It does not show me the slashes. Which means, just before adding into DB, it is adding slashes to it but when it added that info into the DB, it removed the slashes automatically. 2 questions:
- Now does this make it secure? (I don’t think so)
- Also is the DB supposed to contain the slash () from our addslashes function? I mean the current data in DB is great considering that I don’t have to worry about the search results when I implement a search function in the website, but is the data supposed to contain the slashes & I can show the data out by using stripslashes?
Please let me know point by point, the correct answer. My whole website security depends upon this info & I really want to understand the pros & cons of the method that I am using above. I tried php.net & other sites via google, but this question does not seem to be answered or the answers were not clear.
Would really appreciate an early reply. Thank you in advance.