Skip to content
Snippets Groups Projects
Commit 3ee4bdd4 authored by Virmith's avatar Virmith
Browse files

Initial login cookie setup

parent 4ed167e3
No related merge requests found
Pipeline #6902 failed with stages
/**
* Created by PhpStorm.
* User: kevin
* Date: 2/1/2019
* Time: 4:17 PM
*/
<?php
/**
* Created by PhpStorm.
* User: kevin
* Date: 10/22/2018
* Time: 10:47 PM
*/
require "dbConnect.php";
#Cookie Checking, Expire -> Redirect to Login, Not set -> Redirect to Login
if(isset($_COOKIE["loginCredentials"] )){
# Already set
list($idUser,$expire) = explode("|",$_COOKIE["loginCredentials"]);
if ($expire < time()){
#Expire
unset($_COOKIE['loginCredentials']);
setcookie('loginCredentials','',time()-3600,"/");
header("Location: admin/login.html");
die();
}
else {
$stmt = $conn -> prepare('SELECT * FROM `user` WHERE id = ?');
if (!$stmt) {
echo $conn->error;
return;
}
$stmt->bind_param("s", $idUser);
$stmt->execute();
$result = $stmt->get_result();
$results_array = array();
while ($row = $result->fetch_assoc()) {
$results_array[] = $row;
}
if (empty($results_array)){
#User invalid
unset($_COOKIE['loginCredentials']);
setcookie('loginCredentials','',time()-3600,"/");
header("Location: admin/login.html");
die();
}
}
}else{
#Not set
unset($_COOKIE['loginCredentials']);
setcookie('loginCredentials','',time()-3600,"/");
header("Location: admin/login.html");
die();
}
\ No newline at end of file
......@@ -28,11 +28,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST'){
while ($row = $result->fetch_assoc()) {
$results_array[] = $row;
}
var_dump($results_array);
}
if (!empty($results_array)) {
header("Location: admin/");
# set cookie and expire in 1 hour
$expire = time() + 3600;
setcookie("loginCredentials", (string) $results_array[0]['id'] . "|$expire", $expire, "/");
die();
} else {
header("Location: admin/login.html");
......
......@@ -21,9 +21,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST'){
if(!is_null($user)) {
header("Location: admin");
echo '<script language="javascript">';
echo 'alert("Username has already taken")';
echo '</script>';
echo "<script language='javascript'>";
echo "alert('Username has already taken')";
echo "</script>";
die();
//exit();
} else {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment