'PHP and WebView - Cookie doesn't are the same

I'm using a WebView to show a web application. All works, except one thing:

My login:

setcookie("USER_ID", $my_id, time() + 86400);

My logout:

setcookie("USER_ID", 0, time()-3600);
header('Location: ./index.php');

after that, i see the login page. And all is normal. The print_r of $_COOKIE array is:

Array
(
 [PHPSESSID] => e44ceefl92jvesh0kddmdicmv1
)

there isn't any USER_ID. and is all correct.

But if i close the Android Webview and reopen this app, magically I am logged in! And if I print the cookie Array, is:

Array
(
[USER_ID] => 1  // WTF?!?
[PHPSESSID] => e44ceefl92jvesh0kddmdicmv1
)

it seems like the android webview send to the server an older cookie array.

Please Help!

--------------------edit----------------

also if i print

cookieManager.getCookie("MyUrl");

there is the USER_ID cookie that i've deleted with logout



Solution 1:[1]

I tried to do this onPageFinished

CookieManager.getInstance().cookieManager.flush();

it seems to work, but i'm not sure

Solution 2:[2]

Try setting path and domain when you first set cookie and when you remove it, often times in my experience this solved a lot of problems with cookie.

And also instead setting cookie value to 0 on log out just set it to empty string like this ""

If that does not work, then also add unset($_COOKIE['USER_ID']); to your log out part, so it will be like this;

setcookie("USER_ID", 0, time()-3600);
unset($_COOKIE['USER_ID']);
header('Location: ./index.php');

Solution 3:[3]

The cookie can't be set because the name given in the domain argument in the setcookie function don't match with your host

in your case it's empty :(

So listen !

(My System Info: macOS using android studio with android virtual device manager API 24 For testing WebView)

My Android WebView url: http://192.168.1.147:8080/pfe

My PHP CODE : setcookie('CUSTOMER_ID','$id',time() + 24 * 86400 * $user_stay, "/",'localhost');

And this will not work because My Android WebView url don't match with my Setcookie Domain name

SO TRY TO HAVE COOKIE DOMAIN NAME SAME THAT YOUR COMPUTER IP ADDRESS


I HAVE USED ANDROID WEBVIEW & PHP & MYSQL & FLUTTER

Solution 4:[4]

I did not see your code but i can assume that the user is not properly logged out.

Logout code for php sesssion shold look like

<?php
session_start();
session_destroy();

// thats all
// you can unset all php sessions if you wants
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gianluca Demarinis
Solution 2 Mr Shumar
Solution 3 Adams
Solution 4 Nancy Moore