blob: 9fcdc397aeb422a66ad1fd2f00337afdfe8a8a52 [file] [log] [blame]
Andreu0d72bd62019-09-17 23:31:14 +02001<?php
Andreu2457e402019-09-22 00:52:41 +02002 require '../credentials.php';
Andreuabbcb7e2019-09-21 18:22:14 +02003 require 'utils.php';
Andreuefe66eb2019-09-21 18:41:49 +02004
Andreu09b8b052019-09-21 21:47:20 +02005 // Set the 'user' POST and COOKIE variable
Andreu20cbd1d2019-09-22 00:00:57 +02006 $user = '';
7 if (isset($_POST['user'])) $user = $_POST['user'];
8 else if (isset($_COOKIE['user'])) $user = $_COOKIE['user'];
9 else {
10 header("Location: ../index.php");
Andreu09b8b052019-09-21 21:47:20 +020011 die();
12 }
13
Andreu20cbd1d2019-09-22 00:00:57 +020014 // Check if password is correct
15 $query_password = "SELECT password FROM users WHERE id=".$user;
16 $real_password = query($query_password)->fetch_row()[0];
17
18 // Prioritize input rather than memory
19 $password = '';
20 if (isset($_POST['password'])) $password = $_POST['password'];
21 else if (isset($_COOKIE['password'])) $password = $_COOKIE['password'];
22
23 // Redirect if wrong
24 if ($real_password != "" && $real_password != md5($password)) {
Andreu543e70c2019-09-22 14:08:49 +020025 // Forget cookies
26 setcookie('user', '', -1, "/");
27 setcookie('password', '', -1, "/");
28
Andreu20cbd1d2019-09-22 00:00:57 +020029 header("Location: ../index.php?wrongpassword=1");
30 die();
31 }
32
33 // Save variables as cookies
34 setcookie('user', $user, time() + (86400 * 10), "/");
Andreu543e70c2019-09-22 14:08:49 +020035 if ($real_password != "") setcookie('password', md5($password), time() + (86400 * 10), "/");
Andreu20cbd1d2019-09-22 00:00:57 +020036 else setcookie('password', '', -1, "/");
37
38 // Success, proceed to main page
Andreu09b8b052019-09-21 21:47:20 +020039 header("Location: ../main.php");
Andreu0d72bd62019-09-17 23:31:14 +020040?>