Please note, this is purely for educational purposes only and I ALWAYS protect my SQL queries no matter what they are used for, it would be a majorly bad idea not to! This question specifically focuses on $_GET requests and NOT $_POST requests.
So I have recently experienced a brute force attack on one of my accounts and thought it was probably for the best to check my scripts in case I had missed anything (Luckily the attack was unsuccessful).
I came across this script from one of my older accounts:
if(isset($_GET['del'])){
$user_id = $_SESSION['userid'];
$id_pho = $_GET['del'];
$sql = "DELETE FROM new_user_favourites WHERE `user_id` = '$user_id' AND `photo_id` = '$id_pho'";
$conn->query($sql);
}
I thought before I protected this script, that I would give it a bit of a test and found that even if I attempted to cause a problem to this script, using the URL, that the URL added encoded spaces %20 or speech marks %27 and these would not allow the script to process any further code?
For example, I tested:
mysite.co.uk?del='; UPDATE new_users SET account_type='1' WHERE email_address='test@test.com
Which should have sent:
$sql = "DELETE FROM new_user_favourites WHERE `user_id` = '' AND `photo_id` = ''; UPDATE new_users SET account_type='1' WHERE email_address='test@test.com'";
That would have given that user admin privileges to my site. Although because of the encoding, the result in the URL shows as:
mysite.co.uk?del=%27;%20UPDATE%20new_users%20SET%20account_type=%271%27%20WHERE%20email_address=%27test@test.com
And so the query would process like this, and fail:
$sql = "DELETE FROM new_user_favourites WHERE `user_id` = '' AND `photo_id` = '%27;%20UPDATE%20new_users%20SET%20account_type=%271%27%20WHERE%20email_address=%27test@test.com'";
Could this mean that my query is already protected from this kind of attack? Or is there a way around this for people who would wish to destroy my website? I've taken a look online for information about this but there doesn't seem to be anything?