Question:
I would like to know how I can complement the code below so that if the user is logged in, no other person can access the restricted area with the same login and password when the user who used that access is active. So that if he closes the browser or logs out, the section is destroyed, thus releasing access to that account.
Along with that the placement of a remember lease button on the login form panel.
Below is MySQL along with the codes.
mysql
CREATE TABLE IF NOT EXISTS `membros` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`login` char(20) NOT NULL DEFAULT 'admin',
`autor` varchar(20) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL DEFAULT 'Ryumaru',
`senha` char(255) NOT NULL DEFAULT 'admin',
`idade` int(2) NOT NULL DEFAULT '0',
`email` varchar(255) NOT NULL DEFAULT 'exemplo@email.com',
`cargo` enum('Adminstrador','Editor','Upload') NOT NULL DEFAULT 'Adminstrador',
`data_cadastro` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`data_ultimo_login` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`arquivo_data_cad` date NOT NULL DEFAULT '2014-05-03',
`arquivo_hora_cad` time NOT NULL DEFAULT '11:11:11',
`ativado` enum('0','1') NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `membrosDados` (`id`,`login`,`senha`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
check.php
<?php
session_start(); // Inicia a session
include "config.php";
$login = $_POST['login'];
$senha = $_POST['senha'];
if((!$login) || (!$senha)){
echo "<script>window.alert('Por favor, todos campos devem ser preenchidos!');</script>";
include "index.php";
}
else{
$senha = md5($senha);
$sql = mysql_query("SELECT * FROM membros WHERE login='{$login}' AND senha='{$senha}' AND ativado='1'");
$login_check = mysql_num_rows($sql);
if($login_check > 0){
while($row = mysql_fetch_array($sql)){
foreach( $row AS $key => $val ){
$$key = stripslashes( $val );
}
$_SESSION['id'] = $id;
$_SESSION['login'] = $login;
$_SESSION['autor'] = $autor;
$_SESSION['email'] = $email;
mysql_query("UPDATE membros SET data_ultimo_login = now() WHERE id ='{$id}'");
header("Location:/admin/inicio/");
}
}
else{
echo "<script>window.alert('Acesso negado tente nova mente');</script>";
include "index.php";
}
}
?>
function.php
<?php
function session_checker(){
if(!isset($_SESSION['id'])){
header ("Location:/admin/");
exit();
}
}
function verifica_email($EMAIL){
list($User, $Domain) = explode("@", $EMAIL);
$result = @checkdnsrr($Domain, 'MX');
return($result);
}
?>
Protect sections. checking if the user is logged in I put this at the top of the page
<?php include("config.php");
session_start();
include "functions.php";
session_checker(); ?>
Logout.php
<?php
session_start();
if(!isset($_REQUEST['logmeout'])){
echo "Você realmente deseja sair da área restrita?<br />";
echo "<a href=\"logout.php?logmeout\">Sim</a> | <a href=\"javascript:history.go(-1)\">Não</a>";
}
else{
session_destroy();
include "index.php";
}
?>
Answer:
I found what I wanted, thanks for everyone's help, I'll leave the code below in case anyone wants to use it.
<?php
session_start();
// seta configurações hora e tempo limite de inatividade//
date_default_timezone_set("Brazil/East");
$tempolimite = 900;
//fim das configurações de hora e limite de inatividade//
// aqui ta o seu script de autenticação no momento em que ele for validado você seta as configurações abaixo.//
// seta as configurações de tempo permitido para inatividade//
$_SESSION['registro'] = time(); // armazena o momento em que autenticado //
$_SESSION['limite'] = $tempolimite; // armazena o tempo limite sem atividade //
// fim das configurações de tempo inativo//
?>
Put the code below on the pages where you want to check the uptime.
<?php
$registro = $_SESSION['registro'];
$limite = $_SESSION['limite'];
if($registro)// verifica se a session registro esta ativa
{
$segundos = time()- $registro;
}
// fim da verificação da session registro
/* verifica o tempo de inatividade
se ele tiver ficado mais de 900 segundos sem atividade ele destrói a session
se não ele renova o tempo e ai é contado mais 900 segundos*/
if($segundos>$limite)
{
session_destroy();
die( "Sua seção expirou.");
}
else{
$_SESSION['registro'] = time();
}
// fim da verificação de inatividade
?>