I'm working on some quiz script, found one from internet, everything works like a charm, but i need to modify for security reasons. Actually i use $_GET , but i'm afraid about sql injections.
I'll let here all my script, and if someone can help me with suggestions, will be great. I tried so many things to make script non-vulnerable, but even .htaccess didn't helped me!
1.php quiz page with form
<?php
//Set question number
$number = (int) $_GET['n'];
//Get total number of questions
$query = "select * from questions";
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total=$results->num_rows;
// Get Question
$query = "select * from `questions` where question_number = $number";
//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$question = $result->fetch_assoc();
// Get Choices
$query = "select * from `choices` where question_number = $number";
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);
?>
<?php echo $question['question'] ?>
<form method="post" action="../p/process.php" class="table-responsive">
<?php while($row=$choices->fetch_assoc()): ?>
<button type="submit" class="btn btn-info btn-block" name="choice" value="<?php echo $row['id'] ?>"><?php echo $row['choice']; ?></button>
<?php endwhile; ?>
<input type="hidden" name="number" value="<?php echo $number; ?>" />
</form>
process.php page
//Check to see if score is set_error_handler
if (!isset($_SESSION['score'])){
$_SESSION['score'] = 0;
}
//Check if form was submitted
if($_POST){
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next=$number+1;
$total=10;
//Get total number of questions
$query="SELECT * FROM `questions` LIMIT 10";
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total=$results->num_rows;
//Get correct choice
$q = "select * from `choices` where question_number = $number and is_correct=1";
$result = $mysqli->query($q) or die($mysqli->error.__LINE__);
$row = $result->fetch_assoc();
$correct_choice=$row['id'];
//compare answer with result
if($correct_choice == $selected_choice){
$_SESSION['score']++;
}
if($number == $total){
header("Location: ../final.php");
exit();
} else {
header("Location: ../f/1.php?n=".$next."&score=".$_SESSION['score']);
}
}
As like i said, everyhing works fine, i'm ussing that script for 2 months already, but i'm afraid. Current URL looks like: f/1.php?n=1&score=1 Will be nice if someone can help me. Thank you