Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a secure way to structure a mysql_query in PHP

I have tried and tried to achieve an SQL injection by making custom queries to the server outside of firefox.

Inside the php, all variables are passed into the query in a string like this.

Note, by this stage, $_POST has not been touched.

mysql_query('INSERT INTO users (password, username) VALUES(' . sha1($_POST['password']) . ',' . $_POST['username'] . '));

Is that a secure way to make a change?

like image 551
Supernovah Avatar asked May 12 '10 11:05

Supernovah


2 Answers

You should definitely escape the username with mysql_real_escape_string.

Of course the best solution would be to use prepared statements. That way the separation of query syntax and data is made on the mysql API level.

And, as others pointed out, values should absolutely be surrounded with quotes. Especially the text ones.

like image 186
macbirdie Avatar answered Sep 29 '22 15:09

macbirdie


what you are doing there is dangerous since someone can send a POST request with an evil user name. you can either check every parameter and escape it, additionally you could use mysqli (http://php.net/manual/en/book.mysqli.php), and bind the parameters using prepare+bind.

the first step is good to avoid exploits on other users, while the second is good for your server side safety.

also check out this question: How do you prevent SQL injection in LAMP applications?

like image 28
Yonatan Karni Avatar answered Sep 29 '22 16:09

Yonatan Karni