Avoid Session in 2 PHP Browsers

1

Good morning, I would like to know if someone knows how to prevent a user from entering their session in a different browser, the code that I have is the following if someone knows how I would thank them too much

<?php
//incluye la conexion

session_start();


require("Funciones/Conexion.php");
$mysqli=inicio();

//AQUI SE HACE LA VALIDACIÓN PARA EL ADMINISTRADOR PREDETERMINADO

$username=$_POST['usuario'];
$contrasena=$_POST['contrasena'];

$sql2=mysqli_query($mysqli, "SELECT * FROM usuarios WHERE correo='$username'"); // Aquí hace la seleccion de la tabla usuarios de la base de datos

if($f2=mysqli_fetch_assoc($sql2)){
	if($contrasena==$f2['pasadmin']){ 
		$_SESSION['pasadmin']=$f2['pasadmin'];
		$_SESSION['idusuario']=$f2['idusuario'];
		$_SESSION['nombre']=$f2['nombre'];
		$_SESSION['login']=$f2['login'];
		
		  //Aquí valida los campos de la tabla usuarios para Administrador

		print "<script>window.location='Administrador.php';</script>";	
}
  }

$sql=mysqli_query($mysqli,"SELECT * FROM usuarios WHERE correo='$username' and habilitado='1'");
	if($f=mysqli_fetch_assoc($sql)){
		if($comprobar_pass=password_verify($contrasena,$f['contrasena'])){
			$_SESSION['idusuario']=$f['idusuario'];
			$_SESSION['nombre']=$f['nombre'];
			$_SESSION['activo']=$f['activo'];
			$_SESSION['correo']=$f['correo'];
			$_SESSION['login']=$f2['login'];

			header("Location: Usuario.php");
		}
		
if ($login == 1){
  $mysqli = new mysqli("localhost", "root", "12056", "reloj");		
	$sql = $mysqli->query("UPDATE usuarios SET login='1' WHERE correo='$username'");
else{
echo "Esta Cuenta Ha Sido Abierta En Otro Navegador";
}


		if(!isset($_SESSION['intentos'])) {
$intentos = 1; 

$_SESSION['intentos'] = $intentos;
} else {
$intentos = $_SESSION['intentos'];

}

$_SESSION["intentos"]++;
if ($_SESSION['intentos'] > 4)
{
$mysqli = new mysqli("localhost", "root", "12056", "reloj");		
	$sql = $mysqli->query("UPDATE usuarios set habilitado='0' WHERE correo='$username'");


	echo '<script>jQuery(function(){swal({
title:"Error",text:"Tu sesión a sido bloqueada, restablece tu contraseña presiona Aceptar",type:"error",confirmButtonText:"Aceptar"
},function(){location.href="pass/nueva.php";});});</script>';

	session_destroy(); 
}else{ 
echo '<script>jQuery(function(){swal({
title:"Error",text:"Contraseña Incorrecta, Llevas ('.($_SESSION['intentos']-1).') Intento de 3 Intentos ",type:"warning",confirmButtonText:"Aceptar"
},function(){location.href="Index.php";});});</script>';
}

	}else{

		echo '<script>jQuery(function(){swal({
title:"Error",text:"Esta Cuenta A Sido Bloqueada",type:"error",confirmButtonText:"Aceptar"
},function(){location.href="Index.php";});});</script>';
	}






?>
    
asked by iBokii 13.08.2017 в 23:18
source

1 answer

1

In one project I had the same situation, and the solution that was made was to register in BD that the user had logged in. Upon entering, the corresponding validation is made that you do not have a session started.

@Muriano, what I comment is something conceptal and what I see in the code is already being implemented

if ($login == 1){
  $mysqli = new mysqli("localhost", "root", "12056", "reloj");      
  $sql = $mysqli->query("UPDATE usuarios SET login='1' WHERE correo='$username'");
else{
  echo "Esta Cuenta Ha Sido Abierta En Otro Navegador";}

However I do not understand why it is validated that the loguin field is equal to 1 ( if ($login == 1) ) and if it is true, this field is reset to 1 ( ...UPDATE usuarios SET login=1 ... ), apparently this is what it does that the user can authenticate in several browsers. if it should be replaced by

if ($login == _condicion_de_no_estar_logueado_){...}else{...}

And when the user logs off, return to the state of _no_logged _

$sql = $mysqli->query("UPDATE usuarios SET login='_condicion_de_no_estar_logueado_' WHERE correo='$username'");
    
answered by 14.08.2017 в 05:11