PHP Optimization Solution

Source: Internet
Author: User
Tags unpack
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:
  • 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.