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.