thinkajax返回頁(yè)面session信息(PHP)
Jul 06, 2016 pm 01:52 PM
1.在嘗試仿寫(xiě)一個(gè)登錄程序。這個(gè)登錄表單在首頁(yè),要求登錄成功后在首頁(yè)使用ajax無(wú)刷新返回用戶(hù)名信息(保存的session值)以及自動(dòng)隱藏表單?,F(xiàn)在通過(guò)js的確能返回用戶(hù)信息,但是只要刷新,用戶(hù)信息和表單就會(huì)恢復(fù)原狀了,怎么可以維持長(zhǎng)久直到用戶(hù)點(diǎn)擊注銷(xiāo)或session消失后?
2.我通過(guò)F12的xhr模式能看到返回的用戶(hù)信息,通過(guò)jquery的append方法和remove()也確實(shí)可以實(shí)現(xiàn),不過(guò)問(wè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,'用戶(hù)名不能為空'); } if(!trim($userPass)){ return show(0,'密碼不能為空'); } //對(duì)用戶(hù)密碼真實(shí)性進(jìn)行檢驗(yàn)->Model $res=D("Stuser")->getUser($userName); if(!$res['username']){ return show(0,'用戶(hù)名不存在'); } //密碼處理->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() { //獲取登錄頁(yè)面中的用戶(hù)名、密碼 var userName=$('input[name="username"]').val(); var userPass=$('input[name="userpass"]').val(); if(!userName) { dialog.error("用戶(hù)名不能為空"); } 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.在嘗試仿寫(xiě)一個(gè)登錄程序。這個(gè)登錄表單在首頁(yè),要求登錄成功后在首頁(yè)使用ajax無(wú)刷新返回用戶(hù)名信息(保存的session值)以及自動(dòng)隱藏表單?,F(xiàn)在通過(guò)js的確能返回用戶(hù)信息,但是只要刷新,用戶(hù)信息和表單就會(huì)恢復(fù)原狀了,怎么可以維持長(zhǎng)久直到用戶(hù)點(diǎn)擊注銷(xiāo)或session消失后?
2.我通過(guò)F12的xhr模式能看到返回的用戶(hù)信息,通過(guò)jquery的append方法和remove()也確實(shí)可以實(shí)現(xiàn),不過(guò)問(wè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,'用戶(hù)名不能為空'); } if(!trim($userPass)){ return show(0,'密碼不能為空'); } //對(duì)用戶(hù)密碼真實(shí)性進(jìn)行檢驗(yàn)->Model $res=D("Stuser")->getUser($userName); if(!$res['username']){ return show(0,'用戶(hù)名不存在'); } //密碼處理->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() { //獲取登錄頁(yè)面中的用戶(hù)名、密碼 var userName=$('input[name="username"]').val(); var userPass=$('input[name="userpass"]').val(); if(!userName) { dialog.error("用戶(hù)名不能為空"); } 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寫(xiě)的在模板中用{$Think.session.username}這個(gè)賦值,通過(guò)js賦值刷新肯定沒(méi)有了
頁(yè)面搞個(gè)隱藏域放信息就好啦,session信息在頁(yè)面上只有你登錄成功以后會(huì)去請(qǐng)求,頁(yè)面刷新是不會(huì)觸發(fā)請(qǐng)求的
<code>public function index(){ //你顯示頁(yè)面的函數(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è)問(wèn)題,好像也不是很難嘛!把你存放用戶(hù)信息的表單弄成動(dòng)態(tài)的就可以了吧,具體如下:
<code>//這個(gè)是php的處理代碼塊 if($this->chen_user()) { //這個(gè)是驗(yàn)證是否登陸成功 $_SESSION['username'] = $username; //這里把用戶(hù)信息存入session $_SESSION['sex] = $sex; } //這個(gè)是前端顯示的 <?php if($_SESSION['username']) { ?> 如果session中有username的值就輸出用戶(hù)信息 沒(méi)有就不輸出 <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)

??? ??











tostaycurrentwithphpdevelopments ? bestpractices, followkeynewssources lifephp.netandphpweekly, adgytwithcommunitiesonforumsandconferences, readlingupdated andgrad indewfeatures, andreadorcontributetoopensourceproceprosts.first

phpbecamepupularforwebdevelopmentduetoiteofleneflening, whithhtml, wididepreadhostingsupport, andalargeecosystemincludingframeworkslikelaravelandcmsplatformsformslikewordpress.itexcelsinhandlingformsubmissions, managingussess, interptisussivers, ?? ???

TOSETTHERIGHTTIMEZONEINPHP, usedate_default_timezone_set () functionattStartOfyourscriptwitHavalidInlifiersuchas'America/new_york'.1.edate_default_timezone_set () beforeanydate/timeFunctions.2

TovalidateUserInputInphp, useBuilt-invalidationFunctions likefilter_var () ? filter_input (), applyRegulArexPessionSforCustomFormatsSuchasUsUserPhonEnumbers, CheckDatatypesFornumericValuesLikeAgeArPrice, setLtrimtsAnspacetReopeTopeTopeTopePeTopePeTopePeTopeTopeTopeTlyout

thephpfunctionserialize () andunserialize () areusedtoconvertcomplexDattoresintostorasandabackagain.1.serialize () c onvertsDatalikeCarraysorObjectSrayStringStringStrainingTainingTypeanDtuctureIncomation.2.

PHP ??? HTML ??? ???? ? ??? ??? ???? .php? ?? ??? ??? ???? ??? ???? ?? ?? ? ? ??? ??????. ?? ??? ???? PHP ??? ???? HTML? ?? ???? ?? ???? ??????. ?? ??? ???? PHP ? HTML? ?? ? ???? ??? ???? ?? ?? ??? ??? ? ????. ?? ???, ?? ?? ?? ?? ?? ? ?? ???? ?? ??? ??? ?? ?? ?? ? ?? ??????? ???????.

???? ???? ?? PHP ??? ???? ??? ??, ???? ??? ?? ??? ?? ??? ????. ??? ??? ???? ? ?????. 1. $ userData ? calculateToTalPrice ()? ?? ??? ??, ?? ? ??? ??? ??????. 2. PSR-12 ?? ?? ?? ???? ?????. 3. ??? ?? ?? ??? ???? MVC ?? Laravel ??? ????? ???? ??????. 4. ?? ??? ??? ??? ?? ???? ??? ?? ???? ????. 5. ? ???? ??? ???? ????? ??? ???? ?? ??, ?? ? ? ??? ??????. 6. ??? ??? ??, ??? ?? ??, ??? ?? ? ?? ??? ????. ??? ??? ?? ??, ?? ??? ? ?? ?? ? ???? ??????.

?, youcanrunsqlqueriesusingphp, andtheprocessinvolveschoingadatabaseexextension, executequeriessafely, andclosingconnectionswhendone.todothis, firstchoosebetween -mysqliorpdo, withpdobeingmoreflectibleblyblyblyblyblyblyblyblyblyblyblyblyblyblyblybledingmuttitatabas
