Title: How to recognize user before and after logging in Symfony 2?
Tags: php;session;authentication;symfony
Question: I’m using Symfony2.1, a very simple login form based on documentation (http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form) and a custom authentication success handler. 

Anonymous user can do some action which are stored in database with the user's session ID. Now the user is logging into the system and I want to update saved actions with user's ID so that logged user can continue its work . Unfortunately in success handler I have already an updated session ID and I don’t know which records in action's table belongs to user (since they are stored with old session ID that I can’t access to [or can I?]).

What is the best practice to handle this kind of situations. Should actions be saved in database with token stored in cookie instead of session id or is there a build in mechanism and I’m trying reinvent the wheel or maybe I’m asking wrong question and therefore I can’t find answer.

Comment: Jonathan Wage recently wrote about a very similar use case on his blug: http://jwage.com/post/54943645180/tracking-new-member-origination-with-symfony2

Here is the accepted answer: The default mechanism of generating a new session id on an access level change is best practise. You could write your own authentication that does something with the new and old session ID. But unless you really know what you are doing security and authentication code is best left alone.

Best method would be as you suggest to save a token in the database and in a cookie and track your users with that. Don't forget to clean up the used tokens in the database and cookies if you no longer need them.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:08/02/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><session><authentication><symfony>', 'Title': 'How to recognize user before and after logging in Symfony 2?', 'ViewCount': 867, 'id': 18023571, 'user_AboutMe': "<ul>\n<li>PHP (mainly Symfony2)</li>\n<li>JavaScript (mainly jQuery, jQUI and dozens of jQ's plug-ins) </li>\n<li>XHTML, HTML5 </li>\n<li>CSS 1-3, LESS, SCSS</li>\n<li>SQL (mainly on MySQL)</li>\n<li>SVN, Git, Mercurial</li>\n</ul>\n", 'user_AccountId': 325873.0, 'user_DisplayName': 'Błażej Klisz', 'user_Reputation': 1730.0, 'user_Views': 122.0, 'user_id': 647319.0}}