thinkajax返回頁面session信息(PHP)
Jul 06, 2016 pm 01:52 PM
1.在嘗試仿寫一個(gè)登錄程序。這個(gè)登錄表單在首頁,要求登錄成功后在首頁使用ajax無刷新返回用戶名信息(保存的session值)以及自動(dòng)隱藏表單。現(xiàn)在通過js的確能返回用戶信息,但是只要刷新,用戶信息和表單就會(huì)恢復(fù)原狀了,怎么可以維持長久直到用戶點(diǎn)擊注銷或session消失后?
2.我通過F12的xhr模式能看到返回的用戶信息,通過jquery的append方法和remove()也確實(shí)可以實(shí)現(xiàn),不過問題是刷新后信息不存在和表單恢復(fù)原狀。
2.關(guān)鍵代碼:
(1)IndexController.class.php:
<code>public function checkUser(){ //接收值 $userName=$_POST['username']; $userPass=$_POST['userpass']; //空值檢測(cè)->function if(!trim($userName)){ return show(0,'用戶名不能為空'); } if(!trim($userPass)){ return show(0,'密碼不能為空'); } //對(duì)用戶密碼真實(shí)性進(jìn)行檢驗(yàn)->Model $res=D("Stuser")->getUser($userName); if(!$res['username']){ return show(0,'用戶名不存在'); } //密碼處理->function if($res['userpass']!=getMd5Pass($userPass)){ return show(0,'密碼不正確'); } //echo $res['username']; //$_SESSION('username',$res); //設(shè)置session $_SESSION['username']=$res; //dump($username) ; //var_dump($username); return show(1,'登錄成功',$this->getSessionNames()); } //判斷session情況->index public function getSessionNames(){ if($_SESSION['username']['username']){ $username = $_SESSION['username']['username']; //$a=$this->ajaxReturn($username); //$this->assign('username',$username); return $username; } }請(qǐng)輸入代碼</code>
(2)function.php
<code>function show($status,$message,$data=array()){ $result=array( 'status' => $status, 'message' => $message, 'data' => $data, ); //JSON編碼數(shù)據(jù) exit(json_encode($result)); }請(qǐng)輸入代碼</code>
(3)login.js
<code>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}; //執(zhí)行異步請(qǐng)求 $.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'); } }請(qǐng)輸入代碼</code>
回復(fù)內(nèi)容:
1.在嘗試仿寫一個(gè)登錄程序。這個(gè)登錄表單在首頁,要求登錄成功后在首頁使用ajax無刷新返回用戶名信息(保存的session值)以及自動(dòng)隱藏表單?,F(xiàn)在通過js的確能返回用戶信息,但是只要刷新,用戶信息和表單就會(huì)恢復(fù)原狀了,怎么可以維持長久直到用戶點(diǎn)擊注銷或session消失后?
2.我通過F12的xhr模式能看到返回的用戶信息,通過jquery的append方法和remove()也確實(shí)可以實(shí)現(xiàn),不過問題是刷新后信息不存在和表單恢復(fù)原狀。
2.關(guān)鍵代碼:
(1)IndexController.class.php:
<code>public function checkUser(){ //接收值 $userName=$_POST['username']; $userPass=$_POST['userpass']; //空值檢測(cè)->function if(!trim($userName)){ return show(0,'用戶名不能為空'); } if(!trim($userPass)){ return show(0,'密碼不能為空'); } //對(duì)用戶密碼真實(shí)性進(jìn)行檢驗(yàn)->Model $res=D("Stuser")->getUser($userName); if(!$res['username']){ return show(0,'用戶名不存在'); } //密碼處理->function if($res['userpass']!=getMd5Pass($userPass)){ return show(0,'密碼不正確'); } //echo $res['username']; //$_SESSION('username',$res); //設(shè)置session $_SESSION['username']=$res; //dump($username) ; //var_dump($username); return show(1,'登錄成功',$this->getSessionNames()); } //判斷session情況->index public function getSessionNames(){ if($_SESSION['username']['username']){ $username = $_SESSION['username']['username']; //$a=$this->ajaxReturn($username); //$this->assign('username',$username); return $username; } }請(qǐng)輸入代碼</code>
(2)function.php
<code>function show($status,$message,$data=array()){ $result=array( 'status' => $status, 'message' => $message, 'data' => $data, ); //JSON編碼數(shù)據(jù) exit(json_encode($result)); }請(qǐng)輸入代碼</code>
(3)login.js
<code>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}; //執(zhí)行異步請(qǐng)求 $.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'); } }請(qǐng)輸入代碼</code>
如果你用tp寫的在模板中用{$Think.session.username}這個(gè)賦值,通過js賦值刷新肯定沒有了
頁面搞個(gè)隱藏域放信息就好啦,session信息在頁面上只有你登錄成功以后會(huì)去請(qǐng)求,頁面刷新是不會(huì)觸發(fā)請(qǐng)求的
<code>public function index(){ //你顯示頁面的函數(shù) $user=$this->getSessionNames(); $this->assing('user',$user); ... }</code>
<code>//index.html <script> $(function(){ var username="{{$user}}"; if(username != ""){ $('#index_form2').remove(); $('#test').append(username); } }) </script></code>
<code> //login.js</code>
樓主這個(gè)問題,好像也不是很難嘛!把你存放用戶信息的表單弄成動(dòng)態(tài)的就可以了吧,具體如下:
<code>//這個(gè)是php的處理代碼塊 if($this->chen_user()) { //這個(gè)是驗(yàn)證是否登陸成功 $_SESSION['username'] = $username; //這里把用戶信息存入session $_SESSION['sex] = $sex; } //這個(gè)是前端顯示的 <?php if($_SESSION['username']) { ?> 如果session中有username的值就輸出用戶信息 沒有就不輸出 <td>姓名</td> <td><?php echo $_SESSION['username'];?></td> <?php } ?></code>
你看看是不是這樣?

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











PHP ?? ???? ???? ????? ?? ? ??? ???? ?? ?? ? ??? ???? ?? ??? ?????? ??? ??? ? ? ???????. 1. ??? ?? CSRF? ???? ?? ??? ??? ???? ?????? ??? ???? FINFO_FILE? ?? ?? MIME ??? ?????. 2. ??? ??? ??? ???? ??? ?? ??? ?? ? WEB ????? ??? ???? ??????. 3. PHP ?? ??? ?? ? ?? ???? NGINX/APACHE? ??? ????? ?? ???? ?????. 4. GD ?????? ??? ? ?? ???? ??? ?? ??? ?? ????.

Ageneratorinphpisamemory- ???? Way-Erate-Overgedatasetsetsbaluesoneatimeatimeatimeatimallatonce.1.generatorsuseTheyieldKeywordTocroadtOpvaluesondemand, RetingMemoryUsage.2

AJAX ??? ?? ( '?? : ...')? ???? ?? ??? ????? ???? ??? ????? ???? ?? ?????. AJAX ???? ??? ?? ? 302 ?? ?? ? ?? ?? ??? ?? ??? ????? ?? ?? ???? ?????. ???? ??? ????. 1. PHP?? JSON ???? ???? ?? URL? ?????. 2. ??? ?? Ajax ???? ???? ??? ???? Window.location.href? ???? ?????. 3. PHP ??? ?? ?? ??? ??? ?? JSON? ???? ??????. 4. ??? ??? ??? ????? ??? CORS ??? ???????. 5. ?? ??? ????? ?? ???? ????? ??? ??? ? ????.

PHP?? ?? ??? ????? ?? ??? ??????. 1. HTTPS? ???? ?? ? ?? ??? ?????? .Cookie_Secure = 1?? php.ini; 2. Httponly, Secure ? Samesite? ??? ?? ?? ??? ??????. 3. ???? ?????? ??? ?? ? ? Session_Regenerate_id (true) Call Session_Regenerate_id (true) SessionID ??? ?????. 4. ?? ????? ???? GC_MAXLIFETIME? ????? ???? ???? ?? ??? ??????. 5. SessionID? URL? ????? ?? ???? Session.use_only? ??????.

PHP??? ???? ??? ?? ?? ????? ???? ??? ?? ??? ??? ?? ? ??? ??? ???? ?????. ???? 0?? ???? ?? ??? ???? ? ?? ???? ?? ?? ? ? ????. MB_SUBSTR? ?? ??? ??? ???????. ? : $ str = "hello"; echo $ str [0]; ?? H; ??? MB_SUBSTR ($ str, 1,1)? ?? ??? ??? ??? ??????. ?? ???????? ???? ??? ???? ?? ???? ?? ?? ???? ?????? ??? ????? ?? ??? ?? ??? ???? ???? ?? ????.

urlencode () ??? ???? URL-SAFE ???? ????? ? ????, ? ??? ? ?? (-, _ ?.)? ??? ??? ?? ? ?? 2 ?? 16 ?? ??? ?????. ?? ??, ??? ????? ???? ????!? ???? ???? UTF-8 ??? ???? ?????. ???? ?? URL ??? ???? ??? ?? ?? ? ? ?? URL? ?? ?????????. ?? ????? ?? URL? ?? ??? ?? ??? ???? rawurlencode () ??? ???????. ?? ?? ??? ?? ? ? http_build_query ()? ???? ? ??? ???? ?????? UrlenCode ()? ???? ???? ???? ???? ??? ? ????. ??

PHP?? ????? ??? ??? ?? ?? ???? ??? strrpos () ??? ???? ????. 1. strrpos () ??? ???? ?? ????? ?? ???? ??? ?? ???? ?? ????. ???? ??? ??? ?????. ??? strrpos??? ($ haystack, $ ??, $ offset = 0). 2. ??? ?? ???? ?? strripos () ??? ???? ??? ???? ?? ??? ??? ? ????. 3. ???? ?? ?? ??? ??? ?? MBString ??? MB_Strrpos () ??? ???? ??? ?? ?? ?? ??? ??????????. 4. strrpos ()? f? ????? ?? ??????
