php优化
<?php<br />//define server info begin<br />$servername = "192.168.1.182"; <br />$serverusername = "whh"; <br />$serverpassword = "whh"; <br />$database="whh";<br />$usertable="user";<br />$userfield="user_name";<br />$passwordfield="password";<br />//define server info end<br /><br />$fh = fopen("php://stdin", 'r');<br />$stdout = fopen('php://stdout', 'w');<br />$fs = fopen("auth-log.txt." . getmypid(), 'a');<br /><br />if(!$fh){<br /> die("Cannot open STDIN\n");<br />}<br /><br />do{<br /> $lenBytes = fgets($fh, 3);<br /> $len = unpack('n', $lenBytes);<br /> $len = $len[1];<br /> if($len<1) continue;<br /> $msg = fgets($fh, $len+1);<br /> $toks=explode(':',$msg);<br /> fwrite($fs, $msg . "\n");<br /><br /> $method = array_shift($toks);<br /> fwrite($fs, "$method\n");<br /> $result = false;<br /><br /> switch($method){<br /> case 'auth':<br /> list($username, $server, $password) = $toks;<br /> $password = trim($password);<br /> fwrite($fs, "checking user: $username and password $password\n");<br /> //check password begin<br /> $conn=mysql_connect($servername, $serverusername, $serverpassword); <br /> mysql_query("set names 'utf8'");<br /> if(!$conn){ <br /> die("could not connect to the database");<br /> }<br /> $db_select=mysql_select_db($database);//选择数据库 <br /> if(!$db_select)<br /> {<br /> die("could not to the database");<br /> }<br /> $query="select * from $usertable where $userfield = '$username' and $passwordfield='$password'";//构建查询语句<br /> $result=mysql_query($query);//执行查询<br /> $count=mysql_numrows($result);//取得条数<br /> mysql_close($conn);<br /> if($count > 0){<br /> $result = true;<br /> }else{<br /> $result = false;<br /> }<br /> //check password end<br /> break;<br /><br /> case 'isuser':<br /> list($username, $server) = $toks;<br /> //check password begin<br /> $conn=mysql_connect($servername, $serverusername, $serverpassword); <br /> mysql_query("set names 'utf8'");<br /> if(!$conn){ <br /> die("could not connect to the database");<br /> }<br /> $db_select=mysql_select_db($database);//选择数据库 <br /> if(!$db_select)<br /> {<br /> die("could not to the database");<br /> }<br /> $query="select * from $usertable where $userfield = '$username'";//构建查询语句<br /> $result=mysql_query($query);//执行查询<br /> $count=mysql_numrows($result);//取得条数<br /> mysql_close($conn);<br /> if($count > 0){<br /> $result = true;<br /> }else{<br /> $result = false;<br /> }<br /> //check password end<br /> break;<br /><br /> default:<br /> $result = false;<br /> }<br /> $message = @pack("nn", 2, $result);<br /> fwrite($stdout, $message);<br /> $dump = @unpack("nn", $message);<br /> $dump = $dump["n"];<br /> fwrite($fs, $dump . "\n");<br /> flush();<br />} while(true);