Home  >  Article  >  Backend Development  >  php优化解决方案

php优化解决方案

WBOY
WBOYOriginal
2016-06-13 11:37:01788browse

php优化

本帖最后由 xuzuning 于 2013-09-30 13:05:14 编辑
<?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);

大家好!
对php我不懂,请大家看下是否可以对该php做些优化呢?
我是写java的,发现可能在这个地方“$conn=mysql_connect($servername, $serverusername, $serverpassword);”可以进行优化,没有必要每次都生成一个$conn。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn