Took a bit longer than expected removing the current cookies but replaced cookies (uid & pass) with sessions (uid & pass)
There is a remember me button with a new cookie (token) if you can think of any improvements everything is working 100% the logout on all devices is a nice bonus
html add
<input type="checkbox" name="rememberme" value="rememberme" /> Remember Me
in functions
replace cookie functions with
<?php
// Obtain / Create Cookie Function
function logincookie($id, $password, $secret, $updatedb = 1, $expires = 0x7fffffff)
{
global $pdo;
// Retrieving Cookie Information
$hash = $id . $secret . $password . getip() . $secret;
// Cookie Information Encryption
$hash = password_hash($hash, PASSWORD_BCRYPT);
// Declaration And Sending Cookie
setcookie("token", $hash, $expires, "/");
if ($updatedb) {
$stmt = $pdo->run("UPDATE users SET last_login=? WHERE id=?", [get_date_time(), $id]);
$token = $pdo->run("UPDATE users SET token=? WHERE id=?", [$hash, $id]);
}
}
// Cookie Destruction function
function logoutcookie()
{
session_start();
// Declaration And Sending Cookie
// As Set to NULL ==> Destruction
setcookie("token", null, time(), "/");
// Destroy Sessions
unset($_SESSION['uid']);
unset($_SESSION['pass']);
// session_destroy();
}
function setsess($id, $password)
{
session_start();
$_SESSION['uid'] = $id;
$_SESSION['pass'] = $password;
}
find unset($GLOBALS["CURUSER"]);
under add (this is where improvement could be made)
// Check The Cookies and Sessions details
if (isset($_COOKIE['token'])) {
$res1 = $pdo->run("SELECT id FROM users WHERE token=?",[$_COOKIE['token']]);
$row = $res1->fetch(PDO::FETCH_ASSOC);
$id = $row['id'];
$_SESSION["uid"] = $id;
}
if (!isset($_SESSION['uid'])) {
logoutcookie();
return;
}
//Get User Details And Permissions
$res = $pdo->run("SELECT * FROM users INNER JOIN groups ON users.class=groups.group_id WHERE id=$_SESSION[uid] AND users.enabled='yes' AND users.status = 'confirmed'");
And finally the account login
public function login(){
session_start();
dbconn();
global $site_config, $pdo;
// Check for POST
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// Process form
// Sanitize POST data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
// Init data
$username = $_POST['username'];
$password = $_POST['password'];
$testing = $_POST['rememberme'];
// called model method/function
$row = DB::run("SELECT id, password, secret, status, enabled FROM users WHERE username =? ", [$username])->fetch();
// echo $row;
// Validate Input
// Verify user password and set $_SESSION
if (password_verify($password, $row['password'])) {
setsess($row["id"], $row['password']);
if( isset($_POST['rememberme']) ){
logincookie($row["id"], $row["password"], $row["secret"]);
setsess($row["id"], $row['password']);
}
header("Location: ".TTURL."/index.php");
}else {
echo 'Wrong username or password';
header("Location: ".TTURL."/account/login");
}
}
stdhead(T_("LOGIN"));
begin_frame(T_("LOGIN"));
// Members Only
if ($site_config["MEMBERSONLY"]) {
$message = T_("MEMBERS_ONLY");
print("<center><b>" . $message . "</b></center>\n");
}
// add view
$data = [];
// load view
$this->view('account/login', $data);
end_frame();
stdfoot();
}