登录后跳转到原页面(servlet)
这里使用servlet实现访问控制。
第一步:.当未登录用户访问某个需要登录的功能页面时,首先会跳转到servlet,在方法前放置案例代码如下:
//需要登录设置
String userId=(String)session.getAttribute("userid");
if(userId==null)
{
if (null!=request.getQueryString())
{
session.setAttribute("redirectUrl",
request.getRequestURL().append("?").append
(request.getQueryString()).toString());
}
else
{
session.setAttribute("redirectUrl",
request.getRequestURL().toString());
}
response.sendRedirect
("user/cy/login.jsp");
}
//再次获得userId,判断是否成功登录
userId=(String)session.getAttribute("userid");
if(userId!=null){ ... }
第二步:登录跳转到的servlet方法里需要放下如下的类似代码:
//登录成功后跳回原页面
String redirectUrl=(String)
request.getSession().getAttribute( "redirectUrl");
if (redirectUrl!=null){//判断是否有跳回的操作
//清除session中需要调转到的页面
System.out.println("返回");
session.removeAttribute( "redirectUrl");
response.sendRedirect(redirectUrl);//跳回到redirectUrl
}
else
{
//不需要跳回就默认跳到首页
response.sendRedirect("user/cy/home.jsp");
}
应用场景:一般网页游客和登录用户看到的内容是有区别的,如果一个未登录的用户在看到登录提示后跳转到登录界面登录,那么登录成功后怎么返回到该页面呢?
写这篇博客是因为我自己把解决问题的方向想偏了,本来实现是很简单的....为了避免再走弯路吧
假设用户在 www.example.com/a.html 看到登录提示,然后点击登录跳转到 www.example.com/login.html,登录界面使用ajax验证用户登录信息,当返回信息为成功时,在回调函数里要做这些逻辑的处理:
1.判断document.referrer是否为空,若为空本页面就不是从其它页面跳转过来的,就将页面跳转至网站首页
2.若document.referrer不为空,则需要判断前一个页面是否是本站点的页面,以免跳到其它站点去了,如果是其它站点则跳转至首页;
3.若document.referrer不为空且为本站点页面,则需要跳转至该页面
我想偏的地方就是第三步,一开始我用的 window.history.back(); 回到上一个页面,这样的操作相当于点击了浏览器的后退按键,原来的页面是没有得到刷新的,页面的内容完全来自于浏览器缓存,然后我想到用cookie,设置document.cookie = 'reload=true',然后在一个每个页面都包含的js里去读cookie,判断cookie中是否有reload=true,如果有则删除该属性,并使用location.reload(); 重新加载页面,看上去没什么问题,可是百度了各种代码去删除cookie中的reload字段的值都没有成功,也试过敲了一遍《JavaScript高级程序设计(第3版)》的cookie删除代码,仍然不见效果,想来这操作cookies也是件蛋疼的事。
删除cookie始终搞不定让我不得不想想是否还有其它方法来实现这个效果,然后想location.href是否可以实现重新加载呢,试了一下location.href果然会重新加载新URL所指向的页面。《JavaScript高级程序设计(第3版)》中有一句"每次修改location的属性(hash除外),页面都会以新URL重新加载”....另外使用location.href = URL 的效果与使用location.assign(URL) 的效果是完全一样的,因为location.href = URL的方式最终以URL值调用assign()方法!
location.assign() 方法加载新的文档。
window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。
因此在登录成功的回调函数里执行下列操作就可以实现登录跳回刷新页面了
- var prevLink = document.referrer;
- if($.trim(prevLink)==''){
- location.href = 'www.example.com/index.html';
- }else{
- if(prevLink.indexOf('www.example.com')==-1){ //来自其它站点
- location.href = 'www.example.com/index.html';
- }
- if(prevLink.indexOf('register.html')!=-1){ //来自注册页面
- location.href = 'www.example.com/index.html';
- }
- location.href = prevLink;
思路
get
get enurl()带过去参数,再登录页面hidden表单提交,登录成功后得到的post值再跳转
post
enurl()hidden表单提交,登录成功后得到的post值再跳转
- //渲染登录页
- public function login_y()
- {
- //$this->display('User/pczzc');
- if(is_mobile()){
- header("Location:http://wap.xxxx.com/login_page.html");
- }
- $refererUrl = urldecode(I('get.refererUrl',U('Index/index')));
- if (empty($refererUrl)) {
- $refererUrl = $_SERVER['HTTP_REFERER'];
- }
- if (strpos($refererUrl, 'login') || strpos($refererUrl, 'regist')) {
- $refererUrl = "http://" . $_SERVER['HTTP_HOST'];
- }
- $this->assign('refererUrl', $refererUrl);
- $this->display('User/login');
- }
- /**
- * 用户登录
- */
- public function login()
- {
- if (IS_GET) {
- $this->display();
- }
- if ($_POST) {
- $account = I('post.account');
- $password = I('post.password');
- $auto = I('post.auto');
- $refererUrl = I('post.refererUrl');
- }
- if(!$account || !$password){
- $this->error('请输入账号或密码');
- }
- $object = D('User');
- $user_info = $object->getLoginUserInfo($account, $password);
- $user_info = $user_info[0];
- if (empty($user_info)) {
- $this->error('用户名错误');
- // echo "<script>alert('用户名错误');location.href='login_y';</script>";
- }
- $verify_password = encode_password($password, $user_info['salt']);
- // echo $verify_password;die;
- if ($verify_password != $user_info['password']) {
- $this->error('密码错误');
- // echo "<script>alert('用户名错误');location.href='login_y';</script>";
- }
- $avatar = !empty($user_info['largeAvatar']) ? get_avatar_path($user_info['largeAvatar']) : 'http://v.xxx.com/assets/img/default/avatar026827c4.png';
- //成功页
- $uid = encrypt($user_info[id]);
- if ($user_info['nickname']) {
- $account = encrypt($user_info['nickname']);
- }
- // 兼容ie
- $_COOKIE["uid"] = $uid;
- $_COOKIE["account"] = $account;
- $_COOKIE["avatar"] = $avatar;
- // cookie('uid', $uid);
- // cookie('account', $account);
- // cookie('avatar', $avatar);
- // if ($auto == 'on') {
- cookie('uid', $uid, 30 * 24 * 3600);
- cookie('account', $account, 30 * 24 * 3600);
- cookie('avatar', $avatar, 30 * 24 * 3600);
- // }
- /**
- * 登录成功更新时间操作
- */
- $data = [
- 'id' => decrypt($uid),
- 'loginTime' => time(),
- 'loginIp' => GetIP()
- ];
- M('user')->data($data)->save();
- header("Location: $refererUrl");
- }