I have a web game which through an AJAX request accesses the php file below to save user's score to the database. How secure is this approach? In what way could someone hack this?
<?php
$db = "db name";//Your database name
$dbu = "db username";//Your database username
$dbp = "db user pass";//Your database users' password
$host = "localhost";//MySQL server - usually localhost
$dblink = mysql_connect($host,$dbu,$dbp);
$seldb = mysql_select_db($db);
if(isset($_GET['name']) && isset($_GET['score']))
{
//Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
$name = strip_tags(mysql_real_escape_string($_GET['name']));
$score = strip_tags(mysql_real_escape_string($_GET['score']));
$sql = mysql_query("INSERT INTO `$db`.`scores` (`id`,`name`,`score`) VALUES ('','$name','$score');");
if($sql)
{
echo 'Your score was saved. Congrats!';
}
else
{
echo 'There was a problem saving your score. Please try again later.';
}
}
else
{
echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.';
}
mysql_close($dblink); //Close off the MySQL connection to save resources.
?>