本文笔者是一个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 阅读(810)
评论(3) 编辑 收藏 所属分类:
PHP