PHP background operations and some issues that slow down server pressure

Source: Internet
Author: User
Tags configuration php openid server memory

Last mention of a polling system, did a redirect to solve, a licensing problem, the voting system was formally put into use yesterday, the test is not a problem, there is a small problem after the launch,

 A: PHP page parameter acceptance and PHP in the switch that first executed?

Answer: switch is executed first

  Example: on the index.php page

Header ("location:/api/wx.php?action=putprofile&accesstoken=". $accessToken. " &openid= ". $openId." &headimgurl= ". $headImgUrl." &nickname= ". $nickName);

      The implementation jumps to the wx.php page with several parameters Accesstoken OpenId headimgurl nickname (the word Linked in PHP is " . ");

 Below I receive on the wx.php page

        

$accessToken = $_get["Accesstoken"];
$openid = $_get["OpenID"];
$headimgurl = $_get["Headimgurl"];
$nickname = $_get["nickname"];

Receive parameters

Switch ($_get[' action ']) {

Case "Initprofile":
Initprofile ();
Break
Case "GetProfile";
GetProfile ();
Break
Case "Putprofile";
Putprofile ();
Break
}

  

function Putprofile ($openid, $nickname, $headimgurl) {
 
//echo "<script>alert (' $openid ');</script>";
//echo "<script>alert (' $headimgurl ');</script>";
//echo "<script>alert (' $nickname ');</script>";
//die ("INSERT into V2016_member (Mem_nickname, Mem_photo, Mem_openid, mem_regdate) VALUES ({$nickname}, {$headimgur L}, {$ $openid}, {$time} ");
//$user = Db_exec_query ("Select mem_id from v2016_member WHERE Mem_openid = ' {$openid} ' LIMIT 1");
$sql = "Select mem_id from v2016_member WHERE Mem_openid = ' {$openid} ' LIMIT 1";
$result = mysql_query ($sql);
if (! $row = Mysql_fetch_assoc ($result)) {
//echo "<script>alert (' gengxin ');</script>";
$time = time ();
$ins _id = db_exec_update ("INSERT into V2016_member (Mem_nickname, Mem_photo, Mem_openid, mem_regdate) VALUES (' {$ni Ckname} ', ' {$headimgurl} ', ' {$openid} ', ' {$time} '); ");
}
$user = Db_exec_query ("select * from v2016_member WHERE Mem_openid = ' {$openid} ' LIMIT 1");
$info = Array (
' mem_id ' = $user [0][' mem_id '],
' mem_nickname ' = Json_decode ($user [0][' Mem_nickname ']),
' Mem_photo ' = $user [0][' Mem_photo '],
' Mem_openid ' = $user [0][' Mem_openid '],
);
//Modify the default time of session in PHP
//$lifeTime = * 3600;
//session_set_cookie_params ($lifeTime);
session_start ();
$_session[' user '] = $info;
header ("location:/band.php");
}

As the above code shows, I began to think that in the execution of putprofile () This function when the above four parameters have been obtained, then is to plug in the database, I putprofile () alert a bit of that four parameters only to know that the function is not executed at the time of execution

$accessToken = $_get["Accesstoken"];
$openid = $_get["OpenID"];
$headimgurl = $_get["Headimgurl"];
$nickname = $_get["nickname"];

This code can put me out of the hole, there is no way backstage to the code to get the parameters into the Putprofile () in order to successfully insert the field into the database,

The reason for this is:switch is more than $_get first execution

Second: The default time to save session

The default save time for a session in PHP is 1440 seconds, meaning that the cache will be erased when the time is over.

You can manually modify both methods:

      1: Go to the Server configuration PHP environment of an INI file to find session.gc_maxlifetime change to the number you want, it is recommended not to change too long, so as not to cause server memory full, response too slow or direct paralysis

2: Code implementation:

$lifeTime = 46 * 3600;
        //session_set_cookie_params ($lifeTime);  

  Three: Server pressure problem:

    This problem is very wide many situations will cause the server pressure is too large, resulting in a slow response, for the Code of the program ape to do is to optimize the code, as to how to optimize the server pressure, too complex to say, can self-Baidu;

Next, let's talk about the problems I've encountered:

1: My database is the operation is PHP MYSQL code is implemented;

2: A lot of extra code

3: There are more SELECT, update and other operations, did not do function reuse;

4: Server running memory is also relatively small;

In the first few cases, there was no problem at the start of the code, and then as the user grew more and more, I suddenly found that the response time to open the polling system grew longer, and I ping a bit to see a speed

        

Find the response time of this IP minimum 7ms maximum 13ms average 8ms this too slow, for users, the experience is too bad, has been reflected open too slow

And then I checked my code.

found there are a lot of select, update and other operations, and the extra code is more,      to know that MySQL itself will occupy a large amount of virtual memory, coupled with the database operation is very memory-intensive, resulting in a large server pressure, the response time has become longer consequences;

So in the PHP page of Chinese Medicine to minimize the operation of the database, colleagues on the code to do a good job of optimization, otherwise it will cause the server pressure, slow response results.

This is it, this morning I ran 7 to the company to maintain the code, it is not easy AH!!

Fast 51, Wish Happy!!!!

      

        

    

    

      

PHP background operations and some issues that slow down server pressure

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.