php asynchronous calls to improve the user experience


Advertisements


This is my very good friends, a technical writing, to published in my blog I can be for php php-loving fans of readers and friends who refer to the reference study.
Case: PHP language of a SNS communities in the system, users give their friends (the number of hundreds of friends) to send the message, the content of each message varies, sent after the prompt has been sent!
PHP wording used
sendmail.php

<?php
$count=count($emailarr);//$emailarr Mail address of the array to a friend 
for($i=0;$i<$count;$i++)
{
  sendmail(.....);// Send mail 
}
echo '' Sent ';
?>


Assuming that the time to send 100 messages. This operation there will be the result?
User Experience: User Wait -> send out dozens of messages -> system returns an error message out of this operation because of the need to send a large number of messages, leading to php execution time is too long, restless wait for the user. When the apache or nginx to wait more than allow the execution time, return time-out error. This time the user does not clear the success of this operation in the end, in the end sent a few emails.
We can see the code the user experience very poor and can not be successfully completed task.
That should be how does it work?
A concept mentioned here, asynchronous implementation of the user experience: users waiting for -> Send complete friends would ask, how to link the lack of letters?
OK, sent a letter link to the user submits the request, the letter task was transferred to a separate letter of the php processing procedure, when users see the "send complete" when in fact, not sending the letter, this time, letter program is hard work with the background, sending out a letter of
sendmail.php

<?php
$domain="www.***.com";
$url="/system_mail.php";
$par="email=".implode(',',$emailarr)."&........";
$header = "POST $url HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($par) . "\r\n\r\n";
$fp = @fsockopen ($domain, 80, $errno, $errstr, 30);
fputs ($fp, $header . $par);
fclose($fp);
echo '' Sent ';
?>
system_mail.php
<?php
ini_set("ignore_user_abort",true);
ignore_user_abort(true);// Code here need open PHP.ini options , Ensure the PHP implementation does not timeout , Not understand that reference my another article  " After you close the browser, PHP scripts will continue to run "
/*
 Get email addresses , Letters, here is a letter code 
*/
?>

Well, into the asynchronous mode, the user submits the information, you can immediately get the result "has been sent." Believed it would send a letter in the background, until the completion of this article sent from php enthusiasts http://www.phplover.cn/ , the original address: http://www.phplover.cn/post/phpyibugongzuotishengyonghutiyan. html
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of php asynchronous calls to improve the user experience

  • Answer: Let Ruby On Rails into the enterprise development

    Personally feel that using Ruby On Rails into the enterprise development, the following questions 1, open source products, there is a lack of stability. Patching is not the old approach, even though a patch soon. 2, a scripting language is a question ...

  • iPhone development: the first to embrace the future of software development

    - "CocoaChina iPhone development techniques will communicate" postscript Friend has said: "Apple's products are not only products, not just a culture, more is a kind of belief." Although I am not a fan of apple, but I see, smell, t

  • Collate and CSS code optimization principle 7

    This article The original English text of As a web designer (Front-end Engineer), you may recall that once the page size recommendations: a web page (including HTML, CSS, Javacript, Flash and picture) as far as possible not to exceed the size of 30KB ...

  • JSF Richfaces

    Ajax4jsf components already available, so developers can immediately use this time-saving tool has advanced features to create provides a faster and more reliable user experience of Web programs. Chapter 2. Start using Ajax4jsf Table of Contents Environme

  • javascript performance optimization

    Would like to sum up a long time about javascript performance optimization some things, usually also have the attention of the collection of information in this regard. Del.icio.us put in the collection of random things turned out, only surprised to find

  • Who in the end a matter for the quality control?

    In software, what is the quality. Following this article are compared in detail, I recommend you address. http://www.javaeye.com/topic/65626 If the article too long, I put the definition of software quality extracted summarize. 1. There is no bug doe ...

  • Rails to monitor the process of memory leaks skills

    Rails applications easier to encounter two types of performance problems: Rails implementation of a class are very slow, CPU consumption too high; The other is the process of memory leaks Rails. To resolve these two types of questions are necessary before

  • AJAX Packaging Tools

    Writing a javascript procedure ajax package put in a js file for your reference, please exhibitions. First statement of a few variables: Create XmlHttpRequest object: Implementation of Get / Post requests in the incoming Ways: Get the request method: ...

  • Ajax Portal

    The first test procedure: <html> <title>Full use XMLHttpRequest to load the document example </title> <head> <script type='text/javascript'> var req=null; var console=null; var READY_STATE_UNINITIALIZED=0; var READY_STATE_LOA

  • JS charts extjs implementation tools can be combined

    OpenFlashChart brief introduction by valensoft 2009-1-10 8:29:00 OpenFlashChart is an open source Flash and Javascript to the technical foundation for the free chart, using it to create some very useful analysis of the effect of the statements of the char

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries