Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- require('config.php');
- // check if logged in
- function iflogin(){
- // connect to database
- $con = new PDO('mysql:host='.DB_HOST.';dbname='.DB_DB.';charset=utf8_general_ci', DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
- $con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- // delete old users
- $query = $con->prepare('DELETE * FROM `active_users` WHERE `expires`<=`:time`');
- $query->bindValue(':time', time());
- $query->execute();
- // find current user in active users
- $sql = $con->prepare('SELECT * FROM `active_users` WHERE `session_id` = `:sessid`');
- $sql->bindValue(':sessid', session_id());
- $res = $sql->fetch();
- if($res->rowCount()){
- // is logged in
- return true;
- }else{
- //is not logged it (or timed out)
- header('location: ' . HOST);
- return false;
- }
- }
- // extend user log in session, so he can use it longer (the default timeout from current time)
- function extendSession(){
- // connect to database
- $con = new PDO('mysql:host='.DB_HOST.';dbname='.DB_DB.';charset=utf8_general_ci', DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
- $con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- // calculate new timeout
- $newexp = time() + (60 * SESSION_LENGTH);
- // insert new timeout: prepare
- $myquery = $con->prepare('UPDATE `active_users` SET `expires`=`:expires` WHERE `session_id`=`:id`');
- // bind values
- $myquery->bindValue(':expires',$newexp);
- $myquery->bindValue(':id', session_id());
- // execute
- $myquery->execute();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement