duddosai duddosai - 2 months ago 18
PHP Question

Passing session variables to other pages in cakephp

I'm trying to pass session variables in cakephp. This is how I tried:

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 extends 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"]; ?>">


This is how, I tried to write the values to the session variables. I wanted to use these session variables in different pages. I've assigned the values to those variables to those
$_SESSION
variables, but I don't know how to read them in another page. How can I do that?

Answer

You're accessing an undefined index memberid, it is supposed to be MemberID.

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