php最佳化解決方案

來源:互聯網
上載者:User
php最佳化
本帖最後由 xuzuning 於 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 not connect to the database");
}
$db_select=mysql_select_db($database);//選擇資料庫
if(!$db_select)
{
die("could not to the database");
}
$query="select * from $usertable where $userfield = '$username' and $passwordfield='$password'";//構建查詢語句
$result=mysql_query($query);//執行查詢
$count=mysql_numrows($result);//取得條數
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 not connect to the database");
}
$db_select=mysql_select_db($database);//選擇資料庫
if(!$db_select)
{
die("could not to the database");
}
$query="select * from $usertable where $userfield = '$username'";//構建查詢語句
$result=mysql_query($query);//執行查詢
$count=mysql_numrows($result);//取得條數
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);

大家好!
對php我不懂,請大家看下是否可以對該php做些最佳化呢?
我是寫java的,發現可能在這個地方“$conn=mysql_connect($servername, $serverusername, $serverpassword);”可以進行最佳化,沒有必要每次都產生一個$conn。

分享到:
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.