[英]Passing session variables to other pages in cakephp
我正在嘗試在cakephp中傳遞會話變量。 這是我嘗試的方法:
LoginController.php:-
<?php
namespace App\Controller;
session_start ();
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
class LoginController extends AppController
{
public function loginentry(){
$loginid = $this->request->data('LoginID');
$password = $this->request->data('Password');
$login = TableRegistry::get('login');
$result = $login->loginuser($loginid, $password);
echo json_encode($result);
//extract($result);
//echo "\$a = memberid; \$b = membertype; \$c = roleid";
if($result == false){
$this->Flash->error(__("Incorrect Login"));
} else {
$_SESSION ["MemberID"] = $result[0]->memberid;
$_SESSION ["MemberType"] = $result[0]->membertype;
$_SESSION ["MemberName"] = $result[0]->membername;
$_SESSION ["RoleID"] = $result[0]->roleid;
// $this->redirect(['controller' => 'Mom', 'action' => 'momlist']);
}
}
}
MomController.php:-
dtHelper類{public $ aaData; }類MomController擴展了AppController {
public function initialize() {
parent::initialize ();
}
public function editor($id = NULL)
{
$this->set('mid', $id);
if($this->request->is('post')){
$momid = $this->request->data( 'MomID' );
$momtitle = $this->request->data ( 'MomTitle' );
$customerid = $this->request->data ( 'CustomerID' );
$meetingheldon = $this->request->data ( 'MeetingHeldOn' );
$session = $this->request->data ( 'Session' );
$meetingstartedat = $this->request->data ( 'MeetingStartedAt' );
$meetingendedat = $this->request->data ( 'MeetingEndedAt' );
$participantsfromcustomerside = $this->request->data ( 'ParticipantsFromCustomerSide' );
$staffid = implode(',', $this->request->data ( 'StaffID' ));
$location = $this->request->data ( 'Location' );
$Mom = TableRegistry::get ( 'Mom' );
if(isset($id) && $id > 0){
$result = $Mom->update ( $momid, $momtitle, $customerid, $meetingheldon, $session, $meetingstartedat, $meetingendedat, $participantsfromcustomerside, $staffid, $location);
} else {
$result = $Mom->create ( $momtitle, $customerid, $meetingheldon, $session, $meetingstartedat, $meetingendedat, $participantsfromcustomerside, $staffid, $location);
}
$message=$result['message'];
if($result['webid']>0){
$this->Flash->success($message);
return $this->redirect ( [ 'Controller' => 'Mom','action' => 'momlist'] );
}else{
if(isset($id) && $id > 0)
{
$result = (object) array( 'momid'=>$momid,'momtitle'=>$momtitle,'customerid'=>$customerid,'meetingheldon'=>$meetingheldon,'session'=>$session,'meetingstartedat'=>$meetingstartedat,'meetingendedat'=>$meetingendedat,'participantsfromcustomerside'=>$participantsfromcustomerside,'staffid'=>$staffid,'location'=>$location);
$this->set(array('row'=>$result,'mode'=>'Update'));
}
else{
$result = (object) array( 'momtitle'=>$momtitle,'customerid'=>$customerid,'meetingheldon'=>$meetingheldon,'session'=>$session,'meetingstartedat'=>$meetingstartedat,'meetingendedat'=>$meetingendedat,'participantsfromcustomerside'=>$participantsfromcustomerside,'staffid'=>$staffid,'location'=>$location);
$this->set(array('row'=>$result,'mode'=>'Save'));
}
$this->Flash->error($message);
}
} else {
$mom = TableRegistry::get('Mom');
$momlist = $mom->getlist();
$this->set(array('momlist' => $momlist));
$customer = TableRegistry::get('customer');
$customerlist = $customer->getlist();
$this->set(array('customerlist' => $customerlist));
$project = TableRegistry::get('mom');
$projectlist = $project->getprojectlist(1);
$this->set(array('projectlist' => $projectlist));
if(isset($id) && $id > 0)
{
$mom=TableRegistry::get('mom');
$result = $mom->getbyid($id);
$this->set(array('row' => $result, 'mode' => 'Update'));
}
else{
$this->set( array('mode'=>'Save'));
}
}
$this->render ();
}
}
editor.ctp:-
<input type="text" class="form-control" id="MomID" name="MomID" value="<?=isset($row->momid)?$row->momid:0 ?>">
<input type="text" class="form-control" id="Session" name="Session" value="<?= $_SESSION["memberid"]; ?>">
這就是我嘗試將值寫入會話變量的方式。 我想在不同頁面中使用這些會話變量。 我已經將這些變量的值分配給了$_SESSION
變量,但是我不知道如何在另一頁中讀取它們。 我怎樣才能做到這一點?
您正在訪問未定義的索引memberid
,它應該是MemberID
。
value="<?= $_SESSION["MemberID"]; ?>"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.