Ok, so now I have this code in my login form handler .php file:
<?php
$username = "root";
$password = "root";
$hostname = "localhost";
$db = "agendadb";
$db = new mysqli($hostname, $username, $password, $db);
$res = $db->query("SELECT userId FROM tblUsers WHERE username='".$_POST['username']."' AND password='".md5($_POST['password'])."'");
if($res){
if($row = $res->fetch_assoc()){
setcookie('userId', strval($row['userId']), 2147483647);
$_COOKIE['userId']= $row['userId'];
echo 1;
}else{
echo -2;
}
}else{
echo -1;
}
?>
Somewhy, the cookie userId is not being set, and I can't figure out why. The script is working because it echoes the result correctly, but the cookie is not being set and no errors are thrown.
In the begining of my index page I have the following code to check if the cookie has been set
<?php
echo '<script>';
if(!isset($_COOKIE['userId'])){
echo 'console.log(" isset");';
}
if(empty($_COOKIE['userId'])){
echo 'console.log(" empty");';
}
if($_COOKIE['userId']==""){
echo 'console.log(" ==");';
}
echo '</script>';
?>
and after loging in my console in index.php always shows isset empty ==
Also, I tried using ob_start(); and ob_end_flush(); in the login handler .php file, but did not work either.
Any help would be appreciated.