thinkajax返回頁(yè)面session信息(PHP)
Jul 06, 2016 pm 01:52 PM
1.在嘗試仿寫一個(gè)登錄程序。這個(gè)登錄表單在首頁(yè),要求登錄成功后在首頁(yè)使用ajax無(wú)刷新返回用戶名信息(保存的session值)以及自動(dòng)隱藏表單?,F(xiàn)在通過(guò)js的確能返回用戶信息,但是只要刷新,用戶信息和表單就會(huì)恢復(fù)原狀了,怎么可以維持長(zhǎng)久直到用戶點(diǎn)擊注銷或session消失后?
2.我通過(guò)F12的xhr模式能看到返回的用戶信息,通過(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,'用戶名不能為空'); } 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() { //獲取登錄頁(yè)面中的用戶名、密碼 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è)登錄表單在首頁(yè),要求登錄成功后在首頁(yè)使用ajax無(wú)刷新返回用戶名信息(保存的session值)以及自動(dòng)隱藏表單?,F(xiàn)在通過(guò)js的確能返回用戶信息,但是只要刷新,用戶信息和表單就會(huì)恢復(fù)原狀了,怎么可以維持長(zhǎng)久直到用戶點(diǎn)擊注銷或session消失后?
2.我通過(guò)F12的xhr模式能看到返回的用戶信息,通過(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,'用戶名不能為空'); } 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() { //獲取登錄頁(yè)面中的用戶名、密碼 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è)賦值,通過(guò)js賦值刷新肯定沒有了
頁(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)題,好像也不是很難嘛!把你存放用戶信息的表單弄成動(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)

??? ??











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

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

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

PHP Page Caching? ????? ??? ????? ??? ?? ? ??? ??? ??????. 1. ?? ?? ??? ?? HTML ??? ???? ?? ?? ?? ???? ???? ??? ? ?? ???? ?? ? ????. 2. Opcache? PHP ????? ??? ??? ????? ???? ???? ?? ???? ??????. 3. ?? ????? ?? ???? ?? URL ?? ??? ?? ??? ?????? ?? ? ??? ? ???? ??????. 4. PhpfastCache? ?? ??? ?? ?????? ???? ??? ????? ?? ???? ????? ??? ? ????. ??? ??? ???? PHP ????? ?? ??? ????? ??? ? ? ????.

toquicklytestaphpcodesnippet, useanonlinephpsandbox like3v4l.orgorphpize.onlineforinstantexecutionsupoutsetup; runcodelocallywithphclibycreatinga.phpfileandexecutingititviathemerminal; setupal allocalte-ronone-liners;

PHP ??? ??????? ?? ??? ??? ??? ??? ?? ??? ?? ??? ????. ??? ?? ?????. 1. ?? PHP ?? ? ???? ??? ???? ?? ? ?? phpinfo.php ??? ???? ? ? ????. 2. ??? ? ??? ???? ??????. 8.2 ?? 8.1? ???? ?? ????. Linux ???? Package Manager? ???? MacOS ???? Homebrew? ?????. 3. ?? ?? ? ???? ???????? php.ini? ?????? ??? ??? ??????. 4. ? ???? ????? ?? ??? ????? ?? ??? ???? ??? ??? ??? ??????. ?? ??? ??? ???? ???? ?????? ????? ?? ? ? ????.

TopreventCSRFattacksinPHP,implementanti-CSRFtokens.1)Generateandstoresecuretokensusingrandom_bytes()orbin2hex(random_bytes(32)),savethemin$_SESSION,andincludetheminformsashiddeninputs.2)ValidatetokensonsubmissionbystrictlycomparingthePOSTtokenwiththe

PHP ?? ??? ????? ??? ??? ???? ??? ???? ???????. ?? ???? PHP ?? ???? ? ?? ?? ??? ?????. ? ?? (Apache ?? Nginx), PHP ?? ? ?????? (? : MySQL/MariaDB); ???? XAMPP ?? MAMP? ?? ?? ???? ???? ?? ????? ????? ?? ????. XAMPP? Windows ? MacOS? ?????. ?? ? ???? ??? HTDOCS ????? ???? LocalHost? ?? ??????. mAMP? MAC ????? ???? PHP ??? ??? ??? ????? ?? ???? ??? ???? ????. ④ ?? ???? MacOS/Linux ????? Homebrew? ???? ??? ? ????.
