Bridge session data between legacy Application and Symfony2 - php

Sorry if this question has been asked before I could not find it. I couldn't find the answer I was looking for.
I have a legacy application that I'm rebuilding with Symfony2, unfortunatly I need to run parallel for a while until I can finish rebuilding the entire system. I'm to the part where I need to have symfony be able to access the legacy session data to be able to function.
I found this from the Symfony site: http://symfony.com/doc/current/cookbook/session/php_bridge.html
http://symfony.com/doc/current/components/http_foundation/session_php_bridge.html
But I dont understand were the configuration needs to happen and where Im supposed to call the session-start and also how to access the session data from my new application.
Would I be placing the example given in symfony in every controller I create?
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
// legacy application configures session
ini_set('session.save_handler', 'files');
ini_set('session.save_path', '/tmp');
session_start();
// Get Symfony to interface with this existing session
$session = new Session(new PhpBridgeSessionStorage());
// symfony will now interface with the existing PHP session
$session->start();
Seems like there should be a way to centralize it.
UPDATE
The manual gives an example but It doesn't say if its supposed to go in a controller, service, entity, or config somewhere.
I tried using the example code in a controller and when I do a print_r on the $session I don't see any legacy session data.
**
Thanks in advance.

What you are looking at there is sample code for the Component that symfony uses. Symfonys session is initialized from parameters set in to app/config.yml. The component instructions are only if you are using the component outside of symfony. The one you want is the first one. However using:
#app/config.yml
framework:
session:
storage_id: session.storage.php_bridge
handler_id: session.handler.native_file
Is meant to be used if you are including symfony within your other application where session_start() is being called before symfony is instantiated. I havent tested this but it may still work having them run in parallel but you may end up having to do some customization to the session handler.
Maybe look into using a pdo session handler: http://symfony.com/doc/current/cookbook/configuration/pdo_session_storage.html

Related

Wrapping Zend 1 into Symfony 2. How to share session?

In my job, we are working on migrating our Zend 1 app to Symfony 2. I tried to figure how to do this harmlessly. I found the following SF2 Bundle : https://github.com/mainlycode/Zf1WrapperBundle. I installed it, followed the configuration instructions, copied my Zend app in /vendor/zend and everything is working pretty fine. When I browse my old login route, SF2 can't understand it and ask the response to Zend, and my login form appears. I can log in, and it works, the authed part of the site displays correctly.
Now I want to start revamp a feature where the user needs to be authed. So I created a new SF2 bundle, called BackendBundle. It tried this in my controller :
$securityContext = $this->container->get('security.context');
if( $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') ){
echo 'AUTHED';
}
But I get the following error :
The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL.
I totally understand why. But I really dont know how I can "share" the authentification between Zend and SF2.
Any suggestions ?
My first guess was to share the session (configure both ZF and Sf2 to use a similar SessionHandler), but Sf2 stores session information in a very specific way that ZF would probably not support easily.
But I guess you could try to create an Sf2 RequestListener which would read your ZF session, find an auth token in it, then create an Sf2 auth token to finally inject it to the SecurityContext.
What d'you think about that?

APC as a session handler using Symfony components

I'm using Symfony components in my web application. I need to store session in APC but unfortunatelly I can't find the way to do it.
As I see here Symfony does not support APC as a session handler. Is that true?
I have found an old example of using APC as a session handler in Symfony. But there all configuration is done in factories.yml file which I don't have since I'm only using standalone Symfony components.
Can anyone give me an example of using APC as a session handler using only Symfony components?
Since I don't get any answer here for a long time I will answer the question myself. For now there is no built in suport for APC as a session handler in Symphony framework. There is no particlar reason for it, likely Symphony developers just did not get to it.
The solution is simple, just code APCSessionHandler.php file yourself (I was not doing it because we decided not use this in project), APCSessionHandler will be very similar to MemcachedSessionHandler.php file.
How to store PHP sessions in APC Cache? suggests it is feasible but a bad idea for a busy site. The accepted answer lists a few useful ideas

How to enable PHP native sessions in codeigniter?

I'm trying to integrate Facebook's API in a CodeIgniter project, however it seems to be not working because sessions are disabled somehow in CodeIgniter. (Trying to integrate outside CodeIgniter works, but inside a codeigniter project it doesn't work).
Is there any way to do this? All I want is for native sessions to not be cleared or destroyed by CodeIgniter. If the only way is to hack the core to remove a line or two, then I'm willing to do it. I think there's someplace where its doing $_SESSION = null or something like that. Where's this place, so I can remove that line?
If you follow this blog on a fresh CI install, it works without any problem:
http://www.dannyherran.com/2011/02/facebook-php-sdk-and-codeigniter-for-basic-user-authentication/
From there, you can continue to build you application, or find where it goes wrong.
in CI session are not disabled.
It must be done in your manual coding.
Please refer your coding again.
It is also possible that some jQuery - JS is conflicted in project, which can destroy session.
There is a library to extend CI sessions (which are cookies) to use native server sessions.
Have a look at this native session library.

Using Zend Framework Inside Drupal

I have an issue with a project that I am working on, basically the whole application runs as a Drupal 6 install, and I am bootstrapping a Zend Framework Application to pull out the data I need (which I will then make available in various views via a module I will create).
I have the majority of this working correctly, but the issue I am having is with sessions, the actual error I am getting is as follows :-
PHP Fatal error: Uncaught exception 'Zend_Session_Exception' with
message 'session has already been started by session.auto-start or
session_start()'
Which I think is happening because Drupal is setting up the session and then ZF is then also trying to setup its own sessions and they are clashing. Is there a way I can override/extend the default Zend Session handling to let it use the Drupal Session API?
Thanks
Rich
Are you going to actually be storing information in the Zend session? If not, you could do away with starting a Zend session completely, or at least conditionally skip starting the Zend Session if the session has already been started.
If you do, you will not be able to start or use any Zend_Session* related functions (Zend_Session, Zend_Session_Namespace) if a session was previously started with session_start(). There does not appear to be any way around this.
Instead, from your Zend Application, you can just use the $_SESSION superglobal anywhere you need to use session data.
This won't be of much help, but a useful reference is Starting a session in Zend.

Kohana sessions and files not under the framework

I'm working on a site that is using the Kohana framework. So far I've been fine jumping in and taking over the project. Today I'm implementing a few 3rd-party features that need to use a session variable. I can set the session variable and use it fine on pages that are using controllers to serve them up, but if I make ajax calls or in this instance calls to the 3rd party page, the session data is not available to me.
I cannot figure this one out. I've tried a few things from the Kohana forums, but nothing seems to work.
Visual example:
html
html/application
controller files
ajax.php
third-party.php
If I call up ajax.php I cannot access the session data that I am setting inside "kohana" pages.
my system/config/session.php file is using 'cookie' as the driver. Expiration and Regenerate are set to 0.
Any ideas?
That's because you're using the cookie session driver. You'd probably be able to get access if you were using the 'native' driver, which uses the internal php session.
As asked above, is there some reason you're not building the ajax.php within Kohana? That would save you a lot of trouble.
On the other hand, it's possible to decrypt the session from within your external ajax.php (which I wouldn't recommend, but have had to do in order to get the kohana session data passed to a WordPress installation running on a subdomain).
From what you say about the session configuration I think you are using Kohana 2.x.
And Kohana 2 clears the $_GLOBALS. So you might have problems accessing native $_POST, $_COOKIE, $_SESSION, $_GET, $_GLOBALS and $_REQUEST arrays.
I recommend using the native driver for the session library, but I highly recommend even more migrating to Kohana 3.x.

Categories