Rumah > pembangunan bahagian belakang > tutorial php > php优化解决方案

php优化解决方案

WBOY
Lepaskan: 2016-06-13 12:36:09
asal
996 orang telah melayarinya

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);
Salin selepas log masuk

大家好!
对php我不懂,请大家看下是否可以对该php做些优化呢?
我是写java的,发现可能在这个地方“$conn=mysql_connect($servername, $serverusername, $serverpassword);”可以进行优化,没有必要每次都生成一个$conn。
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan