i'm trying to do a login working with cookies, but can't make it work, because i got an infinite loop everytime, already blown my head thinking how to fix it.
Just like this it will show everything perfect, store cookies and redirect to panel.php but the the validations doesn't work anymore i can enter to every modules like index.php?do=module
UPDATE session cookie it's a ramdon value saved in the database after the user login, and then it's stored in the cookie to be compared everytime.
I use $_COOKIE["session"] to look for user info when needed as it's dynamically changed everytime it log in.
Core.php
if (! defined ( 'SRCP' )) {
die ( "Error" );
}
@include_once (CORE_DIR. '/security/check.loged.php');
// i was trying to set a variable to tell the script to do not check again, so the loop will break, but i just got a blank page.
if (!$conectado='si') {
header("Location: index.php?do=login");
}
//recive and store, i was going to use the $_GET inside the switch, but that loop got me
if (isset($_GET['do'])) {
$do = $_GET['do'];
}
switch ( $do ) {
case "panel" :
include_once CORE_DIR . '/modulos/panel.php';
break;
case "login" :
include_once CORE_DIR . '/modulos/login.php';
break;
default:
include_once CORE_DIR . '/modulos/login.php';
break;
}
check.login.php
if (! defined ( 'SRCP' )) {
die ( "Error" );
}
if (isset($_COOKIE["id_usuario"]) && isset($_COOKIE["session"])){
if ($_COOKIE["id_usuario"]!="" || $_COOKIE["session"]!=""){
$query = " SELECT ID,
password,
salt,
correo,
logueado
FROM usuarios
WHERE cookie = :cookie
";
$query_params = array(
':cookie' => $_COOKIE['session']
);
try{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){
//echo the error.
}
$row = $stmt->fetch();
$conectado='si';
}
else{
$conectado='no';
}
}
inside the panel, i have no php code, because i will include it inside this index.php file.
define ( 'SRCP', true );
define ( 'ROOT_DIR', dirname ( __FILE__ ) );
define ( 'CORE_DIR', ROOT_DIR . '/core' );
require_once ROOT_DIR . '/core/core.php';
EDIT: fixed it. Had to remake the code in check.loged.php
$row = $stmt->fetch();
if($row['logueado']=='SI'){
$login_ok = true;
}else{
$login_ok = 0;
}