I have a website with a simple login function which includes the option "remember me" and uses cookies to manage that option with a token.
I have few lines below that I use to detect in the home page if the user is logged in already because of the "remember me" option or not.
Strangely the loading of this home page is extremely slow (the request to the server remains pending for even one minute) before it really starts downloading the content and then in less than a second the page is loaded.
By doing several tests i noticed that this very slow loading is linked to the login feature and it applies only when the user is logged in. Infact if the user is NOT logged in the the loading goes perfectly. On the other hand if it's logged in it takes a lot, more than a minute. With the user logged in, if I remove the following script than it goes fast again.
Any idea of what drives this strange behavior? I think it might be related to cookies or session variables but I use them in other parts of the website in the same way for other features and I didn't have this behavior.
HERE IS THE CODE:
//Self login check
$source = $_SESSION['source_login']; //Source of the login in case of social login
$sessione_login = $_SESSION['login_user_mywesite']; //user token saved in the session when logged in
$cookie_login = $_COOKIE["user_mywesite"]; //Cookie token saved in case of "remember me" option
//If session and cookie are empty than the user is not logged in
if (($sessione_login == "") && ($cookie_login == "")) {
$loggato = "no";
$token_utente = $_COOKIE["cookie_token_mywesite"];
$sql_init_cart = "SELECT Sum(qta) AS tot FROM t_carrello WHERE token_utente = '$token_utente' GROUP BY token_utente";
} else {
//Otherwise it's logged in
$loggato = "si";
$sql_user = "SELECT * FROM t_user WHERE id = '$sessione_login' OR id = '$cookie_login'";
$result_user = mysqli_query($db,$sql_user);
$row_user = mysqli_fetch_array($result_user,MYSQLI_ASSOC);
// Saving user token in the session variable
$_SESSION['login_user_mywesite'] = $row_user["id"];
$token_utente = $_COOKIE["cookie_token_mywesite"];
$utente_corrente = $row_user["id"];
$sql_init_cart = "SELECT Sum(qta) AS tot FROM t_carrello WHERE token_utente IN (SELECT cookie_token FROM t_utenti_cookie WHERE utente_token = '$utente_corrente')";
$sql_init_whish = "SELECT id_prodotto AS tot FROM t_preferiti WHERE token_utente = '$utente_corrente' GROUP BY id_prodotto";
}
Thanks!
It should simply load the page as usual, but when logged in the loading get stuck and it remains pending, waiting for the server for more than 1 minute before it really starts downloading the content from the server and loading the page.