php优化解决方案
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php优化解决方案,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2439字,纯文字阅读大概需要4分钟。
内容图文
![php优化解决方案](/upload/InfoBanner/zyjiaocheng/224/d906a07b29034e97909e73e6be0e7b44.jpg)
本帖最后由 xuzuning 于 2013-09-30 13:05:14 编辑
<?php
//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);//选择数据库
if(!$db_select)
{
die("could not to the database");
}
$query="select * from $usertable where $userfield = '$username' and $passwordfield='$password'";//构建查询语句
$result=mysql_query($query);//执行查询
$count=mysql_numrows($result);//取得条数
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);//选择数据库
if(!$db_select)
{
die("could not to the database");
}
$query="select * from $usertable where $userfield = '$username'";//构建查询语句
$result=mysql_query($query);//执行查询
$count=mysql_numrows($result);//取得条数
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);
大家好!
对php我不懂,请大家看下是否可以对该php做些优化呢?
我是写java的,发现可能在这个地方“$conn=mysql_connect($servername, $serverusername, $serverpassword);”可以进行优化,没有必要每次都生成一个$conn。 分享到:
内容总结
以上是互联网集市为您收集整理的php优化解决方案全部内容,希望文章能够帮你解决php优化解决方案所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。