Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
Session Mod [TTv3]
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Mods Completed

2020-05-02 16:46:31
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


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" />&nbsp;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
Untitled.png
Untitled.png
session.png
session.png
cookie.png
cookie.png
Unknown User
2020-05-03 09:09:27
When you define functionality for the session handler you don’t need to call the functions this is automatically done. The only thing required is to set SESSION vars.
2020-05-03 09:55:15
First post updated m8  (y) and thanks for the tip all advice, help and ideas are very appreciated :D
2020-05-05 04:42:55
The class has a nice little pdo connection this can be global so you can use pdo on v3

   public function connect() {
     global $site_config;
     global $pdo;
     // 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;
   }


example of using session pdo connect ( for use in functions add $pdo = connect(); or global $pdo;)
  if (!isset($_SESSION["uid"])) {
   header("Location: logout.php");
 }
 $pdo = connect();
 if (!$pdo) {
   die("Could not connect");
 }
 $sql = "select * from sessions where id=:id";
 $statement = $pdo->prepare($sql);
 $myarray = array();
 $myarray[':id'] = $id;
 $statement->execute($myarray);
Unknown User
2020-05-05 21:07:41
Looking good
2020-05-09 13:34:12
"m-jay, post: 68460, member: 20288":

This mod is complete in that it does what it is intended to do  ?
Ive placed in progress because it might need some fine tuning now we use the database for sessions its more secure but also we can start playing with the data, this is where testing will help the cleanup for example you could wipe tables on cleanups.
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" />&nbsp;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

   // 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();
?>






   // 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
   }

First, thank you and then I would like to know
Or do you place this line ?
2020-05-09 16:42:32
updated post my friend  :D
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 9
  • Members: 0
  • Guests online: 9
  • Comments: 3
  • Messages: 1,728