<?php
ob_start();
    
    if(isset($_POST['btnLogin'])){
        if(!empty($_REQUEST["remember-me"])) {
    	setcookie ("login-username",$_REQUEST["login-username"],time()+ 3600);
    	setcookie ("login-password",$_REQUEST["login-password"],time()+ 3600);
        // 	echo "Cookies Set Successfuly";
        } else {
        	setcookie("login-username","");
        	setcookie("login-password","");
        // 	echo "Cookies Not Set";
        }
        	

         // Randomly generate code.
         function sessioncode() {
            $chars = "003232303232023232023456789";
            srand((double)microtime()*1000000);
            $i = 0;
            $pass = '' ;
            while ($i <= 7) {
    
                $num = rand() % 33;
    
                $tmp = substr($chars, $num, 1);
    
                $pass = $pass . $tmp;
    
                $i++;
    
              }
            return $pass;
        }
        $scode=''.sessioncode();
        $sess_id= md5($scode);


        try {
        $password  = $_REQUEST['login-password'];
    	$today = date("Y-m-d H:i:s");
        $ip = $_SERVER['REMOTE_ADDR'];
        
        $sth = $db->query("SELECT * FROM tbl_user_access WHERE username='" . $_REQUEST['login-username'] . "' AND status=1 ");
        if ($sth->rowCount() != 0) {
        $row = $sth->fetch();
        if (password_verify($password, $row['password']))
        {
       
        $login = TRUE;            
                
        // assign session
        $_SESSION['sess_id']=$sess_id;
        $_SESSION['email'] = $row['email'];;
        $_SESSION['role'] = $row['u_role'];
        $_SESSION['privilege_id'] = $row['privilege_id'];
        $_SESSION['institut_id'] = $row['institut_id'];
        $_SESSION['family_name'] = $row['fmly_name'];
        $_SESSION['first_name'] = $row['fst_name'];
        $_SESSION['status']='ON';
        $_SESSION['accid'] = $row['last_acc'];
        $_SESSION['us_access'] = $row['u_id'];
        $_SESSION['phone'] = $row['phone'];
        $_SESSION['u_function'] = $row['u_function'];

        $message = 'success';
        $register_user = $db->prepare("INSERT INTO tbl_user_session(sess_id,username,ip,time_login)VALUES(?,?,?,?)");
        try {
            // insert into tbl_personal_ug
            $register_user->execute(array($sess_id, $_SESSION['accid'], $ip, $today));
            // end to insert
        } catch (PDOException $ex) {
            //Something went wrong rollback!
         echo $ex->getMessage();
        }
		
        ######## REDIRECT #####
        header('refresh:0;res/gate'); // Gate

        ######## END REDIRECT #####
                    
        $login_time = $db->prepare("UPDATE tbl_user_access SET last_logged_in=? WHERE u_id=?");
          
        try {
        // time logged
        $login_time->execute(array($today, $_SESSION['us_access']));
        } catch (PDOException $ex) {
            //Something went wrong rollback!
            echo $ex->getMessage();
        }

        }else{
            $errormsg2 = 'error';    
        }
        //END VERFIE
        			
		}else{
		    $errormsg = 'error';   
		}  
	
        }catch (PDOException $ex) {
          //Something went wrong rollback!
          echo $ex->getMessage();
        }	
    }

ob_end_flush();
?>