javascript - Which is the correct way to check if a session is still running? Which are some ways? -
so have had first experience web design, there things did in way didn't like, because of time. anyway, don't know correct way handle sessions. did in previous project embed php html , based on whatever in $_session, id , user type number, i'd open or close parts of website user, through embedded php.
however, not how wanted initially, @ first wanted make javascript code send request through ajax server, , check if session still active, , based on open/close parts of website, i'd have make request on every page , every make sure user logged in. should handle client-side?
what other ways check session without embedding php , using javascript instead modify html document.
to this, need ajax request on loop , php file check session.
the javascript
function checklogin(un) { //put function in loop of sort depending on how want use var xmlhttp = new xmlhttprequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { logout(xmlhttp.responsetext); } } xmlhttp.open("get", "checklogin.php?un=" + un, true); xmlhttp.send(); } function logout(resp) { if (resp == "false") { //comparing actual text, not boolean window.location.href = 'logout.php'; //destroy session or } }
the php file checklogin.php (very simple)
<?php if ($_session[$_get['un']] != true) { echo 'false'; } ?>
the php depend on how store sessions, in case using 1 session titled username stores true
if signed in. recommend making more complex if decide use it.
Comments
Post a Comment