3

I'm building a AngularJs app using joomla in background. The AngularJs app get loaded once and then calls the server always through ajax. Some calls happen simultaneously. If I'm using the default PHP session in those calls, php will process one call at a time. So, I wrote my own Session handler to allow more than one call at a time and I would like use that in place of the default php Session. Is there a easy way to do this in Joomla or I will have to write my own JSession class?

Edit

<?php
    session_start();
    sleep(10);
    echo "Hello";
?>

If you call this file twice in the same browser in the same time, the first call will return in 10 seconds, the second call will return in 20 seconds. The JSession class call the session_start(), so the joomla has the same behavior.

Edit

When we use the database option or extend the JSessionStorage with own session handler, joomla use session_set_save_handler() and it changes the default behavior of lock the session. So, I was wrong when I say that to call session_start involves lock the session.

  • 1. Try to use database as session storage, and let me know if it solves the problem. 2. Number of simultaneous connection should be minimized. – Shyam Apr 23 '14 at 14:19
  • 1
    A must read to understand behaviour of session and php - stackoverflow.com/questions/1430883/… – Shyam Apr 23 '14 at 14:24
  • Thanks @Shyam. I will read that and look at JSessionStorageDatabase class – csbenjamin Apr 23 '14 at 14:34
3

You don't have to write your own JSession class, but you'll need to write a class extending JSessionStorage to integrate with Joomla's session handling.

Most likely you'll run into trouble when trying to set the the session handler trough the Global Configuration. Either override JFormFieldSessionHandler with a version that includes your handler or just specify it manually in configuration.php. However the latter option means that you might run into trouble when you try to use the Global Configuration in the future.

  • I will not have trouble in specify the session manually in configuration.php. I create my own admin interface in angular with a global configuration page. Thanks for the answer. – csbenjamin Apr 23 '14 at 14:36
  • This doesn't work. The php session is started in JSession class in the method _start. This method calls session_start that looks the session. I think that I need override the JSession class. – csbenjamin Apr 23 '14 at 17:57
  • How does it lock the session when your Storage class does not support locking? I suppose at this point it would be easier if you could show some code. – Rouven Weßling Apr 23 '14 at 18:01
  • have a look on my edit above – csbenjamin Apr 23 '14 at 18:17
  • Have you tested this? If you don't enable locking in your driver than there will be no locking. Here's an example of a non-locking (non-Joomla) session storage: github.com/symfony/HttpFoundation/blob/master/Session/Storage/… – Rouven Weßling Apr 23 '14 at 18:49
4

Edit: It seams that the answer is not correct.

The default Joomla Session Handler (Database) does not lock the session while processing. So you don't have to worry about locking.

Anyway its possible that you override that session data of the first request with data of the second requests.

  • I'm going to look if the Database session handler meets my needs. Thanks – csbenjamin Apr 23 '14 at 14:49
  • even using the Session Handler (Database), the JSession still starts the php session with session_start() call. It looks the session. – csbenjamin Apr 23 '14 at 18:00
  • hmm, you are right, it seams that there is no way around this by standard php session handling. You can only close the session with session_write_close(); But I think you know this already... – Harald Leithner Apr 23 '14 at 18:24
  • Yes, The easiest way is copy the JSession and change it to call the session_start() and session_write_close() always it needs access $_SESSION. – csbenjamin Apr 23 '14 at 18:37
  • Your answer is correct. See the Rouven Weßling answer below. – csbenjamin Apr 24 '14 at 0:05
3

Welcome to the world of asynchronous calls. My advice is don't use sessions for the AJAX/RESTful calls at all. The most I would do is have some sort of authentication token so you know who is making the call.

  • Great advice. I'm going to think about that. Thanks – csbenjamin Apr 23 '14 at 23:44
2

Joomla allows you to create new Session Storage backends, which may solve your problem. Just extend JSessionStorage with your custom session class, and then you can set the handler in your configuration.php file directly. (Custom session storage backends don't show up in the global admin config.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.