How to deal with php optimization

Source: Internet
Author: User
Php Optimization & lt ;? Phpdefine & nbsp; server & nbsp; info & nbsp; begin $ servername & nbsp; 192.168.1.182; & nbsp; $ serverusername & nbsp; whh; & nbsp; $ serverpassword & nbsp; & nbs php optimization
 // Define server info begin
$ Servername = "192.168.1.182 ";
$ Serverusername = "whh ";
$ Serverpassword = "whh ";
$ Database = "whh ";
$ Usertable = "user ";
$ Userfield = "user_name ";
$ Passwordfield = "password ";
// Define server info end

$ Fh = fopen ("php: // stdin", 'r ');
$ Stdout = fopen ('php: // stdout ', 'w ');
$ Fs = fopen ("auth-log.txt.". getmypid (), 'A ');

If (! $ Fh ){
Die ("Cannot open STDIN \ n ");
}

Do {
$ LenBytes = fgets ($ fh, 3 );
$ Len = unpack ('N', $ lenBytes );
$ Len = $ len [1];
If ($ len <1) continue;
$ Msg = fgets ($ fh, $ len + 1 );
$ Toks = explode (':', $ msg );
Fwrite ($ fs, $ msg. "\ n ");

$ Method = array_shift ($ toks );
Fwrite ($ fs, "$ method \ n ");
$ Result = false;

Switch ($ method ){
Case 'auth ':
List ($ username, $ server, $ password) = $ toks;
$ Password = trim ($ password );
Fwrite ($ fs, "checking user: $ username and password $ password \ n ");
// Check password begin
$ Conn = mysql_connect ($ servername, $ serverusername, $ serverpassword );
Mysql_query ("set names 'utf8 '");
If (! $ Conn ){
Die ("cocould not connect to the database ");
}
$ Db_select = mysql_select_db ($ database); // select a database
If (! $ Db_select)
{
Die ("cocould not to the database ");
}
$ Query = "select * from $ usertable where $ userfield = '$ username' and $ passwordfield =' $ password'"; // Construct a query statement
$ Result = mysql_query ($ query); // execute the query
$ Count = mysql_numrows ($ result); // Obtain the number of entries
Mysql_close ($ conn );
If ($ count> 0 ){
$ Result = true;
} Else {
$ Result = false;
}
// Check password end
Break;

Case 'isuser ':
List ($ username, $ server) = $ toks;
// Check password begin
$ Conn = mysql_connect ($ servername, $ serverusername, $ serverpassword );
Mysql_query ("set names 'utf8 '");
If (! $ Conn ){
Die ("cocould not connect to the database ");
}
$ Db_select = mysql_select_db ($ database); // select a database
If (! $ Db_select)
{
Die ("cocould not to the database ");
}
$ Query = "select * from $ usertable where $ userfield = '$ username'"; // Construct a query statement
$ Result = mysql_query ($ query); // execute the query
$ Count = mysql_numrows ($ result); // Obtain the number of entries
Mysql_close ($ conn );
If ($ count> 0 ){
$ Result = true;
} Else {
$ Result = false;
}
// Check password end
Break;

Default:
$ Result = false;
}
$ Message = @ pack ("nn", 2, $ result );
Fwrite ($ stdout, $ message );
$ Dump = @ unpack ("nn", $ message );
$ Dump = $ dump ["n"];
Fwrite ($ fs, $ dump. "\ n ");
Flush ();
} While (true );

Hello everyone!
I am not familiar with php. could you please check whether you can optimize the php?
I wrote java and found that "$ conn = mysql_connect ($ servername, $ serverusername, $ serverpassword);" can be optimized, there is no need to generate a $ conn each time.
------ Solution --------------------
There is nothing to optimize
In the imported data, only auth or isuser may exist, and both of them may not exist.
There is no need to establish a database connection without database access.

Program optimization is not to reduce code, but to improve performance
------ Solution --------------------
If the input parameters are consistent, $ conn and its judgment can be mentioned before do... while.

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.