Professional Documents
Culture Documents
PHP Journal 1
PHP Journal 1
<?php
session_start();
include "connection.php";
if (isset($_SESSION['admin'])) {
header("location: adminhome.php");
if (isset($_POST['password'])) {
$adminpass = '$2y$10$8WkSLFcoaqhJUJoqjg3K8eWixJWswsICf7FTxehKmx8hpmIKYWqju';
if (password_verify($password , $adminpass)) {
$_SESSION['admin'] = "active";
header("Location: adminhome.php");
else {
?>
<html>
<head>
<title>PHP Quiz</title>
</head>
<body>
<header>
<div class="container">
<h1>PHP Quiz</h1>
</div>
</header>
<main>
<div class="container">
<h2>Enter Password</h2>
</div>
</main>
<footer>
<div class="container">
</div>
</footer>
</body>
</html>
<?php
session_start();
include "connection.php";
?>
<?php
if (isset($_SESSION['id'])) {
header("location: home.php");
?>
<?php
if (isset($_POST['email'])) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mysqli_num_rows($runcheck) > 0) {
$update = "UPDATE users SET played_on = '$played_on' WHERE email = '$email' ";
$row = mysqli_fetch_array($runcheck);
$id = $row['id'];
$_SESSION['id'] = $id;
$_SESSION['email'] = $row['email'];
header("location: home.php");
else {
if (mysqli_affected_rows($conn) > 0) {
if (mysqli_num_rows($run2) > 0) {
$row = mysqli_fetch_array($run2);
$id = $row['id'];
$_SESSION['id'] = $id;
$_SESSION['email'] = $row['email'];
header("location: home.php");
else {
}}
?>
<html>
<head>
<title>PHP-Quiz</title>
</head>
<body>
<header>
<div class="container">
<h1>PHP-Quiz</h1>
</div> </header>
<main>
<div class="container">
</div>
</main>
<footer>
<div class="container">
<?php
session_start();
if (isset($_SESSION['admin'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP-Quiz</title>
</head>
<body>
<header>
<div class="container">
<h1>PHP-Quiz</h1>
</header>
<main>
<div class="container">
<?php }
else {
header("location: admin.php");
?>
if (isset($_SESSION['admin'])) {
if(isset($_POST['submit'])) {
$qno = mysqli_num_rows($runcheck) + 1;
$query = "INSERT INTO questions(qno, question , ans1, ans2, ans3, ans4, correct_answer) VALUES
('$qno' , '$question' , '$choice1' , '$choice2' , '$choice3' , '$choice4' , '$correct_answer') " ;
if (mysqli_affected_rows($conn) > 0 ) {
else {
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<div class="container">
<h1>PHP-Quiz</h1>
</div>
</header>
<main>
<div class="container">
<h2>Add a question...</h2>
<p>
<label>Question</label>
</p>
<p>
<label>Choice #1</label>
</p>
<p>
<label>Choice #2</label>
</p>
<p>
<label>Choice #3</label>
</p>
<p>
<label>Choice #4</label>
</p>
<p>
<label>Correct answer</label>
<select name="answer">
</select>
</p>
<p>
</p>
<?php }
else {
header("location: admin.php");
?>
if (isset($_SESSION['admin'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP-Quiz</title>
</head>
<body>
<header>
<h1>PHP-Quiz</h1>
</div>
</header>
<table class="data-table">
<thead>
<tr>
<th>Q.NO</th>
<th>Question</th>
<th>Option1</th>
<th>Option2</th>
<th>Option3</th>
<th>Option4</th>
<th>Edit</th>
<?php
if (mysqli_num_rows($select_questions) > 0 ) {
$qno = $row['qno'];
$question = $row['question'];
$option1 = $row['ans1'];
$option2 = $row['ans2'];
$option3 = $row['ans3'];
$option4 = $row['ans4'];
$Answer = $row['correct_answer'];
echo "<tr>";
echo "<td>$qno</td>";
echo "<td>$question</td>";
echo "<td>$option1</td>";
echo "<td>$option2</td>";
echo "<td>$option3</td>";
echo "<td>$option4</td>";
echo "<td>$Answer</td>";
echo "</tr>";
?>
</table>
</body>
</html>
<?php }
else {
header("location: admin.php");
?>
if (isset($_SESSION['admin'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP-Quiz</title>
</head>
<body>
<header>
<div class="container">
<h1>PHP-Quiz</h1>
</div>
</header>
<table class="data-table">
<tr>
<th>Player Id</th>
<th>Email</th>
<th>Played On</th>
<th>Score</th>
</tr>
</thead>
<tbody>
<?php
if (mysqli_num_rows($select_players) > 0 ) {
$id = $row['id'];
$email = $row['email'];
$played_on = $row['played_on'];
$score = $row['score'];
echo "<tr>";
echo "<td>$id</td>";
echo "<td>$email</td>";
echo "<td>$played_on</td>";
echo "<td>$score</td>";
echo "</tr>";
?>
<?php }
else {
header("location: admin.php");
?>
session_start();
include "connection.php";
if (isset($_SESSION['id'])) {
$total = mysqli_num_rows($run);
?>
<html>
<head>
<title>PHP-Quiz</title>
</head>
<body>
<header>
<div class="container">
<h1>PHP-Quiz</h1>
</div>
</header>
<main>
<div class="container">
<ul>
</ul>
</div>
</main>
<footer>
<div class="container">
<?php }
else {
header("location: index.php");
?>
<?php
session_start();
include "connection.php";
if (isset($_SESSION['id'])) {
$qno = $_GET['n'];
if ($qno == 1) {
$_SESSION['quiz'] = 1;
else {
header('location: question.php?n='.$_SESSION['quiz']);
if (mysqli_num_rows($run) > 0) {
$row = mysqli_fetch_array($run);
$qno = $row['qno'];
$question = $row['question'];
$ans1 = $row['ans1'];
$ans2 = $row['ans2'];
$ans3 = $row['ans3'];
$ans4 = $row['ans4'];
$correct_answer = $row['correct_answer'];
$_SESSION['quiz'] = $qno;
$countqsn = mysqli_num_rows($runcheck);
$time = time();
$_SESSION['start_time'] = $time;
else {
?>
<?php
$totalqn = mysqli_num_rows($run);
?>
<html>
<head>
<title>PHP-Quiz</title>
</head>
<body>
<header>
<div class="container">
<h1>PHP-Quiz</h1>
</div>
</header>
<main>
<div class= "current">Question <?php echo $qno; ?> of <?php echo $totalqn; ?></div>
<ul class="choices">
</ul>
<br> <br>
<?php }
else {
header("location: home.php");
?>
session_start();
include "connection.php";
if (isset($_SESSION['id'])) {
?>
<?php if(!isset($_SESSION['score'])) {
header("location: question.php?n=1");
?>
<html>
<head>
<title>PHP Quiz</title>
</head>
<body>
<header>
<div class="container">
<h1>PHP Quiz</h1>
</div>
</header>
<main>
<h2>Congratulations!</h2>
echo $_SESSION['score'];
}; ?> </p>
</div>
</main>
</body>
</html>
<?php
$score = $_SESSION['score'];
$email = $_SESSION['email'];
?>
<?php }
else {
header("location: home.php");
?>
if(!isset($_SESSION['score'])) {
$_SESSION['score'] = 0;
}
if ($_POST) {
$newtime = time();
if ( $newtime > $_SESSION['time_up']) {
echo "<script>alert('time up');
window.location.href='results.php';</script>";
}
else {
$_SESSION['start_time'] = $newtime;
$qno = $_POST['number'];
$_SESSION['quiz'] = $_SESSION['quiz'] + 1;
$selected_choice = $_POST['choice'];
$nextqno = $qno+1;
if ($qno == $totalqn) {
header("location: results.php");
}
else {
header("location: question.php?n=".$nextqno);
}
<?php
if (isset($_GET['qno'])) {
$qno = mysqli_real_escape_string($conn , $_GET['qno']);
if (is_numeric($qno)) {
$query = "SELECT * FROM questions WHERE qno = '$qno' ";
$run = mysqli_query($conn, $query) or die(mysqli_error($conn));
if (mysqli_num_rows($run) > 0) {
while ($row = mysqli_fetch_array($run)) {
$qno = $row['qno'];
$question = $row['question'];
$ans1 = $row['ans1'];
$ans2 = $row['ans2'];
$ans3 = $row['ans3'];
$ans4 = $row['ans4'];
$correct_answer = $row['correct_answer'];
}
}
else {
echo "<script> alert('error');
window.location.href = 'allquestions.php'; </script>" ;
}
}
else {
header("location: allquestions.php");
}
}
?>
<?php
if(isset($_POST['submit'])) {
$question =htmlentities(mysqli_real_escape_string($conn , $_POST['question']));
$choice1 = htmlentities(mysqli_real_escape_string($conn , $_POST['choice1']));
$choice2 = htmlentities(mysqli_real_escape_string($conn , $_POST['choice2']));
$choice3 = htmlentities(mysqli_real_escape_string($conn , $_POST['choice3']));
$choice4 = htmlentities(mysqli_real_escape_string($conn , $_POST['choice4']));
$correct_answer = mysqli_real_escape_string($conn , $_POST['answer']);
$query = "UPDATE questions SET question = '$question' , ans1 = '$choice1' , ans2= '$choice2' , ans3 =
'$choice3' , ans4 = '$choice4' , correct_answer = '$correct_answer' WHERE qno = '$qno' ";
$run = mysqli_query($conn , $query) or die(mysqli_error($conn));
if (mysqli_affected_rows($conn) > 0 ) {
echo "<script>alert('Question successfully updated');
window.location.href= 'allquestions.php'; </script> " ;
}
else {
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP-Quiz</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<header>
<div class="container">
<h1>PHP-Quiz</h1>
<a href="index.php" class="start">Home</a>
<a href="add.php" class="start">Add Question</a>
<a href="allquestions.php" class="start">All Questions</a>
<a href="exit.php" class="start">Logout</a>
</div>
</header>
<main>
<div class="container">
<h2>Add a question...</h2>
<form method="post" action="">
<p>
<label>Question</label>
<input type="text" name="question" required="" value="<?
php echo $question; ?>">
</p>
<p>
<label>Choice #1</label>
</p>
<p>
<label>Choice #2</label>
</p>
<p>
<label>Choice #3</label>
<p>
<label>Choice #4</label>
</p>
<p>
<label>Correct answer</label>
<p>
<?php }
else {
header("location: admin.php");
}
?>
session_start();
session_destroy();
header("location: index.php");
?>
?>
START TRANSACTION;
--
-- Database: `php-kuiz`
--
-- --------------------------------------------------------
--
-- Table structure for table `questions`
--
CREATE TABLE `questions` (
--
-- Dumping data for table `questions`
--
INSERT INTO `questions` (`qid`, `qno`, `question`, `ans1`, `ans2`, `ans3`, `ans4`, `correct_answer`) VALUES
(9, 5, 'What is a correct way to add a comment in PHP?', '<!--…-->', '/*…*/', '*\\..\\*',
'<comment>…</comment>', 'b'),
(8, 3, 'The PHP syntax is most similar to:', 'Perl and C', 'VBscript', 'Javascript', 'none of these', 'a'),
(7, 2, 'How do you write \"Hello World\" in PHP?', 'echo \"Hello World\";', 'Document.Write(\"Hello
World\");', '\"Hello World\";', 'none of these', 'a'),
(6, 1, 'What does PHP stand for?', 'Personal Hypertext Processor', 'Private Home Page', 'Personal Home
Page', 'PHP: Hypertext Preprocessor', 'a'),
(5, 4, 'How do you get information from a form that is submitted using the "get" method?',
'$_GET[];', 'Request.Form;', 'Request.QueryString;', 'none of these', 'a'),
(10, 6, 'When using the POST method, variables are displayed in the URL:', 'True', 'False', 'Can\'t say',
'none of these', 'b'),
(11, 7, ' Which of the following function is used to get the size of a file?', 'fopen()', 'fread()', 'fsize()',
'filesize()', 'd'),
(12, 8, 'Which of the following is used to delete a cookie?', 'setcookie()', '$_COOKIE variable', 'isset()
function', 'none of the above', 'a');
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
--
-- Dumping data for table `users`
--
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
COMMIT;