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
MVC Update (PDO)
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / M-BiTsy / M-BiTsy Support

Unknown User
2020-04-28 00:08:30
"m-jay, post: 68431, member: 20288":

That can be done I'm redoing account controller now with sessions
I have login, logout, recover etc all in one controller just pulling models and views out

With session you can store as much data as you want and it will be always obfucated, the only downside is; session is available only as long as user doesn't close the window and new session file will be created per every site visit.

Also, this increases the server I/O. Therefore, to have 'remember me' option, session must be saved to database because it cant point to user after they leave the site or close the browser.

The cool thing is that you can expire session at exact time you want, without user modyfing values (for example cookies), plus use sessions to logout if more than 1 user is logged to current accound and much more.
2020-04-29 16:24:43
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" />&nbsp;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();
}
Unknown User
2020-04-30 19:49:59
But what is really the point of changing cookies to session? I mean doing this with sessions doesn't really change much to the code other than adding unnecessary additions(Hundreds of sessions files being written every second in /tmp/ folder). The session SID (Should be stored in database, then checked against the user which is logging in with their current session, the session should contain all the info you need to verify if user only logged once, when they performed something and so forth)

This way, its not better than cookies because it lacks data therefore cookies are way more efficient when the site grows big.

Here's an example:
CREATE TABLE sessions (
 sid varchar(32) NOT NULL default '',
 uid int unsigned NOT NULL default '0',
 ip varbinary(16) NOT NULL default '',
 time int unsigned NOT NULL default '0',
 useragent varchar(200) NOT NULL default '',
 anonymous tinyint(1) NOT NULL default '0',
 nopermission tinyint(1) NOT NULL default '0',
 PRIMARY KEY(sid),
 KEY time (time),
 KEY uid (uid),
 KEY ip (ip)
) ENGINE=InnoDB


This way you can expire user from accessing admin panel or moderator panel if 1 hour passed since last login, prevent multiple logins if user has more than 5-10 incorrect logins, you can check if useragent is same as the user which is logging and if another user is already logged and so on.
2020-04-30 21:01:39
thanks ill use that how would you go about implementing that
the function setsess can include all of them and the remember me token would insert them all into database

quick test
function setsess($id, $password)
{
   session_start();
   $_SESSION['uid'] = $id;
   $_SESSION['pass'] = $password;
   $_SESSION['ip'] = getip();
   $_SESSION['time'] = gmtime();
   $_SESSION['useragent'] = '0';
   $_SESSION['anonymous'] = '1';
   $_SESSION['nopermission'] = '0';
}


[ATTACH type="full"]5474[/ATTACH]
Untitled.png
Untitled.png
Unknown User
2020-05-01 09:30:06
Look into session set save handler that will allow you to implement a custom session handler. I think the session start should also be global and only time it needs clearing is in logoutcookie.
Unknown User
2020-05-01 09:32:42
Also the only piece of data actually required in a session would be the user ID.
Unknown User
2020-05-01 09:36:00
You can store the ip and user agent and then implement a config option to ensure the ip and user agent the same but on dynamic ips this would log people out a lot and same for browser if that gets updated it would log them out.
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: 1
  • Members: 0
  • Guests online: 1
  • Comments: 3
  • Messages: 1,728