I am using my own session class, in that class, I am using some protected data members and some public data methods While I storing some variable on my session class Like
Mage::getSingleton('decision/session')->storeProductInfo('2');
Here is function implementation, $this->_productId is the private data member of my session class.
Public function storeProductInfo($product_id){
$this->_productId = $product_id;
return $this;
}
I am getting the stored variable by calling the below statement, it return me "null".
$product_stored_id = Mage::getSingleton('decision/session')->getStoredProductInfo();
public function getStoredProductInfo(){
return $this->_productId;
}
Even
Mage:getSingleton('decision/session')->setData('product_id', '2');
Didn't working. Can you please let me know where I am going wrong? I have to store some arrays in my session that's why I created my own session class to separately deal with my logic.
Use Magento Magic Method get and set
For that when your observer will call then you can create the session and set the value of that.
you can set the session using set, getting value using get and unset session using uns.
Mage::getSingleton('core/session')->setMySessionVariable('MyValue');
$myValue = Mage::getSingleton('core/session')->getMySessionVariable();
echo $myValue;
To Unset the session
Mage::getSingleton('core/session')->unsMySessionVariable();
$inputMessage = 'Hello World';
Mage::getSingleton('core/session')->setWelcomeMessage($inputMessage);
Now you want to echo the "welcome message" somewhere else in your code/site.
$outputMessage = Mage::getSingleton('core/session')->getWelcomeMessage();
echo $this->__($outputMessage);
Related
I have developing a basic session handler class. The issue is when ever I am setting session into any controller action, same session details are not accessible in other action/controller. It displays me empty array. Entire system is php namespace oriented.
Reference: Session Manager
We do save and retrieve session as below.
use Cygnite\Common\SessionManager\Session;
use Cygnite\Common\Encrypt;
$session = new Session(new Encrypt);
$session->save($key, $value);
$userDetails = $session->get($key);
var_dump($userDetails);
It works inside the same action and but when ever I am redirecting into some other controller ->action session doesn't display anything.
Can anyone help?
Probably you create in each controller new Session object. And it's quite possible you should create one Session object and inject it for example as constructor argument to other classes.
use Cygnite\Common\SessionManager\Session;
use Cygnite\Common\Encrypt;
$session = new Session(new Encrypt);
$session->save('something', 'My data');
class A
{
private $session;
public function __construct($session) {
$this->session = $session;
echo $this->sesssion->get('something'); // it should work
$x = new Session(new Encrypt);
echo $x->get('something'); // possible it won't work
}
How do I output the current session data.
This is my code in the controller
$this->load->library('session'); //load library of session; encryption key = key
$data['user'] = $this->session->set_userdata('email', 'email#hp.com');
The value that should be outputed in the view is email#hp.com
I tried this in the view.php but it is not working.
foreach($user as $row){
$row->email;
}
but it doesnt work. it says invalid argument.
Try like this,
$this->session->set_userdata('email', 'email#hp.com'); // set the session
$data['user'] = $this->session->userdata('email'); // get the session and store it in an array which is passed to the view
and in view file echo the value,
echo $user;
Otherwise, you can directly get the session value in view file like,
$objCI =& get_instance(); //now CI object can be used
$objCI->session->userdata('email');
set_userdata() is used to set data in session,
to get session data you should use userdata()
like
$this->session->set_userdata('email', 'email#hp.com');//to set data
$data['user']=$this->session->userdata('email');//to get sessiondata
And in view echo $user; will do.
here
I defined a session class,which uses values from the $_POST variable in an array called $sessionVars. When a user logs-in a new instance of the session class is created, a construct function sets session variables.I checked that this is working correctly.Problem: When i try to access those variables from a different page the session shows that its not started and those variables are undefined. Confused cause I thought $_SESSION being a super global means its accessible all the time(i.e scope doesn't matter) . I suspect im doing something wrong when I try to access the $_SESSION variables since they are in a class. I appreciate any help..thanks in advance.
class userSession{
public function __construct($sessionVars){
session_start();
$_SESSION['userEmail']=$sessionVars['user'];
$_SESSION['userID']=$sessionVars['userID'];
$_SESSION['userFolder']='users/user_'.$_SESSION['userID'];
}
/*just for housekeeping. not used in application*/
function showvars(){
echo $_SESSION['userEmail'].'<br><br>';
echo $_SESSION['userID'].'<br><br>';
echo $_SESSION['userFolder'];
$sessionID=session_id();
echo '<br><br>'.$sessionID;
}
}//**END USER SESSION
/*This is the login script that calls the session*/
include 'library.php';
$show=new render;
$show->index();
if(!isset($_POST['login']) ){
$show->usrLogin();
} else{
if(!empty($_POST['email'])){
$postVars=array('user'=>$_POST['email'],'pass'=>$_POST['password']);
$user=new user();
$data=$user->loginUser($postVars);
$currSession=new userSession($data);
}else{
die('No data in POST variable');}
}
/*file upload that needs the session[userFolder] variable*/
function file_upload(){
$userFolder=&$_SESSION['userFolder'];
echo '<hr>userFolder is : '.$userFolder;
function do_upload(){
if(!empty( $_FILES) ){
echo $userFolder.'<hr>';
$tmpFldr=$_FILES['upFile']['tmp_name'];
$fileDest=$userFolder.'/'.$_FILES['upFile']['name'];
if(move_uploaded_file($tmpFldr,$fileDest)){
echo 'file(s) uploaded successfully';
}
else{
echo 'Your file failed to upload<br><br>';
}
return $fileDest; //returns path to uploaded file
} else{die( 'Nothing to upload');}
}//END FUNCTION DO_UPLOAD,
/*Perform upload return file location*/
$fileLoc=do_upload();
return $fileLoc;
}
You need to instantiate an object of this class on every page that uses the session (or start the session manually). Also, you'll not want that constructor, instead some other way of setting vars. This is just for illustration of how it works with your current code, there are much better approaches:
class userSession {
public function __construct(){
session_start();
}
function set_login_vars($sessionVars){
$_SESSION['userEmail']=$sessionVars['user'];
$_SESSION['userID']=$sessionVars['userID'];
$_SESSION['userFolder']='users/user_'.$_SESSION['userID'];
}
}
//page1.php
$session = new userSession;
$session->set_login_vars($loginVars);
//page2.php
//you need to start the session, either with the class
$session = new userSession;
//or session_start();
print_r($_SESSION);
I am new to PHP and I want to save an object from this class which I can access in my webservice this object hold a sessions id which can be used for calling an API:
MyObject.php:
class MyObject {
private $sessionId = '';
private function __construct(){
$this->sessionId = '';
}
public static function getInstance() {
if (!$GLOBALS['MyObject']) {
echo 'creating new instance';
$GLOBALS['MyObject'] = new MyObject();
}
return $GLOBALS['MyObject'];
}
public function getSessionsId() {
if ($GLOBALS['MyObject']->sessionId == '') {
// Do curl to get key (works)
if (!curl_errno($curlCall)) {
$jsonObj = json_decode($result);
$sessionID = $jsonObj->session_id;
$GLOBALS['MyObject']->sessionId = $sessionID;
}
}
return $GLOBALS['MyObject']->sessionId;
}
}
Webservice GetKey.php
include 'MyObject.php';
$instance = MyObject::getInstance();
echo $instance->getSessionsId();
The I visit the GetKey.php file it always echoes 'creating new instance'
what you store in $GLOBALS will be destroyed when the page is loaded... that variable is available only when the response is being created...
You don't save an object and use it everywhere (for all users) .. you are in php :) and you can store it anywhere... but if you're in a function then it will be destroyed when you are outside the function...
And in webservices you don't use session because it won't exist at next operation call
In the case that you want to store that variable for multiple requests of the same user you can store it in $_SESSION and not in $GLOBALS...
In PHP everything is destroyed when the user gets the response... (only session is an exception)... a session is like in java or any other language... you have it / user and you can modify it at request time... you don't have something like applicationScope and store there everything for all users... everything is being recreated at every request
while using CRUD to one of my database driven website ..when we try to create / updating database using Create or Update option i am getting following error
Notice: Undefined index: crud_table in /home/sulabgqh/public_html/leads/controllers/grid_controller.php on line 89
i wnat to know what is the possible error , for reference below is the public declation of variable 'crud_table' table name
/********************* PUBLIC METHODS ********************/
public function setDbTable($table){
$_SESSION['crud_table'] = $table;
}
public function setPrimaryKey($primaryId) {
$_SESSION['crud_primary_key']=$primaryId;
}
and contructor is used here ..
/********************* CONSTRUCTOR ********************/
public function __construct(){
session_start();
$_SESSION['crud_table'] = null;
$_SESSION['crud_title_map'] = null;
$_SESSION['crud_actions'] = null;
$_SESSION['crud_primary_key']='id';
$_SESSION['crud_per_page']=10;
}
and Crete function goes like this
public function create(){
//setting from grid object
$table = $_SESSION['crud_table'];
$pk = $_SESSION['crud_primary_key'];
Seems like the Session-Variable isnt set, because of that it is "undefined". Try to check the varibale before u use it in the function like
if(isset($_SESSION['crud_table'])){
do stuff...
}
Is the Session startet correct?
Make sure you have put session_start() at the top of all scripts that are using sessions, this is the most common reason for session variables not being set.
session_start() doesn't just start a session, it checks if one is already running, if so it loads that one if not it creates a new one.