How to use cookies with WebGL/Javascript/PHP

Hello,

I have a Unity WebGL app where after the app loads the first scene is a log in scene where the user types his username and password which gets sent to server for validation (a PHP script), then if server says he is valid, he gets logged in and moved to the next scene.

I want to the user not to have to go through that every time, so I want to automatically log him in if he checked a “remember me” button during login, so that next time he will get logged in automatically.

Anybody knows how to do this using cookies?

I guess the WebGL app would tell the server to remember the user, and then the server PHP script would send back a cookie to save in the browser, so that next time the WebGL app can check if the cookie is there, and use that to somehow log the user in automatically.

I dont know if all this can work with Unity and WebGL. I probably need to use a combination of javascript and the server PHP script in addition to the WebGL app code itself.

Anybody did this before could give me a hint on the right way to do it?

Thanks!

Seems cookies dont work at all in WebGL. I wrote code in Javascript to set and retrieve cookies, but it doesnt work.

    setCookie: function (cname, cvalue, exdays) {
       var d = new Date();
       d.setTime(d.getTime() + (exdays*24*60*60*1000));
       var expires = "expires="+ d.toUTCString();
       document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
       console.log('set cookie='+document.cookie);
    }

    getCookie: function (cname) {
       var ret="";
       var name = cname + "=";
       var decodedCookie = decodeURIComponent(document.cookie);
       console.log('get cookie='+decodedCookie);
       var ca = decodedCookie.split(';');
       for(var i = 0; i <ca.length; i++) {
           var c = ca[i];
           while (c.charAt(0) == ' ') {
               c = c.substring(1);
           }
           if (c.indexOf(name) == 0) {
               ret=c.substring(name.length, c.length);
               break;
           }
       }
       var buffer = _malloc(lengthBytesUTF8(ret) + 1);
       writeStringToMemory(ret, buffer);
       return buffer;
    }

Anybody knows why it just returns empty string when i retrieve the cookies?