This mod is complete in that it does what it is intended to do ?
The base mod has two session uid (user id) & pass (hashed password, id, ip, secret) again this is somewhere things can be added
Members can login with session which closes when browser close
[ATTACH type="full" alt="session.png"]5490[/ATTACH]
Members can click remember for session login cookie which last 1 month
[ATTACH type="full" alt="cookie.png"]5491[/ATTACH]
if anyone plays with this can you please share any work or examples
sql
in account-login.php find
add below
find
replace that old code with this (just after show error)
find
add below
in account-logout.php find
add under
in functions.php replace logincookie & logout cookie with
find
add under
[/code]
now the userlogin function for auth
find
replace with
Session.php (the class)
LAST PART add to cleanup.php before optimise tables you can add this any where login, logout 09/05/20
in config
The base mod has two session uid (user id) & pass (hashed password, id, ip, secret) again this is somewhere things can be added
Members can login with session which closes when browser close
[ATTACH type="full" alt="session.png"]5490[/ATTACH]
Members can click remember for session login cookie which last 1 month
[ATTACH type="full" alt="cookie.png"]5491[/ATTACH]
if anyone plays with this can you please share any work or examples
sql
create table sessions(
id serial,
sess_id text not null,
_access integer,
data text,
primary key (id)
);
in account-login.php find
$password = $_POST['password'];
add below
$testing = $_POST['rememberme'];
find
if (!$message){
replace that old code with this (just after show error)
if (!$message){
// Session Handler
setsess($row["id"], $row["password"], $row["secret"]);
if( isset($_POST['rememberme']) ){
logincookie($row["id"], $row["password"], $row["secret"]);
setsess($row["id"], $row["password"], $row["secret"]);
}
header("Location: index.php");
}else {
show_error_msg(T_("ACCESS_DENIED"), $message, 1);
}
find
<tr><td align="center"><b><?php echo T_("PASSWORD"); ?>:</b> <input type="password" size="40" name="password" /></td></tr>
add below
<tr><td align="center"><input type="checkbox" name="rememberme" value="rememberme" /> Remember Me</td></tr>
in account-logout.php find
deconn();
add under
// Remove cookies & sessions
session_destroy();
in functions.php replace logincookie & logout cookie with
// Obtain / Create session Cookie Function
function logincookie($id, $password, $secret, $updatedb = 1, $expires = 0x7fffffff)
{
// get sess_id
$sid = session_id();
// store only sess_id
setcookie("PHPSESSID", $sid, time()+ 30*30*60*60, "/"); // one month
// maybe add session in cleanup
if ($updatedb) {
SQL_Query_exec("UPDATE users SET last_login = '".get_date_time()."' WHERE id = $id");
}
}
// Cookie Destruction function
function logoutcookie()
{
// get sess_id
$sid = session_id();
// reset cookie to session
setcookie("PHPSESSID", $sid, "0", "/");
}
function setsess($id, $password, $secret)
{
// encrypt pass
$hash = $id . $secret . $password . getip() . $secret;
// set session handler
$_SESSION['uid'] = $id;
$_SESSION['pass'] = $hash;
}
find
function dbconn($autoclean = false){
add under
// Session Handler
require "classes/Session.php";[/code]
now the userlogin function for auth
find
//Check The Cookie and get CURUSER details
if (strlen($_COOKIE["pass"]) != 40 || !is_numeric($_COOKIE["uid"])) {
logoutcookie();
return;
}
//Get User Details And Permissions
$res = SQL_Query_exec("SELECT * FROM users INNER JOIN groups ON users.class=groups.group_id WHERE id=$_COOKIE[uid] AND users.enabled='yes' AND users.status = 'confirmed'");
$row = mysqli_fetch_assoc($res);
if (!$row || sha1($row["id"].$row["secret"].$row["password"].$ip.$row["secret"]) != $_COOKIE["pass"]) {
logoutcookie();
return;
}replace with
// Check The Cookies and Sessions details
if (!$_SESSION["pass"] || !is_numeric($_SESSION["uid"])) { // todo
logoutcookie();
return;
}
//Get User Details And Permissions
$res = SQL_Query_exec("SELECT * FROM users INNER JOIN groups ON users.class=groups.group_id WHERE id=$_SESSION[uid] AND users.enabled='yes' AND users.status = 'confirmed'");
$row = mysqli_fetch_assoc($res);
$hash = $row["id"] . $row["secret"] . $row["password"] . $ip . $row["secret"];
if (!$row || !$_SESSION['pass'] == $hash){
logoutcookie();
return;
} else {
// success - need else to match hash & session above
}
Session.php (the class)
<?php
class Session {
private $pdo;
public function connect() {
global $site_config;
// create a db connection
$pdo = new PDO('mysql:host='.$site_config['mysql_host'].';dbname='.$site_config['mysql_db'], $site_config['mysql_user'], $site_config['mysql_pass']);
return $pdo;
}
public function _open() {
$this->pdo = Session::connect();
if ($this->pdo) {
return true;
}
else {
return false;
}
} // end function _open()
public function _close() {
$this->pdo = null;
return true;
} // end function _close()
public function _read($sess_id) {
if (!$this->pdo) {
$this->pdo = Session::connect();
}
$sql = "select data from sessions where sess_id=:sess_id";
$statement = $this->pdo->prepare($sql);
$statement->execute(array(':sess_id' => $sess_id));
$num = $statement->rowCount();
if ($num == 1) {
$row = $statement->fetch(PDO::FETCH_ASSOC);
$data = $row["data"];
return $data;
}
else {
return ''; // return empty string if no data returned
}
} // end function _read()
public function _write($sess_id,$data) {
$access = time();
$sql = "select * from sessions where sess_id=:sess_id";
$statement = $this->pdo->prepare($sql);
$statement->execute(array(':sess_id' => $sess_id));
$num = $statement->rowCount();
if ($num === 0) {
$sql = "insert into sessions (sess_id, _access, data) values ".
"(:sess_id,:access,:data)";
$statement2 = $this->pdo->prepare($sql);
$myArray = array();
$myArray[':sess_id'] = $sess_id;
$myArray[':access'] = $access;
$myArray[':data'] = $data;
$statement2->execute($myArray);
if ($statement2 === false) {
return false;
}
else {
// new data stored
return true;
}
} // end case of inserting new session data
else {
$sql = "update sessions set _access = :access, " .
"data = :data where sess_id = :sess_id";
$statement2 = $this->pdo->prepare($sql);
$myArray = array();
$myArray[':sess_id'] = $sess_id;
$myArray[':access'] = $access;
$myArray[':data'] = $data;
$statement2->execute($myArray);
if ($statement2 === false) {
return false;
}
else {
// data updated
return true;
}
} // end case of updating existing record
} // end function _write()
public function _destroy($sess_id) {
$sql= "delete from sessions where sess_id=:id";
$statement = $this->pdo->prepare($sql);
$statement->execute(array(':id' => $sess_id));
if ($statement === true) {
return true;
}
else {
return false;
}
} // end function _destroy()
public function _gc($max) {
$old = time() - $max;
$sql = "delete from sessions where _access < :old";
$statement = $this->pdo->prepare($sql);
$statement->execute(array(':old' => $old));
if ($statement === true) {
return true;
}
else {
return false;
}
} // end function _gc
}
$sess = new Session();
session_set_save_handler(
array($sess,'_open'),
array($sess,'_close'),
array($sess,'_read'),
array($sess,'_write'),
array($sess,'_destroy'),
array($sess,'_gc')
);
register_shutdown_function('session_write_close');
/*
// change php.ini
ini_set('session.cookie_lifetime', 60 * 60 * 24 * 90); // 3months
ini_set('session.gc_maxlifetime', 60 * 60 * 24 * 90); // 3 months
*/
// start
session_start();
?>
LAST PART add to cleanup.php before optimise tables you can add this any where login, logout 09/05/20
// DELETE OLD SESSION ENTRIES
$ts = gmtime() - $site_config["session_time"];
SQL_Query_exec("DELETE FROM sessions WHERE _access > FROM_UNIXTIME($ts)");
in config
$site_config["session_time"] = 1800; // default above 30 mins

