I recently created a new symfony application on the existing website. Then, in this new application, I want to read the session of old applications(something like login user id). Unfortunately, in each application, the session are completely separate(I mean the symfony session, something like $this->getUser()->getAttribute("userSession")).
I guess the symfony session is implemented using $_SESSION like:
$_SESSION = array("symfonyapp1" => array(....), "symfonyapp2" => array(....));
So I wrote $_SESSION["test"] = "testStr" in the old application and wrote var_dump($_SESSION["test"]);. The screen simply prints "null", so my guess is wrong.
Then I think maybe I can read the configuration of a certain application and then get the user of that application. So I wrote the following code in my new application:
require_once($_SERVER['DOCUMENT_ROOT'].'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
$context = sfContext::createInstance($configuration);
var_dump($context->getUser()->getAttribute("userId"));
Unfortunately again, it prints "null".
I completely have no idea now. Any advice is greatly appreciated
Check session identifiers of your apps. Php's default session id is PHPSESSID and as far as I remember default session identifier for Symfony 1.4 apps is just symfony
You can change Symfony's session identifier by modyfing apps/YOUR_APP_NAME/config/factories.yml file, by setting:
all:
storage:
param:
session_name: PHPSESSID
By doing that your Symfony app will share the same session id as your old app and you will be able to read $_SESSION attributes in Symfony app
Related
I have recently updated form 2.2.x to 3.0.0 with following the update procedure from codeigniter's website.
I have having real issues with the new session library - heres the issue.
We have a login section which dependant on the subdomain and user/pass credentials will give you certain privileges from ADMIN / RESELLER / CLIENT / USER
In order to determine the correct privileges for the user we have built a customer LIBRARY (location:application/library) which we have called Session_management, this library DOES NOT extend the core SESSION driver/library and never has and has no extension to another class, this library is also auto-loaded, prior to CI 3.0.0 everything was working fine.
First this the Session_management does is __construct()
$this->CI =& get_instance();
$this->CI->load->model('users');
$this->CI->load->model('clients');
$this->CI->load->model('sessions');
$this->CI->load->driver('session');
$this->CI->load->library('password_hash');
$this->CI->load->helper('url');
$this->users = $this->CI->users;
$this->clients = $this->CI->clients;
$this->sessions = $this->CI->sessions;
$this->session = $this->CI->session;
$this->password_hash = $this->CI->password_hash;
Prior to CI 3.0.0 I has no issues in using the
$this->CI->load->library('session');
but for some unknown reason (to me) I HAVE to load it through the driver
$this->CI->load->diver('session');
if someone could explain why I am having to do it this way that would be great.
When a user submits their user/pass credentials a CONTROLLER session/signin is requested which runs firstly form validation, providing everything is successfully, the Session_management login method is called.
$success = $this->session_management->login
($this->input->post('email'), $this->input->post('password'));
In the LOGIN method in the Session_management class a bunch of sessions are set using
$this->session->set_userdata();
$this->session->set_userdata('user_id', 0);
$this->session->set_userdata('user_name', '');
$this->session->set_userdata('client_id', 0);
$this->session->set_userdata('client_administrator', 0);
$this->session->set_userdata('reseller_administrator', 0);
However when I var_dump() the session for all its session data it has NOTHING and I can't why this is, no session data is there except my protected fields form the config, which I have double checked and triple checked and are working fine, my sess_save_path is storing the session files correctly, and the rest of the sess configs are also correct.
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = '/Users/******/Sites/********/tmp';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
This is a development website on my local OS X iMac, as I say prior to CI 3.0.0 everything was working fine.
Just to add before I get reply's saying "you need to use"
$this->load->library('session');
I have "HAD" to load it as a driver, I don't have a choice, I have read the documentation and have seen how to initialise the session library.
If I do attempt to load it as a library
$this->load->library('session');
This is what I get
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Session::$session
Filename: libraries/Session_management.php
Line Number: 38
For your ref: line 38 is:
$this->session = $this->CI->session;
This is after trying to load the session library
$this->CI->load->library('session');
Also to add to this message re: Database sessions / File session.
Database sessions were my first option, a have revamp the database columns and indexes to suit CI 3.0.0 as the documentation mentions and session were and are storing in the database table when I change my config to use the database, however reading the performance differences between File sessions against Database sessions under high load, File sessions will out perform database session and since the website / platform I am creating will be under high load, database sessions aren't the way forward.
As a note: The website runs under a subdomain.domain.*** structure where subdomain is registered as a company name upon a company registration i.e
mycompany.mywebsiteurl.com
anothercompany.mywebsiteurl.com
As previously mention - prior to my update to CI 3.0.0 it was working fine.
Might I also add: I have checked my log files, I am running tail -f for live log updates - and it don't see any log issues.
Any help, information or anything that could possible put me in the right direction would be appreciated.
I have also posted on the CI forum.
A fresh copy of CI 3.0.0 with necessary files and configs transferred across didn't solve my problem/issue.
What seems to be the whole cause of the problem was the fact I had a controller call Sesssion.php which is named the same as the Driver file.
When CI (Codeigniter) calls
$this->load->library('session')
there are a few checks done before CI knows that you want to actually load the CI_Session class ... class_exists('Session') returns TRUE and it stops there in order to avoid a fatal error.
Hope this helps others too.
Apparently every key I save to a Symfony2 session goes under a subkey:
$session->set('foo', 'bar');
Will result in:
array('_sf2_attributes' => array('foo' => 'bar'));
This is a problem because I'm building an application that interacts with legacy code. I want to use Symfony's session handler but I want old parts of the app to be able to read from $_SESSION. In other words, I want
$session->set('foo', 'bar');
and
$_SESSION['foo'] = 'bar';
to have the same effect.
I didn't see any configuration option to achieve this. I use a specific session handler which I set to
session:
handler_id: my_app.session_handler
So it won't be a problem that Symfony uses a different session name. The only solution I can think of is creating a new session handler class around my customer session handler (my_app.session_handler) that checks if the application tries to access _sf2_attributes but I think it's extremely ugly and I'm not even sure if would work.
Thanks!
This is not only the handler that you have to implement on your own, but it also should be your SessionStorage.
By default, Symfony2 uses NativeSessionStorage. You should be looking for the method loadSession(). As you can see, it initializes a session with the $key received from getStorageKey(), which is initialized with _sf2_attributes here - in the AttributeBag
Let me know if you have problems injecting custom session storage.
I'm using Cakephp's build in test framework to test my controllers. I have a logout function that expires a variety of cookies that are created as the user uses the site. I am trying to read said cookies to determine if a test should pass or not, i.e. to test if the cookie is correctly expired. I have made sure that the cookie component is correctly instantiated, but I cannot read any value back from the cookie that should be there. This is the code that composes the test I am running:
public function testLogout() {
// setup the cookie component
$collection = new ComponentCollection();
$this->Cookie = new CookieComponent($collection);
$result = $this->testAction('/users/logout');
$cookie_name = Configure::read('tech_cookie_name');
$cookie_data = $this->Cookie->read($cookie_name);
debug($cookie_name);
// cookie data is returning as NULL but I'm expecting some type of value.
debug($cookie_data);
debug($result);
exit;
}
I realize that exit is killing the test early, but I'm using it to see if anything is send back from the cookie. I'm not sure why I cannot read any data from a cookie that I know is there. Does anyone know why that might be, or have a solution for how to properly read from cookies in a unit test.
You cann't read from routes.php Configure::read() in certain cases and it is not a good practice. it will work in localhost but not in live. try to configure your session properly.
by calling your session from AppController and also from your current Controller (UserController) then you should be able to see it in your testing actions.
public $components = array('Session', 'RequestHandler', 'Cookie', ...);
if you write your session like this:
$this->Session->write('Test.tech_cookie_name', 'tech_cookie_name');
then you should be able to read it like this:
$this->Session->read('Test.tech_cookie_name');
New to Laravel and having some problems with Sessions. Specifically, reading session data from a PHP file outside of Laravel.
For example, let's say I set the session variable like so: Session::put('isAuthorized', 'yes') - I can retrieve this just fine in the Laravel context with Session::get('isAuthorized') but the following PHP will not retrieve this session key -
<?php
session_start();
echo $_SESSION['isAuthorized'];
?>
returns
Notice: Undefined index: isAuthorized in C:\xampp\htdocs\session.php on line 3
I have tried setting the Laravel session driver to both the default cookie and file modes, same result.
You could also write a session adapter, so the $_SESSION variable will be an instance of it:
<?php
class SessionAdapter implements \ArrayAccess {
public function offsetExists($offset) {
return Session::has($offset);
}
public function offsetGet($offset) {
return Session::get($offset);
}
public function offsetSet($offset, $value) {
return Session::put($offset, $value);
}
public function offsetUnset($offset) {
return Session::forget($offset);
}
}
And then somewhere in your code:
<?php
$_SESSION = new SessionAdapter();
// or
$GLOBALS['_SESSION'] = new SessionAdapter();
This way native PHP session and Laravel session will be "the same".
Laravel uses storage drivers for its sessions, namely cookie, file, database, memory, memcached and redis (and APC in Laravel 4).
The web is a stateless environment. This means that each request to your application is considered unrelated to any previous request. However, sessions allow you to store arbitrary data for each visitor to your application. The session data for each visitor is stored on your web server, while a cookie containing a session ID is stored on the visitor's machine. This cookie allows your application to "remember" the session for that user and retrieve their session data on subsequent requests to your application.
http://laravel.com/docs/session/config
The default storage driver is Cookie, so try this:
print_r($_COOKIE);
Please note that this answer is specific to Laravel 3
Laravel doesn't use PHP sessions, so forget session_start(), $_SESSION, etc.
If you're running with file session driver, the session data is stored in a file in storage/sessions. You can obtain the name of the file by reading the Laravel session ID from the cookie. So the hacky way to solve your problem would be to write some code that obtains the session ID from the cookie and then looks for the file with that name in the storage/sessions folder, read that file in, json_decode() it and you can read the whole thing.
If you're running with cookie session driver, all of the session data is stored in the cookie, but it is encrypted, so you'd have to have a copy of the key (which should be in application/config/application.php) and then figure out what encryption method Laravel is using so you can decrypt it. Then you can read all the session variables.
To achieve what you're hoping to achieve - that is, figure out if the current person is authorized, it might be better to build an API into your app and secure it so that it can only be accessed by localhost. Not a great solution from a performance standpoint, but potentially more elegant because you're not hacking around with the internals of Laravel session management.
Session handling in Laravel is indeed different from native PHP session. To use native PHP session, set the value as below:
<?php
session_start();
$_SESSION['isAuthorized'] = 'yes';
echo $_SESSION['isAuthorized']; // output yes
?>
I'm working on a Zend project where I need to include another project, which isn't using ZF. This other project is stored in the public directory in the folder of the zend project.
For this other project I need the logindata from the zend project (zend auth is used for this). There are 2 ways to accomplish this i think.
Just get the stored login sessionvariable. But where/what variable?
Or try to get the data with zend methodes in the other project. But how? Without changing the structure of this other project.
Or maybe (probably) there's an other/better solution?!
Hope it's clear.
Tnx
$authNamespace = new Zend_Session_Namespace('Zend_Auth');
$authNamespace->user = "myusername";
Just include pathToZendProjectDirectory\Zend\Session.php from your 'nonzend` project
The login data is in SESSION variable. But we can't access the session data directly outside the project, because the SESSION contain some Zend objects. When we start the session it race an error __PHP_Incomplete_Class has no unserializer.
To over come this add the code in starting of the page.
function __autoload($class) { // required files load automatically
require_once "pathToZendProjectDirectory/PathToZendLibrary/$class.php";
}