PHP optimization
This post was last edited by xuzuning on 2013-09-30 13:05:14
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 ("Could does connect to the database");
}
$db _select=mysql_select_db ($database);//Select Database
if (! $DB _select)
{
Die ("Could not to the database");
}
$query = "SELECT * from $usertable where $userfield = ' $username ' and $passwordfield = ' $password ';//Build Query statement
$result =mysql_query ($query);//Execute Query
$count =mysql_numrows ($result);//Get the number
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 ("Could does connect to the database");
}
$db _select=mysql_select_db ($database);//Select Database
if (! $DB _select)
{
Die ("Could not to the database");
}
$query = "SELECT * from $usertable where $userfield = ' $username '";//Build Query statement
$result =mysql_query ($query);//Execute Query
$count =mysql_numrows ($result);//Get the number
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!
To PHP I do not understand, please see if you can do some optimization of the PHP?
I wrote Java and found it possible in this place "$conn =mysql_connect ($servername, $serverusername, $serverpassword);" Can be optimized, there is no need to generate a $conn every time.
Share to: