php 解决方案,php优化解决方案
php優(yōu)化
本帖最后由 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);//選擇數(shù)據(jù)庫
if(!$db_select)
{
die("could?not?to?the?database");
}
$query="select?*?from?$usertable?where?$userfield?=?'$username'?and?$passwordfield='$password'";//構(gòu)建查詢語句
$result=mysql_query($query);//執(zhí)行查詢
$count=mysql_numrows($result);//取得條數(shù)
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);//選擇數(shù)據(jù)庫
if(!$db_select)
{
die("could?not?to?the?database");
}
$query="select?*?from?$usertable?where?$userfield?=?'$username'";//構(gòu)建查詢語句
$result=mysql_query($query);//執(zhí)行查詢
$count=mysql_numrows($result);//取得條數(shù)
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);
大家好!
對(duì)php我不懂,請(qǐng)大家看下是否可以對(duì)該php做些優(yōu)化呢?
我是寫java的,發(fā)現(xiàn)可能在這個(gè)地方“$conn=mysql_connect($servername,?$serverusername,?$serverpassword);”可以進(jìn)行優(yōu)化,沒有必要每次都生成一個(gè)$conn。
總結(jié)
以上是生活随笔為你收集整理的php 解决方案,php优化解决方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php无表单上传文件,php – 来自表
- 下一篇: 斗罗大陆魂师对决泰坦怎么加点?