我正在开发一个网站,其中角度js作为前端,PHP作为后端。用户可以登录,使用他们的个人资料视图并进行编辑和注销。
我能够做到这一切,但有时候,我认为会话过期或其他什么,我不会在前端获得任何价值。他/她的个人资料看起来很空白,当我点击某个链接或其他内容时,我会被重定向到登录页面。
我使用本地存储来保存tha api返回的令牌。另外,在验证登录时,我使用PHP在会话中保存了一些值。
我认为这是一个有角度的问题。但我不太清楚为什么或导致它的原因。
任何人都可以帮我解决这个问题吗?
function login()
{
$data= array();
$username=validate_input($_POST['username']);
$password=validate_input($_POST['password']);
$qry="SELECT * FROM entrp_login where email='".$username."' AND password='".md5($password)."' ";
$res=getData($qry);
$count_res=mysqli_num_rows($res);
if($count_res>0)
{
while($row=mysqli_fetch_array($res))
{
$data['firstname'] = $row['firstname'];
$data['lastname'] = $row['lastname'];
$data['id'] = $row['clientid'];
$data['username'] = $row['username'];
$data['success'] = true;
$data['msg'] = 'Valid User';
//generate a client token
$client_session_token='thisisdumytoken'
//set session
session_start();
$_SESSION['id'] = $data['id'];
$_SESSION['firstname'] = $data['firstname'];
$_SESSION['lastname'] = $data['lastname'];
$_SESSION['login_token'] = $client_session_token;
$_SESSION['username'] = $data['username'];
$data['login_token'] = $client_session_token;
}
}
else
{
$data['success'] = false;
$data['msg'] = 'Please check your credentials once again';
}
return $data;
}
我的角度函数(代码片段),其中我设置了localstorage
// function to submit the form after all validation has occurred
vm.login = function(isValid)
{
// check to make sure the form is completely valid
if (isValid)
{
//alert('isValid');
$http({
method: 'post',
url: baseUrl+'login',
data: $.param($scope.vm),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data, status, headers, config)
{
if(data.success)
{
//alert(data.msg);
//localStorage.clear();
if (localStorage['entrp_token'])
{
localStorage.removeItem('entrp_token');
}
localStorage.setItem("entrp_token", JSON.stringify(data.login_token));
$location.path('/home');
}
else
{
//alert('invalid 1');
//alert(data.msg);
if (localStorage['entrp_token'])
{
localStorage.removeItem('entrp_token');
}
//localStorage.clear();
vm.errorMessage = data.msg;
}
}).
error(function(data, status, headers, config)
{
//alert('invalid 2');
if (localStorage['entrp_token'])
{
localStorage.removeItem('entrp_token');
}
//localStorage.clear();
vm.errorMessage = data.msg;
});
}
};