1. try to copy a logon program. This logon form is on the homepage. after successful logon, you are required to use ajax to return the username information (saved session value) and automatically hide the form. Currently, the user information can be returned through js, but as long as the user information and forms are refreshed, the user information and forms will be restored... 1. a login program is being counterfeited. This logon form is on the homepage. after successful logon, you are required to use ajax to return the username information (saved session value) and automatically hide the form. Now, JavaScript can indeed return user information, but as long as the user information is refreshed, the user information and forms will be restored to their original state. how can this problem be maintained until the user clicks logout or the session disappears?
2. I can see the returned user information through the xhr mode of F12. It can also be achieved through the jquery append method and remove (), but the problem is that after refreshing, the information does not exist and the form is restored to its original state.
2. key code:
(1) IndexController. class. php:
Public function checkUser () {// receiving value $ userName = $ _ POST ['username']; $ userPass =$ _ POST ['userpass']; // null value detection-> function if (! Trim ($ userName) {return show (0, 'user name cannot be blank ');} if (! Trim ($ userPass) {return show (0, 'password cannot be blank ');} // check the authenticity of user passwords-> Model $ res = D ("Stuser")-> getUser ($ userName); if (! $ Res ['username']) {return show (0, 'username does not exist');} // password processing-> function if ($ res ['userpass']! = GetMd5Pass ($ userPass) {return show (0, 'incorrect password');} // echo $ res ['username']; // $ _ SESSION ('username', $ res); // Set session $ _ SESSION ['username'] = $ res; // dump ($ username ); // var_dump ($ username); return show (1, 'logon successfully', $ this-> getSessionNames ());} // Determine the session state-> index public function getSessionNames () {if ($ _ SESSION ['username'] ['username']) {$ username = $ _ SESSION ['username'] ['username']; // $ a = $ this-> ajaxReturn ($ username ); // $ this-> assign ('username', $ username); return $ username ;}} enter the code
(2) function. php
Function show ($ status, $ message, $ data = array () {$ result = array ('status' => $ status, 'message' => $ message, 'data' => $ data,); // JSON encoded data exit (json_encode ($ result);} enter the code
(3) login. js
Var login = {checkUser: function () {// Obtain the userName and password var username =$ ('input [name = "userName"] ') on the logon page. val (); var userPass = $ ('input [name = "userpass"] '). val (); if (! UserName) {dialog. error ("userName cannot be blank");} if (! UserPass) {dialog. error ("password cannot be blank");} var url = "/stfjzd-12/index. php/Home/Index/checkUser "; var data = {'username': username, 'userpass': userpass}; // execute asynchronous request $. 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') ;}} enter the code
Reply content:
1. try to copy a logon program. This logon form is on the homepage. after successful logon, you are required to use ajax to return the username information (saved session value) and automatically hide the form. Now, JavaScript can indeed return user information, but as long as the user information is refreshed, the user information and forms will be restored to their original state. how can this problem be maintained until the user clicks logout or the session disappears?
2. I can see the returned user information through the xhr mode of F12. It can also be achieved through the jquery append method and remove (), but the problem is that after refreshing, the information does not exist and the form is restored to its original state.
2. key code:
(1) IndexController. class. php:
Public function checkUser () {// receiving value $ userName = $ _ POST ['username']; $ userPass =$ _ POST ['userpass']; // null value detection-> function if (! Trim ($ userName) {return show (0, 'user name cannot be blank ');} if (! Trim ($ userPass) {return show (0, 'password cannot be blank ');} // check the authenticity of user passwords-> Model $ res = D ("Stuser")-> getUser ($ userName); if (! $ Res ['username']) {return show (0, 'username does not exist');} // password processing-> function if ($ res ['userpass']! = GetMd5Pass ($ userPass) {return show (0, 'incorrect password');} // echo $ res ['username']; // $ _ SESSION ('username', $ res); // Set session $ _ SESSION ['username'] = $ res; // dump ($ username ); // var_dump ($ username); return show (1, 'logon successfully', $ this-> getSessionNames ());} // Determine the session state-> index public function getSessionNames () {if ($ _ SESSION ['username'] ['username']) {$ username = $ _ SESSION ['username'] ['username']; // $ a = $ this-> ajaxReturn ($ username ); // $ this-> assign ('username', $ username); return $ username ;}} enter the code
(2) function. php
Function show ($ status, $ message, $ data = array () {$ result = array ('status' => $ status, 'message' => $ message, 'data' => $ data,); // JSON encoded data exit (json_encode ($ result);} enter the code
(3) login. js
Var login = {checkUser: function () {// Obtain the userName and password var username =$ ('input [name = "userName"] ') on the logon page. val (); var userPass = $ ('input [name = "userpass"] '). val (); if (! UserName) {dialog. error ("userName cannot be blank");} if (! UserPass) {dialog. error ("password cannot be blank");} var url = "/stfjzd-12/index. php/Home/Index/checkUser "; var data = {'username': username, 'userpass': userpass}; // execute asynchronous request $. 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') ;}} enter the code
If you use the value {$ Think. session. username} in the template written in tp, the refresh through the js value assignment will surely disappear.
It's okay to hide the domain information on the page. the session information will be requested only after you log on to the page. page refresh will not trigger the request.
Public function index () {// function $ user = $ this-> getSessionNames (); $ this-> assing ('user ', $ user );...}
//index.html《script》 $(function(){ var username="{{$user}}"; if(username != ""){ $('#index_form2').remove(); $('#test').append(username); } }) 《script》
//login.js
It seems that this problem is not very difficult! Make the form that stores user information dynamic, as shown below:
// This is the php processing code block if ($ this-> chen_user () {// this is to verify whether the login is successful $ _ SESSION ['username'] = $ username; // store the user information in session $ _ SESSION ['sex] = $ sex;} // This is displayed on the front end.
If there is a username value in the session, no user information is output.Name
Check whether this is the case?