A brief analysis of how PHP implements the application-commonly used second-hair function _php instances

Source: Internet
Author: User
Tags learn php
Preface

Imagine, we usually send a friend circle, N pictures with text, swish a bit on the issue, without any kind of the muddy, experience feel good ~.

But we stopped to think about it in technical ways, is that possible? Some 2G network up to dozens of K speed, our pictures are just a few m, even if the compression has hundreds of K, how can you send a message in an instant?

Now think about it, isn't it weird?

In fact, a lot of social software (Weibo,) is used a second mechanism. He is not really first to send and then send finished to tell you to send the success, but directly tell you to send the success, and then secretly to upload your hair in the background, so when the internet is not good when we will often find a phenomenon, just start out of the circle of friends just started very normal, but after a few minutes, prompted us to send failed! This is very awkward ah, clearly at the beginning you did not say anything ah, to the most critical time you told me to die.

Do not be high on the technology scare, nothing but to do some small tricks just, really very basic, but also really very practical.

Before discussing the technique, first declare some initial conditions.

1. Some special modifications have been made to the database table structure: The Friend Circle Content table has a special field status, there are two cases of status value, the value can be 1 or 2,
A value of 1 is a friend circle that is not published. A value of 2 is a friend circle that has been published. (temporarily do not understand why do this classmate, you can continue to read, the following will explain)

2. This article of the second hair function refers to the use of the picture, because there are pictures, pictures upload too slow, so need to use the second mechanism, but no picture of plain text, there is not much need, because the text transmission is very low, in accordance with the normal process to send.

3. The Code of this article is based on the PHALAPI framework, the syntax is relatively simple, have an ORM experience of the students should be able to read

4. This article mainly explains the app's second hair function, the web side is not particularly required this feature, because the modern network is enough for our pc to send many pictures at once (10m/s,20m/s)

The general direction discusses the entire execution process:

The client calls the Publish API, the server publishes the content (publish.php), if there is a picture, then the client also calls an additional upload API (upload.php), in this upload API ( Upload.phpbefore the completion of the work, the client will directly tell you the success of the release (in fact, there is no upload completed, there is a process in the back to help you upload it), and then the client will send your text and pictures temporarily splicing to show you (at present only you can see, Your friend circle Other people are not visible), and then wait for the upload API (upload.php) Results/Of course it may also upload timeout (usually in a minute results), if successful, then successfully upload, failed to send failed, but in the waiting for the result of a minute, He will first let you think you have sent, unless the upload failed, will later remind you.

So let's analyze this mechanism on a technical level:

When we point to the top right of the Send button, there are two processes are started at the same time, one process is to help you upload text and tell you have been sent successfully (publish.php), another process is to secretly upload your sent pictures (upload.php ), the specific code is as follows:

publish.php

<?php//Normal access to data (text, pictures, location information, etc.) ... Code ... Code ...//judgment, if there is a picture is not published (status is 1), no picture is published immediately (status is 2)//If there is a picture by the return identifier to tell the client, Let him hurry to call the real upload logic upload.php, we just upload the most basic text, and then set a more status ~ $status = ($pic _num > 0)? 1:2; Splicing inbound data $where _data = Array ("status" = $status)//Data Warehousing DI ()->notorm-> friends->insert ($where _data);?>

Do you see the occult? We made a decision about the status field in the storage, which would be 1 (unpublished) and 2 (released), so what should we do when we read the data (list.php)?

So the presentation page is like this:

lists.php

<?php//code. Get text message Code: Get Picture Info//(It gets f_id in the current user's pic table (i.e. get a picture of a friend), the most critical place is where Condition $data= DI ()->notorm->pic->select (' f_id ') Where ("Status > 1 OR (status = 1 && u_id = {$u _id})")->->fetchall ();  Code: ?>

The where condition here is the most critical place for the second-click mechanism:

status greater than 1 (published) or equal to 1 (not published), (tips:status In the case of a picture default value is 1) but belong to the current user published content, can be read out, there is a wonderful phenomenon , that is, in any case, our own circle of friends, we can always read it, but others are not necessarily (because if there is a picture, you need to call another process to upload the image, and then in that process will change status to 2)

Then there is the last key point, is responsible for uploading the image of the process (upload.php), this is the real upload of images of the logic,

There are several pictures, so upload.php will be called several times

Change the field status of the picture row to 2 after each upload success

upload.php

<?php//code. Upload the image to the server directory//Get the long pass result ID, change the status If (upload succeeded) {//Change status back to 2 $status _data = Array ("status" = 2); DI ()->notorm->pic->select (' u_id ')->where (' u_id, $u _id)-->update ($status _data); }else{Code ...}

After the above several operations (first, publish.php, if there is a picture upload, then call upload.php, show the time is list.php).

Do not know that the doorway did not, and we usually write the release function is different, upload upload.php function is independent, the modified release publish.php will use the fastest speed of your text content into the database, And if there is a picture content, he will call the upload API upload.phpalone.

The key is to do some small tricks on the display, so that you can see what you're sending.

Well, the above is the whole content of this article, I hope that everyone to learn PHP help, but also please continue to focus on the script home.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.