Run imap_close not working

I have a page with clients and with ajax im information is loaded on whether they send us an email or not.

The code is as follows:

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; $username = 'email'; $password = 'password'; $this->session->data['imap_inbox'] = $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); foreach($customers as $customer){ $emails = imap_search($inbox, 'FROM ' . $email); // Processing info } 

But on one page there are about 20-30 clients, so it sometimes takes about 10-20 seconds to complete this process, and I could not optimize the process.

But when the client tries to reload the page, it is still waiting for imap_search finish, so it may take 20 seconds to reload before reloading the page.

I tried beforeunload ajax using the beforeunload function and closing imap , but this does not work.

My code is:

Ajax:

  $(window).bind('beforeunload',function(){ imap_email.abort(); // the ajax is succesfully aborted(as showed in console), yet the page still takes considerable time to reload $.ajax({ type: 'GET', url: 'getimapmails&kill=1', async:false }); // ajax call to the same function to call imap_close }); 

PHP:

 if($this->request->get['kill'] == '1'){ imap_close($this->session->data['imap_inbox']); unset($this->session->data['imap_inbox']); $kill == 1; exit; } 

But even if ajax interrupted and imap_close is called while holding the imap_open variable, it still takes 10-20 seconds to reload the page, so I assume that imap was not closed.

How to close imap so that the page can reload immediately?

+8
javascript jquery ajax php imap
source share
2 answers

I would recommend killing him by creating a file that causes a break:

 $hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; $username = 'email'; $password = 'password'; $this->session->data['imap_inbox'] = $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); foreach($customers as $customer){ clearstatcache(); //Can't use the cached result. if(file_exists('/tmp/kill_imap.'.$this->session->id)) break; //making the assumption that /tmp and session->id are set, but the idea is a temporary folder and a unique identifier to that session. $emails = imap_search($inbox, 'FROM ' . $email); // Processing info } if(file_exists('/tmp/kill_imap.'.$this->session->id)) unlink('/tmp/kill_imap.'.$this->session->id); 

Then in your ajax output just call the php script that just creates this file. and it will break your loop and delete the file.

+2
source share

If I understood correctly, the lengthy code lies inside the foreach () loop.

Now, even if you make a second request to kill the IMAP session, this foreach () loop will continue until it completes, or PHP will kill it if (and when) the execution time exceeds the max_execution_time setting.

In any case, you need something in your foreach () loop that will check every round if an interrupt condition is met, to switch to interrupt the current request and allow the client to create a new one.

I suggest you take a look at the PHP function connection_aborted () , which you could use to find out how the client will terminate the current request, and you can usually read more about connection processing in order to better understand how connections and requests are handled in PHP.

+1
source share

All Articles