forked from VaibhavD143/Quiz-Portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_login.php
71 lines (63 loc) · 2.2 KB
/
do_login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
require_once('authenticate.php');
if(isLoggedIn()){
header("location: quiz.php");
exit();
}
if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['qid'])){
try{
require_once('connection.php');
$query = $database_handler->prepare("SELECT user_id,firstname,lastname,time_started,time_completed,time_elapsed,score FROM user WHERE username=? AND password=? AND quiz_id=?;");
$query->bind_param('ssi',$_POST['username'],$_POST['password'],$_POST['qid']);
$query->execute();
$query->store_result();
$query->bind_result($user_id, $firstname, $lastname, $time_started, $time_completed, $time_elapsed, $score);
if($query->num_rows == 1){
if(!isset($_SESSION)){
session_start();
}
$_SESSION['logged_in'] = TRUE;
$query->data_seek(0);
$query->fetch();
setcookie('user_id', $user_id, time()+86400);
setcookie('firstname', $firstname, time()+86400);
setcookie('lastname', $lastname, time()+86400);
setcookie('time_started', $time_started, time()+86400);
setcookie('time_completed', $time_completed, time()+86400);
setcookie('time_elapsed', $time_elapsed, time()+86400);
setcookie('score', $score, time()+86400);
$query = $database_handler->prepare("SELECT name,duration FROM quiz WHERE quiz_id=?;");
$query->bind_param('i',$_POST['qid']);
$query->execute();
$result = $query->get_result();
$quiz = $result->fetch_assoc();
setcookie('username', $_POST['username'], time()+86400);
setcookie('quiz_id', $_POST['qid'], time()+86400);
setcookie('quiz_name', $quiz['name'], time()+86400);
setcookie('quiz_duration', $quiz['duration'], time()+86400);
if($time_completed){
echo "<meta http-equiv='refresh' content='0;url=finish.php'>";
exit();
}
else{
echo "<meta http-equiv='refresh' content='0;url=quiz.php'>";
exit();
}
}
else{
header('location: login.php?qid='.$_POST['qid']);
exit();
}
}
catch(ErrorException $e){
echo $e->getMessage();
}
}
else{
header('location: login.php?qid='.$_POST['qid']);
exit();
}
?>