I am working on a web site with login registration functionality.I have implemented the login registration part with reference and forum help,but now I am facing problem with the PHP session.This is my ajax form submit:
$.ajax({
type: "POST",
url: "userLogin.php",
data: "eid="+eid+"&pwd="+pwd+"&rememberme="+remember,
dataType: "html",
success: function(msg){
if($.trim(msg)!="error")
{
$("#user_status", window.parent.document).html("Welcome "+msg+" | <a href='forum/logout.php'>Logout</a>");
if(window.parent.document.getElementById('post_user_name'))
$("#post_user_name", window.parent.document).html("<strong style='color:#333333;'>"+msg+"</strong>");
parent.$.fancybox.close();
}
else{
$('#error_common').html("Invalid ID or password.Please try again");
$('#error_common').show();
return false;
}
}
});
This is the PHP code:
if(isset($_POST['eid']) && isset($_POST['pwd']))
{
$userObj = new Model_Users();
$username= return_fetched_value($_POST['eid']);
$password= md5(return_fetched_value($_POST['pwd']));
$getUserDetails = $userObj->getUserValueByUserNameAndPassword($username,$password);
/*print_r($getUserDetails);
exit;*/
if(count($getUserDetails)>0)
{
$_SESSION['username'] = $getUserDetails['uname'];
$displayUser=$getUserDetails['uname'];
echo $displayUser;
}
else
{
//echo "entered here";
echo "error";
}
}
else
{
$errorMessage = "You have entered invald username password" ;
}
Everything works fine,the user logs in and get the username on the login page as here http://www.proptiger.com/,but if I refresh the page the username disappears,but it is still present in the session.I can see it if I do print_r($_SESSION).
The username has been set using the jquery ajax portion.
$("#user_status", window.parent.document).html("Welcome "+msg+" | <a href='forum/logout.php'>Logout</a>");
Which I think is not persisted,but still there in the session.I am new to jquery ajax and stuck at this.Any constructive comments would be helpful.
Thanks
This might sound silly but are you sure you’re displaying the username with php on page load or is it just when there’s a login ajax call?
As far as I can tell your code only displays the username after your AJAX call, but there isn’t anything like
<?php echo $_SESSION['username']; ?>when you load the page without ajax.I hope my answer is clear enought, but just to conclude: Make sure you echo the username when someone goes directly into your website and not just on the login action.