1. Try to write a login program in imitation. This sign-in form is on the home page and requires a successful login to return the user name information (saved session value) and auto-hide the form on the home page using Ajax without refresh. Now through JS can indeed return the user information, but as long as the refresh, the user information and form will be restored to the original, how can be maintained long until the user clicks the logoff or the session disappears?
2. I can see the returned user information through the F12 XHR mode, which can be achieved through jquery's Append method and remove (), but the problem is that the information does not exist after the refresh and the form reverts to its original state.
2. Key code:
(1) IndexController.class.php:
Public Function CheckUser () {//Receive value $userName =$_post[' userName ']; $userPass =$_post[' Userpass ']; Null value detection->function if (!trim ($userName)) {return Show (0, ' username cannot be null '); } if (!trim ($userPass)) {return Show (0, ' password cannot be null '); }//Verify the authenticity of the user's password->model $res =d ("Stuser")->getuser ($userName); if (! $res [' username ']) {return Show (0, ' username does not exist '); }//Password handling->function if ($res [' Userpass ']!=getmd5pass ($userPass)) {return Show (0, ' password incorrect '); }//echo $res [' username ']; $_session (' username ', $res); Set SESSION $_session[' username ']= $res; Dump ($username); Var_dump ($username); Return Show (1, ' Login successful ', $this->getsessionnames ()); }//To determine the SESSION condition->index public Function getsessionnames () {if ($_session[' username ' [' username ']) {$username = $_session[' username ' [' username ']; $a = $this->ajaxreturn ($username); $this->assign (' username ', $username); return $username; Enter your code .}}
(2) function.php
function show($status,$message,$data=array()){ $result=array( 'status' => $status, 'message' => $message, 'data' => $data, ); //JSON编码数据 exit(json_encode($result)); }请输入代码
(3) Login.js
var login = { checkUser : function() { //获取登录页面中的用户名、密码 var userName=$('input[name="username"]').val(); var userPass=$('input[name="userpass"]').val(); if(!userName) { dialog.error("用户名不能为空"); } if(!userPass) { dialog.error("密码不能为空"); } var url="/stfjzd-12/index.php/Home/Index/checkUser"; var data={'username':userName,'userpass':userPass}; //执行异步请求 $.post(url,data,function(result){ if(result.status==0) { return dialog.error(result.message); } if(result.status==1) { if(data!=""){ //alert(data.username); $('#index_form2').remove(); $('#test').append(data.username); } return dialog.success(result.message,"/stfjzd-12/index.php/Home/Index/checkUser"); //alert(result.data['username']) ; } },'JSON'); }}请输入代码
Reply content:
1. Try to write a login program. This sign-in form is on the home page and requires a successful login to return the user name information (saved session value) and auto-hide the form on the home page using Ajax without refresh. Now through JS can indeed return the user information, but as long as the refresh, the user information and form will be restored to the original, how can be maintained long until the user clicks the logoff or the session disappears?
2. I can see the returned user information through the F12 XHR mode, which is also true through the jquery Append method and remove (), but the problem is that the information does not exist after the refresh and the form reverts to its original state.
2. Key code:
(1) IndexController.class.php:
Public Function CheckUser () {//Receive value $userName =$_post[' userName ']; $userPass =$_post[' Userpass ']; Null value detection->function if (!trim ($userName)) {return Show (0, ' username cannot be null '); } if (!trim ($userPass)) {return Show (0, ' password cannot be null '); }//Verify the authenticity of the user's password->model $res =d ("Stuser")->getuser ($userName); if (! $res [' username ']) {return Show (0, ' username does not exist '); }//Password handling->function if ($res [' Userpass ']!=getmd5pass ($userPass)) {return Show (0, ' password incorrect '); }//echo $res [' username ']; $_session (' username ', $res); Set SESSION $_session[' username ']= $res; Dump ($username); Var_dump ($username); Return Show (1, ' Login successful ', $this->getsessionnames ()); }//To determine the SESSION condition->index public Function getsessionnames () {if ($_session[' username ' [' username ']) {$username = $_session[' username ' [' username ']; $a = $this->ajaxreturn ($username); $this->assign (' username ', $username); return $username; Enter your code .}}
(2) function.php
function show($status,$message,$data=array()){ $result=array( 'status' => $status, 'message' => $message, 'data' => $data, ); //JSON编码数据 exit(json_encode($result)); }请输入代码
(3) Login.js
var login = {Checkuser:function () {//Get login page username, password var userName =$ (' input[name= ' username "]). Val (); var userpass=$ (' Input[name= ' Userpass "] '). Val (); if (!username) {dialog.error ("user name cannot be null"); } if (!userpass) {dialog.error ("password cannot be null"); } var url= "/stfjzd-12/index.php/home/index/checkuser"; var data={' username ': username, ' Userpass ': Userpass}; Executes an asynchronous request $.post (Url,data,function (Result) {if (result.status==0) {return Dialog.error (Result.messag e); } if (result.status==1) {if (data!= "") {//alert (data.username); $ (' #index_form2 '). Remove (); $ (' #test '). Append (Data.username); } return Dialog.success (Result.message, "/stfjzd-12/index.php/home/index/checkuser"); Alert (result.data[' username '); }}, ' JSON '); Enter your code
If you write with TP in the template with {$Think. Session.username} This assignment, through the JS assignment refresh certainly no
Page to make a hidden field to put information on it, the session information on the page only if you log in successfully after the request, page refresh will not trigger the request
public function index(){ //你显示页面的函数 $user=$this->getSessionNames(); $this->assing('user',$user); ...}
//index.html
//login.js
Landlord this problem, it seems that it is not very difficult! You can put the form of user information into the dynamic on it, the following:
//这个是php的处理代码块if($this->chen_user()) { //这个是验证是否登陆成功 $_SESSION['username'] = $username; //这里把用户信息存入session $_SESSION['sex] = $sex;}//这个是前端显示的
如果session中有username的值就输出用户信息 没有就不输出 姓名
Do you see that?