将会话变量传递给cakephp中的其他页面

时间:2016-10-04 04:25:19

标签: php session 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: -

class dtHelper {     public $ aaData; } class 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变量,但我不知道如何在另一页中读取它们。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

您正在访问未定义的索引memberid,它应该是MemberID

value="<?= $_SESSION["MemberID"]; ?>"

答案 1 :(得分:1)