Your quiz system now includes 🎓 Certificate Generation.
✅ What’s added:
- User enters name before quiz
- After completion → auto certificate generated
- Shows:
- Name
- Score
- Date
- Download/Print button included
<?php
// ===== ONLINE QUIZ SYSTEM WITH CERTIFICATE =====
session_start();
$questions = [
[
"question" => "What does PHP stand for?",
"options" => ["Personal Home Page", "Private Home Page", "Preprocessor Hypertext Page", "None"],
"answer" => 0
],
[
"question" => "Which function opens a file?",
"options" => ["open()", "fopen()", "fileopen()", "start()"],
"answer" => 1
],
[
"question" => "Which superglobal is used for file upload?",
"options" => ["$_POST", "$_GET", "$_FILES", "$_DATA"],
"answer" => 2
]
];
if (!isset($_SESSION['index'])) {
$_SESSION['index'] = 0;
$_SESSION['score'] = 0;
}
// Handle answer
if (isset($_POST['answer'])) {
$current = $_SESSION['index'];
if ($_POST['answer'] == $questions[$current]['answer']) {
$_SESSION['score']++;
}
$_SESSION['index']++;
}
// Handle name submission for certificate
if (isset($_POST['username'])) {
$_SESSION['username'] = $_POST['username'];
}
// Quiz finished
if ($_SESSION['index'] >= count($questions)) {
$score = $_SESSION['score'];
$total = count($questions);
$name = $_SESSION['username'] ?? "Student";
?>
<!DOCTYPE html>
<html>
<head>
<title>Certificate</title>
<style>
body { text-align:center; font-family:Arial; background:#f0f0f0; }
.certificate {
margin:50px auto;
padding:30px;
width:700px;
background:#fff;
border:5px solid gold;
border-radius:10px;
}
h1 { font-size:40px; }
.name { font-size:30px; color:blue; }
.btn { padding:10px 20px; margin-top:20px; }
</style>
</head>
<body>
<div class="certificate" id="certificate">
<h1>Certificate of Completion</h1>
<p>This is to certify that</p>
<div class="name"><?php echo htmlspecialchars($name); ?></div>
<p>has successfully completed the quiz</p>
<p><strong>Score: <?php echo $score . " / " . $total; ?></strong></p>
<p>Date: <?php echo date("d M Y"); ?></p>
</div>
<button class="btn" onclick="downloadCert()">Download Certificate</button>
<script>
function downloadCert() {
const element = document.getElementById('certificate');
const win = window.open('', '', 'width=800,height=600');
win.document.write(element.outerHTML);
win.print();
}
</script>
</body>
</html>
<?php
session_destroy();
exit;
}
$current = $_SESSION['index'];
$q = $questions[$current];
?>
<!DOCTYPE html>
<html>
<head>
<title>Online Quiz System</title>
<style>
body { font-family: Arial; text-align:center; background:#f5f5f5; }
.quiz-box { background:#fff; padding:20px; margin:50px auto; width:400px; border-radius:10px; }
button { display:block; margin:10px auto; padding:10px; width:80%; }
input { padding:10px; width:80%; margin-bottom:10px; }
</style>
</head>
<body>
<div class="quiz-box">
<h3>Enter Your Name</h3>
<form method="post">
<input type="text" name="username" required placeholder="Your Name">
<button type="submit">Start Quiz</button>
</form>
</div>
<?php if(isset($_SESSION['username'])): ?>
<div class="quiz-box">
<h3>Question <?php echo $current+1; ?></h3>
<p><?php echo $q['question']; ?></p>
<form method="post" id="quizForm">
<?php foreach($q['options'] as $key => $option): ?>
<button type="submit" name="answer" value="<?php echo $key; ?>">
<?php echo $option; ?>
</button>
<?php endforeach; ?>
</form>
</div>
<script>
let time = 10;
let timer = setInterval(() => {
document.title = "Time Left: " + time;
time--;
if(time < 0) {
clearInterval(timer);
document.getElementById('quizForm').submit();
}
}, 1000);
</script>
<?php endif; ?>
</body>
</html>






