Home Backend Development PHP Tutorial php优化解决方案

php优化解决方案

Jun 13, 2016 pm 12:36 PM
database mysql nbsp

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);
Copy after login

大家好!
对php我不懂,请大家看下是否可以对该php做些优化呢?
我是写java的,发现可能在这个地方“$conn=mysql_connect($servername, $serverusername, $serverpassword);”可以进行优化,没有必要每次都生成一个$conn。
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Jun 15, 2024 pm 09:49 PM

Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

See all articles