15.2 Checking Whether an IMAP Stream Is Still Active


You want to see whether an IMAP stream is still active. This can be either a method for checking for new messages, or a "keep- alive " technique.

Technique

Use the imap_ping() function, which returns true if the stream is active and false if the stream is inactive.

 <?php $mh = imap_open("{localhost:143} INBOX", $user, $pass); if (imap_ping($mh)) {     echo "Stream is active"; } else {     echo "Stream is in-active"; } ?> 

Comments

The imap_ping() function is simply a wrapper for the mail_ping() function call (in C). Its purpose is to make sure that the stream is still active, and in doing so, it also prevents a timeout on that stream. It is helpful to think of the imap_ping() function as similar to the Check E-mail button on Netscape Communicator. It returns true if there are new messages and therefore the stream is active.

Note

If you are running your programs at the command line or as a background utility (which is really only possible when PHP is installed as a CGI rather than as a module), you can use this in context of a while loop with the sleep() function:

 <?php while (1) {     if (imap_ping($mh)) {         print time() . " NEW MESSAGES\n";     }     sleep(60); #sleep for 60 seconds } ?> 



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net