I need to get session_id() in ZF2? Using session_id() won't return any value. I tried Zend_Session::getId() but it didn't work, probably because I haven't included the path (which I don't know).
Can anyone help me to find a solution. Thanks in advance.
session_id() should work, but the correct way would be to access it from the session manager, e.g. (from a controller):
$sessionManager = $this->getServiceLocator()->get('Zend\Session\SessionManager');
$id = $sessionManager->getId();
If neither of these are returning a value for you then there's another problem somewhere.
first of all you have to use zend session container in your page that is.
use Zend\Session\Container;
now create an object of container by following.
$session = new Container('User');
now set value $userid or any other variable in session by following object.
$session->offsetSet('userId', $id);
now you can get it by following code.
$user_id=$session->offsetGet('userId');
Related
I have this code retrieved from var_dump($cache) where $cache is the variable holding the object: http://pastebin.com/9Ufpzbdn (Sorry, code is hard to format here but it's not that long).
How do I access property $_redis?
I tried $cache->_redis but it says it's undefined.
I just want to access it so I can use its set and fetch methods.
I have not touched any php for awhile now. Thanks in advance!
I think you can try:
$redis = $cache->getBackend()->_redis;
I have this code in the indexAction of a controller and that index is a start point for a process (include call to several controllers through Ajax):
$session = $request->getSession();
$currentData = [];
$session->set('currentData', $currentData);
Now suppose that I need to set a new value for currentData in another controller, I'm doing right now as:
$session = $request->getSession();
// get currentData from session for not override the values
$currentData = $session->get('currentData');
// sets the new value
$currentData['newValue'] = 1;
// save the var again and override currentData session
$session->set('currentData', $currentData);
Regarding this and as title says the question is easy: Do I need to start (call $session = $request->getSession() all the time whenever I need access to session) the session at any controller where I need to access it? Exists any best way to achieve this or I'm the one did all wrong? Any advice?
NOTE: I forgot to mention I'm talking and working with Symfony 2.6.3
You don't have to, however it is recommended. From the docs:
While it is recommended to explicitly start a session, a session will actually start on demand, that is, if any session request is made to read/write session data.
You do need to get the Session container by using $session = $request->getSession(), $session = $this->get('session'), or $session = new Symfony\Component\HttpFoundation\Session\Session();. This is not the same as starting a session and there's really no difference between the three ways.
This applies to any Symfony 2.x version.
I am new to codeigniter , In my program i want a variable need to be accessed by multiple controllers,
It's not a constant variable, value of variable changes ,
Sorry , My mistake
I want to store a JSON object to be precise
Pls help me to figure this out.
Thanks in advance.
You can create a base controller with an attribute for your variable, then have your controllers extend that base controller.
Option 1
Since you are using CodeIgniter and sessions then something like this could work out for you:
set it
$someJSONobject = 'JSON';
$this->session->set_userdata('GLOBAL_JSON', $someJSONobject);
retrieve it
$someJSONobject = $this->session->userdata('GLOBAL_JSON');
echo $someJSONobject->subitem;
Make sure you are storing sessions in a DB if you go with this option because Cookie space is VERY limited
Option 2
Even if you are not using CodeIgniters' session implementation then you can do something quite similar in native PHP:
$someJSONobject = 'JSON';
$_SESSION['GLOBAL_JSON'] = $someJSONobject;
Appending on Rooneyl's solution you may want to save that value on session which is easier to access from all end
Session docs
I have recently started building an application using Zendframework 2 , I have good experience in ZF1 , the major problem I am facing here with ZF2 is with sessions .
Here is the way that I am creating a session container .
use Zend\Session\Container;
// Session container creation: ( previously we were calling it as namespaces )
$session_user = new Container('user');
$session_user_errors = new Container('usererrors');
$session_user_shares = new Container('usershares');
Now Like this I have several containers ,
I could clear a key of a particular container like this
// Getting value from the session by key: ( get value from namespace )
$email = $session_user->offsetGet('email');
// Setting value in session: ( set value from namespace )
$session_user->offsetSet('username', 'abcd');
Now my problem is to clear an entire container which are set in several levels of my application .
If I try the below code Its clearing all of my session containers .
$session_user = new Container('user');
$session_user->getManager()->getStorage()->clear();
I want to clear only the container called 'user' which has many keys ( I dont know what all will be there at end ) . Is there a way to achieve this
I know I can do offsetunset on each key but thats not an Optimal solution I feel .
Please kindly suggest if any alternative way is there to clear a particular session container .
NOTE : - I am not using any of the third party modules like ZfcUser and Akrabat sessions
Thanks in advance for responding to this posting .
You almost had it, you just need to pass the namespace to the clear method
$session_user->getManager()->getStorage()->clear('user');
You can still treat the $_SESSION like an array, too, so the following also works
unset($_SESSION['user']);
The Solution posted By #Crisp worked like a Charm But here is the alternative way what I found after a research to solve this problem
use Zend\Session\SessionManager;
$sessionManager = new SessionManager();
//get array of sessions from storage
$array_of_sessions = $sessionManager->getStorage();
//Unset which ever container you want by passing its name ( ZF1 its called namespace )
unset($array_of_sessions['user']);
unset($array_of_sessions['usershares']);
unset($array_of_sessions['actions']);
I feel session manager is the one that we need to use to manage sessions whether to clear or read and container is one of the entity which is managed by session manager .
This may help others who are possessive in creating objects of each session container and call clear method .
To destroy all the sessions:
$session = new Container('base');
$session->getManager()->destroy();
or
use the simple php destroy function:
session_destroy();
This function clears all the sessions.
I hope this helps.
Following are the details for Destroying the session in Zend Framework 2:
Using Basic PHP Functionality
session_start() function starts the session.
session_destroy() function deletes ALL the data stored in session array.
Now using Zend Framework functionality:
For clear understanding lets first, create a session in Zend Framework and then make a Delete process.
Creating Session
use Zend\Session\Container;
$session_container = new Container('user_session');
$session_container->last_login = date('Y-m-d H:i:s');
$session_container->sess_token = trim(base64_encode(md5(microtime())),
"=");
Deleting Session
$session = new Container("user_session");
$session->getManager()->getStorage()->clear('user_session');
Where user_session is the name of session array key for storing the details.
Container::getDefaultManager()->getStorage()->clear('user');
In ZF2, for Container use;
Create container:
$sessionTokenizer = new Container('token');
Set variable into container
$token = $sessionTokenizer->token;
Destroy container (ONLY CONTAINER)
$sessionTokenizer->offsetUnset();
You can clear session like this:
$this->session->exchangeArray(array());
Experts,
i have a little Problem with my sessions. I want to save my login data into the session like this:
checklogin Controller
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
var_dump($this->session->set_userdata($user)); // return NULL? is this correct?
var_dump($this->session->all_userdata()); //return the correct data
Now the Session is saved up to a redirect.
Other Controller
var_dump($this->session->all_userdata()); // return session_id,… and a empty user_data array
I think I have the same Problem with the Shopping Cart Class.
Can anyone help?
I dont know why, but i think my story will help someone who facing the same problem.
My problem is just because I forgot to downgrade my Wamp php version, its php 7.1. Looks like my CI not support php 7.1 or higher. So just a few click to downgrade my php, my problem solved.
I added session_start() in the main index.php file. Worked like a charm afterwards.
try this..
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
$this->session->set_userdata('user',$user); //set session of users with a name user.
to get the session value u can do..
print_r($this->session->userdata('user')); // prints the user session array..
read more about sessions in CI
The set_userdata() function does not have a return value, so it is correct to display null on var_dump().
In order to save custom data to user's session you should check the manual here
The parameter it accepts must be an array.
hmm try add to ci->session and then get data
//assign the CodeIgniter object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
//add data to ci session
function test(){
$this->ci->session->set_userdata($user)
}
in the other controller assign the CodeIgniter object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
and get the ci user data
$this->ci->session->all_userdata()
I got a similar issue when I want to rerun an old website that was built formerly using CI 3.0.6 on a server running PHP 7.3.
I have set a session in one controller.
$this->session->set_userdata('logged_in', TRUE);
Then, the other controllers that are accessed after that cannot retrieve the information while a new record in the session table in the database is stored.
empty($_SESSION['logged_in']); // true
empty($this->session->logged_in); // true
The solution that works for me is as follows.
Set a higher size for the id column in the CI session table. For example, our session table name is ci_sessions.
ALTER TABLE ci_sessions CHANGE id id varchar(128) NOT NULL;
Replace the current CI system directory with newer version. In my case, it works with the system directory from CI 3.1.10.
In summary, a newer CI version is required to be able to interact well with newer PHP (7.1+). It has a longer id value for the stored session record. Rather than downgrading your PHP version, it is better to upgrade the CI version.