Click here to Skip to main content
15,878,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is what index.php looks like, where the user registers:




HTML
<title>php
   

     <h3>Registration</h3>

     Login
     
     Password
     
     Repeat password
     
     Registration
     <a href="login.php">Already registered?</a>

 0) {
         $error = "A user with this login already exists!";
     }

     if (!$error) {
         session_unset();
         $_SESSION['username'] = $login;
         $query = "INSERT INTO `users` (`id`, `login`, `password`, `admin`, `points`) VALUES (NULL, '$login', '$password1', 0, 0);" ;
         mysqli_query($link, $query);
         echo "You have successfully created a user!";
         header("Location: test.php");
         exit();
     } else {
         echo $error;
     }
    
}
?>


test.php:




HTML
<title>Testing
   

Hello, $username<br>";
     } else {
         header("Location: index.php");
         exit();
     }
    
     $dateAndTime = date("d.m.Y H:i");
     if ($_SESSION[$username]['selected_questions'] === null) {
         $query = "SELECT * FROM questions ORDER BY RAND() LIMIT 5";
         $result = mysqli_query($link, $query);
         $elapsed_time = 0;
         $selected_questions = array();
         while ($row = mysqli_fetch_assoc($result)) {
             $selected_questions[] = $row;
         }
    
         $_SESSION[$username]['selected_questions'] = $selected_questions;
         $_SESSION[$username]['test_start_time'] = time();
         echo "<a>Start of test $dateAndTime, maximum test time 10 minutes</a>";
     }
?>


     $question<p></p>";
             echo "$answer1<br>";
             echo "$answer2<br>";
             echo "$answer3<br>";
         }
     }
     ?>
     Check answers
     ";
         $total_score = 0;
  
         $query = "SELECT * FROM questions";
         $result = mysqli_query($link, $query);
  
         while ($row = mysqli_fetch_assoc($result)) {
             $question_id = $row['id'];
             $correct_answer = $row['correct_answer'];
  
             if (isset($_POST["question_$question_id"])) {
                 $user_answer = $_POST["question_$question_id"];
  
                 if ($user_answer == $correct_answer) {
                     $total_score++;
                 }
             }
         }
         if ($elapsed_time > $max_allowed_time) {
             echo "The test time exceeded the allowed limit. The results were cancelled.<br>";
             $total_score = 0;
         }
         $update_query = "UPDATE users SET points = $total_score WHERE login = '$username'";
         mysqli_query($link, $update_query);
  
         echo "You scored $total_score out of a possible 5 points.";
  
         $_SESSION['points'] = $_SESSION['points'] + $total_score;
         $_SESSION[$username]['test_start_time'] = time();
         $elapsed_time = 0;
     }
     ?>

<a href="index.php">Return to home</a>


What I have tried:

Each user should be randomly selected 5 questions from the database. How do I record a user's questions in a session so that each user has their own questions and they don't change after the page is refreshed?
Posted
Updated 7-Nov-23 0:42am
v4
Comments
Richard Deeming 7-Nov-23 7:27am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
Richard Deeming 7-Nov-23 7:27am    
You also appear to be storing your users' passwords in plain text. Don't do that!
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

PHP even has built-in functions to help you do the right thing:
PHP: password_hash[^]
PHP: password_verify[^]

1 solution

I didn't see any "session_start()".
Quote:
Note: The session_start() function must be the very first thing in your document. Before any HTML tags.

PHP Sessions[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900