php - Where to use session_write_close(), in ajax

751

My problem is that while an ajax request is going on, the user might click 'contact us' or some other link, but the session is locked, from the ajax, and the user has to wait for that, before being served the next link.

main url:
    makes ajax call,
    displays data

ajax target:
    looks at $_SESSION for various reasons,
    calls an api (this will take 5 seconds), 
    makes changes to $_SESSION,
    returns some values

During that 5 seconds, user clicks 'contact us'

onclick: ajax is aborted, by jquery.
    I dont care, anymore, what the ajax target did.  
    Any changes to SESSION dont matter much, either way.

user has to wait the 5 seconds, then the contact page loads.

Where is the proper place to use session_write_close()? Is it even possible, in this situation? (given that I have to write to SESSION, under normal circumstances)

968

Answer

Solution:

session_start()
looks at $_SESSION for various reasons
  - make copy of values if necessary
session_write_close()
calls an api (this will take 5 seconds), 
session_start()
  - some kind of optimistic lock checks if necessary
makes changes to $_SESSION,
returns some values

People are also looking for solutions to the problem: php - How to collect values of input fields from a form displayed with a loop

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.