本文笔者是一个php初学者,下面是我一些笔记总结:

1.测试一个网站的负载量:
进入apache路径下的bin目录,比如我们要测试“同时处理50个请求并运行 1000 次 ab -c 50 -n 1000

http://localhost/phpinfo.php

2.判断某个站点能不能打开:

fsockopen(string hostname, int port, int , string , int );
$fp = fsockopen("php.wilson.gs", 80, &$errno, &$errstr, 10);
if(!$fp) {
echo "$errstr ($errno)<br/>\n";
} else {
fputs($fp,"get / http/1.0\nhost: php.wilson.gs\n\n");
while(!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
}

************
 下面的代码是正确的,返回了www.nic.edu.cn的首页
<?php $fp = fsockopen("www.nic.edu.cn", 80, &$errno, &$errstr, 10); if(!$fp) { echo "$errstr

($errno)<br>\n"; } else { while(!feof($fp)) { echo fgets($fp,128); } fclose($fp); } ?>

但是问题是我想要得是:http://www.nic.edu.cn/RS/ipstat/internalip/index.html 但我将上面的代码改成

:fsockopen("www.nic.edu.cnRS/ipstat/internalip/", 80, &$errno, &$errstr, 10); 就不行了,返回

错误:(0). 要想得到:http://www.nic.edu.cn/RS/ipstat/internalip/index.html 我该如何做?

************
建立server的socket连接跟HTTP没有关系 不能在主机IP后面加路径.

***********
我觉得获取远程网页 用fopen就可以了.

<?php
 $fp = fopen("http://218.30.84.17:89/dubanet","r");
 if(!$fp)
 {
  echo "N";
 }
 else
 {
  echo "Y";
 }
?>

3.php重定向

echo "<script language=javascript>";
echo "document.location.href='xxxx.php";
echo </script>"; 响应者 3:<?php
header("location: http://www.example.com/"); /* 重定向浏览器 */

/* 确保重定向后,后续代码不会被执行 */
exit;
?>

<?php
header("location;http://www.****.com");
?>

<?php
echo "<meta http-equals=refresh content='0;url=http://www.***.com'>";
?>

4.php 获取当前绝对路径
<?php
        echo $_SERVER["DOCUMENT_ROOT"];
?>
jsp获取当前绝对路径
String path = request.getRealPath("");

5.模拟HTTP 连线
<?php  
  $fp   =   fsockopen("www.blogjava.net",   80,   &$errno,   &$errstr,   10);  
  if(!$fp)   {  
                  echo   "$errstr   ($errno)<br>\n";  
  }   else   {  
                  fputs($fp,"GET   /   HTTP/1.0\nHost:   www.blogjava.net\n\n");  
                  while(!feof($fp))   {  
                                  echo   fgets($fp,128);  
                  }  
                  fclose($fp);  
  }  
  ?>  

5.print_r(HTTPrequest('get',   'www.w3.org',   '/')); 

没有二级域名 以/代替

6.PHP 模拟POST GET

<?php
 print_r(HTTPrequest

('post','lnc.ep.duba.net','/***.aspx','ksn=*****));  
 function HTTPrequest($method,$host,$usepath,$postdata = "")  
 {  
  if(is_array($postdata))  
  {  
   foreach($postdata as $key=>$val) 
   {  
    if(!is_integer($key))  
    $data  .=  "&$key=".urlencode($val);  
   }  
   $data = substr($data,1);  
  }
  else  
  {  
   $data = $postdata;  
  }  
  $fp = fsockopen($host, 80, &$errno, &$errstr, 30);  
  if(!$fp)
  {  
   print "$errstr ($errno)<br>\n";  
   return false;  
  }  
  else
  {  
   if(strtoupper($method) == "GET")
   {  
    $headers = "GET $usepath HTTP/1.1\r\n";  
   }
   else if(strtoupper($method) == "POST")
   {  
    $headers = "POST $usepath HTTP/1.1\r\n";  
   }  
    $headers .= "Host:   $host\n";  
    $headers .= "Connection:   close\r\n";  
   //$headers   .=   "Accept-Encoding:   gzip\r\n";  
   if(strtoupper($method) == "POST")  
   {  
    $strlength   =   strlen($data);  
    $headers   .=   "Content-Type:   application/x-www-form-

urlencoded\r\n";  
    $headers   .=   "Content-Length:   ".$strlength."\r\n";  
   }  
   $headers   .=   "\r\n";  
   $headers   .=   "$data\r\n";  
   fputs($fp,   $headers);  
   while(!feof($fp))  
   {  
    $output[]   =   fgets($fp,   1024);  
   }  
   fclose(   $fp);  
   return   $output;  
  }  
 }  
?>

7.逐行读取文件

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}
?>

8.取当前路径
echo   dirname($_SERVER['SCRIPT_FILENAME']);  
:D:/kingsoft/KAN5/CONSOLE/trunk/src/V5WebConsole
echo $_SERVER['PHP_SELF'];
:/test.php

posted on 2007-12-24 01:56 -274°C 阅读(805) 评论(3)  编辑  收藏 所属分类: PHP


FeedBack:
# re: PHP初学者总结
2008-01-02 15:28 | 小河
9.获取ip

function get_client_ip()
{
global $_SERVER;
if(isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif(isset($_SERVER["HTTP_CLIENT_IP"]))
{
$realip = $_SERVER["HTTP_CLIENT_IP"];
}
else
{
$realip = $_SERVER["REMOTE_ADDR"];
}
return $realip;
}  回复  更多评论
  
# re: PHP初学者总结
2008-01-03 15:31 | java-he
10.得到浏览器版本,得到操作系统语言:
<?php
echo $_SERVER['HTTP_USER_AGENT'];
echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];
// print_r($_SERVER);
?>   回复  更多评论
  
# re: PHP初学者总结
2008-02-26 15:15 | java-he
11.iconv()与mb_convert_encoding() 区别

iconv() 函数
Definition and Usage
定义和用法
iconv()函数的作用是:转换字符串的编码。

Description
string iconv ( string in_charset, string out_charset, string str )

Tips and Notes
注意点
注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符,//IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。

mb_convert_encoding() 函数
Definition and Usage
定义和用法
mb_convert_encoding()函数的作用是:转换字符串的编码。

Description
string mb_convert_encoding ( string str, string to-encoding [, mixed from-encoding])
注意:但是需要先enable mbstring 扩展库。

两者区别:mb_convert_encoding 中根据内容自动识别编码;mb_convert_encoding功能强大,但是执行效率比iconv差太多;

总结:一般情况下用 iconv,只有当遇到无法确定原编码是何种编码时才用 mb_convert_encoding 函数.

<code>
$str = iconv("UTF-8","GB2312//TRANSLIT",$str);
</code>
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 908980
  • 排名 - 40

最新评论