Accessibility of a session in a view - php

I have created a Zend session in a view:
$user = Tools::getUserByLoginAndPassword($form);
if ($user instanceof Membre)
{
$sessionUser = new Zend_Session_Namespace('user');
$sessionUser->data = $user;
$this->_redirect('/adresse');
}
I have tested $sessionUser->data, it contains the right data.
But i need to get this session in another view, and there, it's not working anymore...
for instance
<?php var_dump($sessionUser->data->prenom); ?>
displays "NULL"
it worked fine in the previous view but not in this one.
I have placed my Zend_Session::start();
in the init() function of my controller...
thanks in advance for your help

If you want to use your session in another view, you have to instantiate it again. So if you use the following code you should be able to access your data:
<?php
$session = new Zend_Session_Namespace('user');
Zend_Debug::dump($session->data);
?>

Related

Can the session expire when using a function of an included file?

I've got this session variable, which was initiated in index.php
<?php
$sid = session_id();
if (empty($sid) {
session_start();
}
//value of the variable can be changed and saved before coming back to the index
if(!isset($_SESSION['context'])){
$_SESSION['context'] = 1;
}
//...
?>
Later a script is used to create or update a database entry.
entryupdate.php:
<?php
include_once("template.php");
/*....*/
$sid = session_id();
if (empty($sid)) {
session_start();
}
/*....*/
//Create a new entry, context is not given in the $input_object at this point.
$template = new Template($input_object);
$context = $template->getContext();
?>
template.php is a script where only the Template Object is defined, so I don't call $session_start in it, my rationale being that it would always be called by the scripts including it, as it is in the case I'm describing here. Here is the relevant code:
<?php
class Template
{
private $m_context;
//other private parameters
function __construct($arguments_object){
if(!is_null($arguments_object)){
/*....*/
$this->m_context = $arguments_object->context;
/*....*/
}
}
/*....*/
function getContext(){
if(!empty($this->m_context)){
return $this->m_context;
}else{
//this would be our case, as the parameter was not initialized yet.
return $_SESSION['context'];
}
}
/*....*/
}
?>
Now, a colleague stumbled on an error and upon investigation, I found out that the $context was set to NULL. When I tried to reproduce the issue, the context was correctly initialized. And to be honest, for the few years the tool has been used, it is the first time this kind of issue was encountered.
I've also checked, at no point I am unsetting this particular session variable.
Am I making a false assumption there, in thinking that template.php would find the session variable when the session was started in entryupdate.php where it was included and used?

Why Session details are not accessible inside php namespace

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 to get session values in layout in Zend framework 1?

I want to access session values from layout.xml.
The code I did is
Layout.xml
<h2><?php $myprofile=new Zend_Session('user_session');
print $myprofile->username; ?> </h2>
Index Controller/index action
$userid = $this->_user->getUserId($username,$password);
$session = new Zend_Session_Namespace('user_session');
$session->username = $username;
$session->password = $password;
$session->uid = $userid;
$this->_redirect('home');
Home Controller/index action
$this->session = new Zend_Session_Namespace('user_session');
$this->view->uname = $this->session->username;
Home/index.phtml
<?php echo "The User Name is ".$this->uname?>
But it shows an error
Fatal error: Call to protected Zend_Session::__construct() from context 'Zend_View' in/var/www/shoppingcart/application/layouts/scripts/layout.phtml on line 19
I am able to get the session values in Home/index.html.
Expecting positive help.
Why using Zend_Session in the layout and Zend_Session_Namespace in the view ?
also maybe you should'n use sessions in the view/layout but pass it as param from the controller or the bootstrap file
I agree with Amine here, putting the Zend_Session in the view script is generally considered bad practice. I would indeed put it in the bootstrap as well, something like the following...
// Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initUserSession()
{
// If not already done, bootstrap the view
$this->bootstrap('view');
$view = $this->getResource('view');
// Initialise the session
$session = new Zend_Session_Namespace('user_session');
// See if the username has been set, and if not at
// least make the variable
if (isset($session->username)
$view->username = $session->username;
else
$view->username = null;
}
}
Then in the layout you can do this:
<?php if ($this->username !== null) : ?>
The User Name is <?php echo $this->username ?>
<?php else : ?>
No username has been set.
<?php endif; ?>

Setting Sessions inside PHP Class

I'm creating a user class to handle my logins. As I wish to set the sessions inside the class after the username and password are validated, do I have to use session_start() at the top of the class, inside the public function where the sessions are to be set, or where the instance is created? Perhaps it could go inside function _construct()?
This is how I would like to call the class:
<php
include('user_class.php');
$user = new user;
$user->login($username,$password);
?>
You can just add session_start(); at the top of the file you're including the class in.
So
<?php
session_start();
include('user_class.php');
$user = new user;
$user->login($username,$password);
?>
would work.
Next to your user-class create yourself a session-class as well.
The user-class then is just storing itself into the session class and does not need to take care about calling session_start or not, that's the job of the session-class.
<php
include('session_class.php');
include('user_class.php');
$session = new session;
if ($session->hasRegisteredUser()) {
$user = $session->getRegisteredUser();
} else {
$user = new user;
    $user->login($username, $password);
$session->setRegisteredUser($user);
}
Does this answer your question or do you need now to know how to do it with the session class?
Yes you can use sessions inside your classes because sessions are global variables in php.
Code Example(adding a new session variable):
<?php
class sessionControle{
...
...
public function addSession($index, $value){
$_SESSION[$index] = $value;
return $_SESSION[$index];
}
}
?>
in your main php file you can include the function $_SESSIONS are global
Code in your main file:
<?php
session_start();
include_once("myClass.php");
$Se = new sessionControle;
echo $Se->addSession('User', 'Crx');
//Double check here !!
echo $_SESSION['User'];
?>
Output: Crx

Is there an easy way to get AuthComponent user data from a view in CakePHP?

According to the cakebook section on the Auth component, I can implement simple authentication by using the following Users controller:
class UsersController extends AppController {
var $name = 'Users';
var $components = array('Auth'); // Not necessary if declared in your app controller
/**
* The AuthComponent provides the needed functionality
* for login, so you can leave this function blank.
*/
function login() {
}
function logout() {
$this->redirect($this->Auth->logout());
}
}
I would like to be able to something like the following into my view:
<?php
$username = $auth->user('username');
echo "Welcome " . $username;
?>
Is there a simple way to do this, or do I need to overwrite the login function and store the username to the session?
Update
Alexander's answer is exactly what I wanted. However, I will add the following in case someone else gets confused like I did.
It took me a while to understand that if you change the model that Auth uses (for example, you might have a 'persons' table instead of 'users'), then you need to use something like:
$persondata = $session->read('Auth.Person');
Actually this information is easily available from the session. You use the session helper to grab it. I believe the correct syntax is :
$userdata = $session->read('Auth.User');
$username = $session->read('Auth.User.username');
EDIT:
For CakePHP 2.X and on the syntax is:
$userdata = $this->session->read('Auth.User');
$username = $this->session->read('Auth.User.username');
Check out AuthComponent-Methods in the CakePHP manual....
You can access an user info after a user has logged in from the session via $this->Auth->User(). So if you want the username, just use this in the controller.
$this->set('username', $this->Auth->User('username'));
You can now use $username in the view.
Add a method in your AppController
function beforeFilter() {
$ath = $this->Auth->user();
$this->set('userDetails', $ath['User']);
}
And then you can access it from your views and/or layouts via $userDetails
To access Auth vars in views just do it:
echo $session->read('Auth.User.id');

Categories