The following question is a security question. I'm using Sessions to keep user logged in to my site as long as their browser is open.
Now my php code that keeps user logged is quite simple. I first create session with user name when user logged inside(authentication.php):
if (($row['user'] == $usr)&&($row['value'] == $pwd)) {
$response = "ok";
session_start();
$_SESSION['name'] = $usr;
break;
}
Then I create from each page a call to JavaScript to check if user exist I change the web page(AJAX) to look as if user is logged (displaying in header welcome ), the JavaScript looks like this:
function checkUserExistance(data,size)
{
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = xmlhttp.responseText;
document.getElementById("title").value = response;
}
}
xmlhttp.open("POST",url + page,true);
xmlhttp.send();
}
Now on serer side to check if user is logged I use the following simple function, which JavaScript is invoking:
session_start();
if (isset($_SESSION['user'])) {
echo $_SESSION['name'];
}
Now my question is quite simple is that the right way to so it? to keep user authenticated as long as browser open, because if I got that right the JavaScript is on client side meaning that everyone can change it and skip the check with server side post comment on page.
My solution is to add another check when posting comments is to check on server side if user has an open session but then user can pretend to be another user that has already authenticated, this is so frustrating.... what am I missing what should I do?