C++ POST到PHP (2010-08-17)
今天做WAP版的网站,与C++那边配合
从C++ POST过来的的值用$_POST怎么也接收不到
无论打印 print_r($_POST) 和 print_r($_REQUEST); 都是0
把我郁闷坏了,但是GET值可以接收得到
就要快放弃的时候,找到一个方法
$key=file_get_contents("php://input");
print_r($key);
这样就可以接收到值了,是POST的原始数据
C++ POST给PHP数据用普通的$_POST方法接收不到任何值
没想到还有这种用方法接收POST!
这个问题搞了一天 不过又学会一个小技巧,还算值了!
PHPACL角色权限控制 (2010-05-23)
PHP ACL角色权限类,能实现无限角色权限的访问控制
网上关于RBAC方面资料比较少!
原理很简单,就是在执行control 前检查controller和action在acl表中的权限,
判断当前的访问者“可以”或“不可以”访问某个action的机制
满足条件就继续执行,否则跳转的制定页面
-
<?php
-
class Acl{
-
/**
-
* 默认权限检查的处理程序设置,可以是函数名或是数组(array(类名,方法)的形式)
-
*/
-
public $checker = array('spAclModel','check');
-
-
/**
-
* 默认提示无权限提示,可以是函数名或是数组(array(类名,方法)的形式)
-
*/
-
public $prompt = array('spAcl','def_prompt');
-
/**
-
* 构造函数,设置权限检查程序与提示程序
-
*/
-
public function __construct(){
-
$params = spExt("spAcl");
-
if( !empty($params["prompt"]) )$this->prompt = $params["prompt"];
-
if( !empty($params["checker"]) )$this->checker = $params["checker"];
-
}
-
-
/**
-
* 获取当前会话的用户标识
-
*/
-
public function get(){
-
return $_SESSION["SpAclSession"];
-
}
-
-
/**
-
* 强制控制的检查程序,适用于后台。无权限控制的页面均不能进入
-
*/
-
public function maxcheck(){
-
$acl_handle = $this->check();
-
if( 1 !== $acl_handle ){
-
$this->prompt();
-
return FALSE;
-
}
-
return TRUE;
-
}
-
-
/**
-
* 有限的权限控制,适用于前台。仅在权限表声明禁止的页面起作用,其他无声明页面均可进入
-
*/
-
public function mincheck(){
-
$acl_handle = $this->check();
-
if( 0 === $acl_handle ){
-
$this->prompt();
-
return FALSE;
-
}
-
return TRUE;
-
}
-
-
/**
-
* 使用程序调度器进行检查等处理
-
*/
-
private function check(){
-
GLOBAL $__controller, $__action;
-
$checker = $this->checker; $name = $this->get();
-
-
if( is_array($checker) ){
-
return spClass($checker[0])->{$checker[1]}($name, $__controller, $__action);
-
}else{
-
return call_user_func_array($checker, array($name, $__controller, $__action));
-
}
-
}
-
/**
-
* 无权限提示跳转
-
*/
-
public function prompt(){
-
$prompt = $this->prompt;
-
if( is_array($prompt) ){
-
return spClass($prompt[0])->{$prompt[1]}();
-
}else{
-
return call_user_func_array($prompt,array());
-
}
-
}
-
-
/**
-
* 默认的无权限提示跳转
-
*/
-
public function def_prompt(){
-
$url = spUrl(); // 跳转到首页,在强制权限的情况下,请将该页面设置成可以进入。
-
echo "<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>function sptips(){alert("Access Failed!");location.href="{$url}";}</script></head><body onload="sptips()"></body></html>";
-
exit;
-
}
-
-
/**
-
* 设置当前用户,内部使用SESSION记录
-
*
-
* @param acl_name 用户标识:可以是组名或用户名
-
*/
-
public function set($acl_name){
-
$_SESSION["SpAclSession"] = $acl_name;
-
}
-
-
/**
-
* 获取安全加密的密码输入框,开发者将需要在HTML中form标签上加入<code>onsubmit="aclcode();"</code>来触发加密
-
*
-
* @param id 在input框的id值。
-
* @param add 在input框内的其他内容,除id外,name,class等均可。
-
*/
-
public function pwinput($id, $add = null){
-
$raphash = substr(md5(mt_rand(10000,99999)),2,12);
-
$html = "<script type='text/javascript'>".spAcl::getmd5()."</script>";
-
$html .= "<script type='text/javascript'>function aclcode(){aclpwinput=document.getElementById('{$id}');document.getElementById('{$raphash}').value = hex_md5(aclpwinput.value);aclpwinput.value = '0000000000000000';}</script>";
-
$html .= "<input type='password' id='{$id}' {$add}>";
-
$html .= "<input type='hidden' id='{$raphash}' name='{$raphash}'>";
-
$_SESSION["SpAclInputHash"] = $raphash;
-
return $html;
-
}
-
/**
-
* 辅助pwinput的函数,让pwinput可在模板中使用。
-
* @param params 传入的参数
-
*/
-
public function smarty_pwinput($params){
-
return spAcl::pwinput($params["id"],$params["add"]);
-
}
-
/**
-
* 获取加密后的密码,该密码为MD5加密后的字符串
-
*
-
* 请注意返回值:
-
*
-
* -1 是无hash值,可以判断为远程提交等方式的攻击或是访问超时。需要重新访问登录页面。
-
* false 是没有输入密码,或是远程提交导致无法获取到正确的hash码。同样要求重新访问登录页面以再次输入密码提交。
-
* MD5编码后的密码
-
*/
-
public function pwvalue()
-
{
-
if(empty($_SESSION["SpAclInputHash"]))return -1;
-
$md5pw = spClass("spArgs")->get($_SESSION["SpAclInputHash"],false);
-
unset($_SESSION["SpAclInputHash"]);
-
return $md5pw;
-
}
-
-
}
-
-
class spAclModel extends spModel{
-
-
public $pk = 'aclid';
-
/**
-
* 表名
-
*/
-
public $table = 'acl';
-
-
/**
-
* 检查对应的权限
-
*
-
* 返回1是通过检查,0是不能通过检查(控制器及动作存在但用户标识没有记录)
-
* 返回-1是无该权限控制(即该控制器及动作不存在于权限表中)
-
*
-
* @param acl_name 用户标识:可以是组名或是用户名
-
* @param controller 控制器名称
-
* @param action 动作名称
-
*/
-
public function check($acl_name = SPANONYMOUS, $controller, $action){
-
$rows = array('controller' => $controller, 'action' => $action );
-
if( $acl = $this->findAll($rows) ){
-
foreach($acl as $v){
-
if($v["acl_name"] == SPANONYMOUS || $v["acl_name"] == $acl_name)return 1;
-
}
-
return 0;
-
}else{
-
return -1;
-
}
-
}
-
}
-
?>
封装PHP验证码类 (2010-03-05)
PHP验证码是很常见的应用
下面是一段封装好的类库
方便以后使用
总结一下核心函数:
imagecreate 创建图像;
imagecolorallocate 为图像上色
imagerectangle 创建矩形
imagestring 画字符串
imageline 生成干扰线
imagejpeg 生成JPEG格式
imagedestroy 催婚图像
-
<?php
-
session_start();
-
class checkImage {
-
-
private $config;
-
private $im;
-
private $str;
-
-
function __construct() {
-
$this->config['width'] = 50;
-
$this->config['height'] = 20;
-
$this->config['vcode'] = "vcode";
-
$this->config['type'] = "default";
-
$this->config['length'] = 4;
-
$this->config['interfere'] = 10;
-
$this->str['default'] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
$this->str['string'] = "abcdefghijklmnopqrstuvwxyz";
-
$this->str['int'] = "0123456789";
-
}
-
-
public function init($config=array()){
-
if (!empty($config) && is_array($config)){
-
foreach($config as $key=>$value){
-
$this->config[$key] = $value;
-
}
-
}
-
}
-
-
public function create(){
-
if (!function_exists("imagecreate")){
-
return false;
-
}
-
$this->createImage();
-
}
-
-
private function createImage(){
-
$this->im = imagecreate($this->config['width'],$this->config['height']);
-
imagecolorallocate($this->im, 255, 255, 255);
-
-
$bordercolor= imagecolorallocate($this->im,0,0,0);
-
imagerectangle($this->im,0,0,$this->config['width']-1,$this->config['height']-1,$bordercolor);
-
-
$this->createStr();
-
$vcode = $_SESSION[$this->config['vcode']];
-
$fontcolor = imagecolorallocate($this->im,46,46,46);
-
for($i=0;$i<$this->config['length'];$i++){
-
imagestring($this->im,5,$i*10+6,rand(2,5),$vcode[$i],$fontcolor);
-
}
-
-
$interfere = $this->config['interfere'];
-
$interfere = $interfere>30?"30":$interfere;
-
if (!empty($interfere) && $interfere>1){
-
for($i=1;$i<$interfere;$i++){
-
$linecolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
-
$x = rand(1,$this->config['width']);
-
$y = rand(1,$this->config['height']);
-
$x2 = rand($x-10,$x+10);
-
$y2 = rand($y-10,$y+10);
-
imageline($this->im,$x,$y,$x2,$y2,$linecolor);
-
}
-
}
-
header("Pragma:no-cachern");
-
header("Cache-Control:no-cachern");
-
header("Expires:0rn");
-
header("content-type:image/jpegrn");
-
imagejpeg($this->im);
-
imagedestroy($this->im);
-
exit;
-
}
-
-
private function createStr(){
-
if ($this->config['type']=="int"){
-
for($i=1;$i<=$this->config['length'];$i++){
-
$vcode .= rand(0,9);
-
}
-
$_SESSION[$this->config['vcode']] = $vcode;
-
return true;
-
}
-
$len = strlen($this->str[$this->config['type']]);
-
if (!$len){
-
$this->config['type'] = "default";
-
$this->create_str();
-
}
-
for($i=1;$i<=$this->config['length'];$i++){
-
$offset = rand(0,$len-1);
-
$vcode .= substr($this->str[$this->config['type']],$offset,1);
-
}
-
$_SESSION[$this->config['vcode']] = $vcode;
-
return true;
-
}
-
-
}
-
-
$v = new checkImage();
-
//$config['width'] = 50; //验证码宽
-
//$config['height'] = 20; //验证码高
-
//$config['vcode'] = "vcode"; //检查验证码时用的SESSION
-
//$config['type'] = "int"; //验证码展示的类型default:大写字母,string:小写字母,int:数字
-
//$config['length'] = 2; //验证码长度
-
//$config['interfere']= 10; //干扰线强度,范围为1-30,0或空为不起用干扰线
-
//$v->init($config); //配置
-
$v->create();
-
-
?>
-
-
<SCRIPT LANGUAGE="JavaScript">
-
<!--
-
function reloadcode(){
-
var d = new Date();
-
document.getElementById('safecode').src="./vcode.php?t="+d.toTimeString()
-
}
-
//-->
-
</SCRIPT>
-
-
<img src="vcode.php" id="safecode" onclick="reloadcode()">
最简单的PHP模板引擎 (2010-02-21)
模板类都是用是现成的,没有自己写过,杯具了!
今天自己写一下PHP模板引擎;
这是个最精简的PHP模板类;
模板标签采用纯天然原生态PHP语法
<?=$test?>使用这种原生形式比较快
因为PHP本身就是个很好的模板引擎
瞎写的玩的,反正核心原理就是替换变量
这是0.0001beta版,哈
过两天做个完整的
-
<?php
-
class Templates {
-
-
var $vars;
-
var $path;
-
-
public function __construct($path = null) {
-
$this->path = $path;
-
}
-
-
public function assign($name, $value) {
-
$this->vars[$name] = $value;
-
}
-
-
public function display($file) {
-
extract($this->vars);
-
ob_start();
-
include($this->path.$file.'.tpl.php');
-
$contents = ob_get_contents();
-
ob_end_clean();
-
return $contents;
-
}
-
-
}
-
-
/*test*/
-
$template_url = './tp/';
-
$name = 'Test Tempalte';
-
$tpl = new Templates($template_url);
-
-
$tpl->assign('title', $name);
-
$tpl->assign('user_list', fetch_user_list());
-
-
echo $tpl->display('test');
-
?>
大年初七,在家无聊,写了个mysql操作类 (2010-02-20)
在家无聊,写了个简单的DB类,方便以后CURD;
以前用公司现成的DB类,现在自己写个;
支持Mysqli,高手见笑,本人还是菜鸟 ;
-
<?php
-
-
class MysqlDB {
-
-
private $hostname;
-
private $username;
-
private $password;
-
private $db;
-
-
public function __construct() {
-
$num_args = func_num_args();
-
if($num_args > 0) {
-
$args = func_get_args();
-
$this->host = $args[0];
-
$this->user = $args[1];
-
$this->pass = $args[2];
-
}
-
$this->content();
-
}
-
-
private function mysqli_installed(){
-
if (function_exists ("mysqli_connect")){
-
return true;
-
} else {
-
return false;
-
}
-
}
-
-
private function content() {
-
try{
-
if($this->mysqli_installed()) {
-
if(!$this->db = new mysqli($this->host,$this->user,$this->pass)) {
-
$exceptionstring = "Error connection to database: <br />";
-
$exceptionstring .= mysqli_connect_errno() . ": " . mysqli_connect_error();
-
throw new exception ($exceptionstring);
-
}
-
}else{
-
if(!$this->db = new mysql_connect($this->host,$this->user,$this->pass)) {
-
$exceptionstring = "Error connection to database: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
public function select_db($db_name) {
-
try{
-
if($this->mysqli_installed() ) {
-
if(!$this->db->select_db($db_name)) {
-
$exceptionstring = "Error opening database: $db_name: <br />";
-
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
-
throw new exception ($exceptionstring);
-
}
-
}else{
-
if(!mysql_select_db($db_name,$this->db)) {
-
$exceptionstring = "Error opening database: $db_name: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
public function execute($query) {
-
try{
-
if($this->mysqli_installed()) {
-
if(!$this->db->query($query)) {
-
$exceptionstring = "Error performing query: $query: <br />";
-
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
-
throw new exception ($exceptionstring);
-
}
-
}else{
-
if(!mysql_query($query,$this->db)) {
-
$exceptionstring = "Error performing query: $query: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
}else{
-
echo "Query performed correctly: " . mysql_affected_rows () . " row(s) affected.<br />";
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
public function get_rows ($query){
-
try {
-
if ($this->mysqli_installed()){
-
if ($result = $this->db->query ($query)){
-
$returnarr = array ();
-
while ($adata = $result->fetch_array ()){
-
$returnarr = array_merge ($returnarr,$adata);
-
}
-
return $returnarr;
-
} else {
-
$exceptionstring = "Error performing query: $query: <br />";
-
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
-
throw new exception ($exceptionstring);
-
}
-
} else {
-
if (!$aquery = mysql_query ($query)){
-
$exceptionstring = "Error performing query: $query: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
} else {
-
$returnarr = array ();
-
while ($adata = mysql_fetch_array ($aquery)){
-
$returnarr = array_merge ($returnarr,$adata);
-
}
-
return $returnarr;
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
public function affected_rows() {
-
if($this->mysqli_installed()) {
-
return $this->db->affected_rows;
-
}else{
-
return mysql_affected_rows ($this->db);
-
}
-
}
-
-
public function insert_id() {
-
if($this->mysqli_installed()) {
-
return $this->db->insert_id;
-
}else{
-
return mysql_insert_id ($this->db);
-
}
-
}
-
-
public function escape($str){
-
return trim(mysql_escape_string($str));
-
}
-
-
public function __destruct() {
-
try {
-
if ($this->mysqli_installed()){
-
if (!$this->db->close()){
-
$exceptionstring = "Error closing connection: <br />";
-
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
-
throw new exception ($exceptionstring);
-
}
-
} else {
-
if (!mysql_close ($this->db)){
-
$exceptionstring = "Error closing connection: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
}
-
-
-
/*test*/
-
$mysql = new MysqlDB('localhost','root','');
-
-
$mysql->select_db('test');
-
-
$update = $mysql->execute ("UPDATE test SET usereMail='test@test.com' WHERE id='1'");
-
-
$insert = $mysql->execute ("INSERT INTO test SET usereMail = 'test@test.com'");
-
-
$insert_id = $mysql->insert_id($insert);
-
-
$test = $mysql->get_rows("SELECT * FROM `test` WHERE 1");
-
-
$affected_rows = $mysql->affected_rows($insert);
-
-
-
?>
简单的PHP框架 (2009-10-24)
简单的PHP MVC框架
\application\models\front.php
-
<?php
-
-
class FrontController {
-
-
protected $_controller, $_action, $_params, $_body;
-
-
static $_instance;
-
-
public static function getInstance() {
-
if( ! (self::$_instance instanceof self) ) {
-
self::$_instance = new self();
-
}
-
return self::$_instance;
-
}
-
-
private function __construct() {
-
$request = $_SERVER['REQUEST_URI'];
-
-
$splits = explode('/', trim($request,'/'));
-
$this->_controller = !empty($splits[0])?$splits[0]:'index';
-
$this->_action = !empty($splits[1])?$splits[1]:'index';
-
if(!empty($splits[2])) {
-
$keys = $values = array();
-
for($idx=2, $cnt = count($splits); $idx<$cnt; $idx++) {
-
if($idx % 2 == 0) {
-
//Is even, is key
-
$keys[] = $splits[$idx];
-
} else {
-
//Is odd, is value;
-
$values[] = $splits[$idx];
-
}
-
}
-
$this->_params = array_combine($keys, $values);
-
}
-
}
-
-
public function route() {
-
if(class_exists($this->getController())) {
-
$rc = new ReflectionClass($this->getController());
-
if($rc->implementsInterface('IController')) {
-
if($rc->hasMethod($this->getAction())) {
-
$controller = $rc->newInstance();
-
$method = $rc->getMethod($this->getAction());
-
$method->invoke($controller);
-
} else {
-
throw new Exception("Action");
-
}
-
} else {
-
throw new Exception("Interface");
-
}
-
} else {
-
throw new Exception("Controller");
-
}
-
}
-
-
public function getParams() {
-
return $this->_params;
-
}
-
-
public function getController() {
-
return $this->_controller;
-
}
-
-
public function getAction() {
-
return $this->_action;
-
}
-
-
public function getBody() {
-
return $this->_body;
-
}
-
-
public function setBody($body) {
-
$this->_body = $body;
-
}
-
-
}
\application\models\view.php
-
<?php
-
class View extends ArrayObject {
-
public function __construct() {
-
parent::__construct(array(), ArrayObject::ARRAY_AS_PROPS);
-
}
-
-
public function render($file) {
-
ob_start();
-
include(dirname(__FILE__) . '/' . $file);
-
return ob_get_clean();
-
}
-
}
application\models\icontroller.php
-
<?php
-
-
interface IController {}
Mysql导入CSV文件 (2009-09-15)
PHP导入CSV文件不能直接导入,只能通过sql语句导入
-
LOAD DATA LOCAL INFILE 'c:/1.csv' INTO TABLE `table` FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY 'rn';
去掉VIM的^M (2009-09-02)

每次有时候打开文件的时候会出现换行符,太烦了
于是把换行符替换掉 如下:
:% s/\r//g
:%s/^M//g
最简单的MVC模式 (2009-08-28)
这个实例虽然简单,但是充分体现了 MVC 模式对分离“表现层”和“业务逻辑层”带来的帮助。
首先呢,还是有一个调度器,负责根据 HTTP 请求决定要调用的控制器:
-
<?php
-
require ('controller/' . preg_replace('/[^a-z0-9_]+/i', '', $_GET['controller']));
-
?>
控制器:
-
<?php
-
// 从 Model 获取数据
-
require ('model/m1.php');
-
$m = new m1();
-
$data = $m->getData();
-
-
// 构造视图,显示输出
-
require ('view/v1.php');
-
$v = new v1();
-
$v->assign($data);
-
$v->display();
-
?>
模型:
-
<?php
-
class m1
-
{
-
function getData() {
-
return 'hello';
-
}
-
}
-
?>
视图:
-
<?php
-
class v1
-
{
-
var $data;
-
-
function assign($data) {
-
$this->data = $data;
-
}
-
-
function display() {
-
echo $this->data;
-
}
-
}
-
?>
PHP采集系统 (2009-08-13)
今天公司PHP牛人教了PHP采集系统的原理^_^,太牛了!
-
<?php
-
-
//获得网页内容
-
function getFileContents($url) {
-
$user_agent="User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Windows 2000; Windows XP)";
-
$urlparts = parse_url($url);
-
$path = $urlparts['path'];
-
$host = $urlparts['host'];
-
if (!empty($urlparts['query']))
-
$path .= "?".$urlparts['query'];
-
if (isset ($urlparts['port'])) {
-
$port = (int) $urlparts['port'];
-
} else
-
if ($urlparts['scheme'] == "http") {
-
$port = 80;
-
} else
-
if ($urlparts['scheme'] == "https") {
-
$port = 443;
-
}
-
-
if ($port == 80) {
-
$portq = "";
-
} else {
-
$portq = ":$port";
-
}
-
-
$all = "*/*";
-
-
$request = "GET $path HTTP/1.0rnHost: $host$portqrnAccept: $allrnAccept-Encoding: identityrnUser-Agent: $user_agentrnrn";
-
-
$fsocket_timeout = 60;
-
if (substr($url, 0, 5) == "https") {
-
$target = "ssl://".$host;
-
} else {
-
$target = $host;
-
}
-
-
-
$errno = 0;
-
$errstr = "";
-
$fp = @ fsockopen($target, $port, $errno, $errstr, $fsocket_timeout);
-
if (!$fp) {
-
$contents['state'] = "NOHOST";
-
print "Error: $errstr";
-
return $contents;
-
} else {
-
if (!fputs($fp, $request)) {
-
$contents['state'] = "Cannot send request";
-
return $contents;
-
}
-
$data = null;
-
socket_set_timeout($fp, $fsocket_timeout);
-
$status = socket_get_status($fp);
-
while (!feof($fp) && !$status['timed_out']) {
-
$data .= fgets($fp, 8192);
-
}
-
fclose($fp);
-
if ($status['timed_out'] == 1) {
-
$contents['state'] = "timeout";
-
} else{
-
if(strstr($data,"Location: ")&&strstr($data,"Cache-Control: private")){
-
$contents['state'] = "jump";
-
$contents['file'] = substr($data, strpos($data, "rnrn") + 4);
-
}
-
else{
-
$contents['state'] = "ok";
-
$contents['file'] = substr($data, strpos($data, "rnrn") + 4);
-
}
-
}
-
}
-
-
return $contents;
-
}
-
-
/*
-
检查url文件是否可以读取
-
check if file is available and in readable form
-
*/
-
function url_status($url) {
-
$user_agent="User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Windows 2000; Windows XP)";
-
$urlparts = parse_url($url);
-
$path = $urlparts['path'];
-
$host = $urlparts['host'];
-
if (!empty($urlparts['query']))
-
$path .= "?".$urlparts['query'];
-
-
if (isset ($urlparts['port'])) {
-
$port = (int) $urlparts['port'];
-
} else
-
if ($urlparts['scheme'] == "http") {
-
$port = 80;
-
} else
-
if ($urlparts['scheme'] == "https") {
-
$port = 443;
-
}
-
-
if ($port == 80) {
-
$portq = "";
-
} else {
-
$portq = ":$port";
-
}
-
-
$all = "*/*"; //just to prevent "comment effect" in get accept
-
$request = "HEAD $path HTTP/1.1rnHost: $host$portqrnAccept: $allrnAccept-Charset: iso-8859-1rnAccept-Encoding: identityrnUser-Agent: $user_agentrnrn";
-
-
if (substr($url, 0, 5) == "https") {
-
$target = "ssl://".$host;
-
} else {
-
$target = $host;
-
}
-
-
$fsocket_timeout = 60;
-
$errno = 0;
-
$errstr = "";
-
$fp = fsockopen($target, $port, $errno, $errstr, $fsocket_timeout);
-
-
$linkstate = "ok";
-
if (!$fp) {
-
$status['state'] = "NOHOST";
-
} else {
-
socket_set_timeout($fp, $fsocket_timeout);
-
fputs($fp, $request);
-
$answer = fgets($fp, 4096);
-
$regs = Array ();
-
if (ereg("HTTP/[0-9.]+ (([0-9])[0-9]{2})", $answer, $regs)) {
-
$httpcode = $regs[2];
-
$full_httpcode = $regs[1];
-
-
if ($httpcode <> 2 && $httpcode <> 3) {
-
$status['state'] = "Unreachable: http $full_httpcode";
-
$linkstate = "Unreachable";
-
}
-
}
-
-
if ($linkstate <> "Unreachable") {
-
while ($answer) {
-
$answer = fgets($fp, 4096);
-
-
if (ereg("Location: *([^nr ]+)", $answer, $regs) && $httpcode == 3 && $full_httpcode != 302) {
-
$status['path'] = $regs[1];
-
$status['state'] = "Relocation: http $full_httpcode";
-
fclose($fp);
-
return $status;
-
}
-
-
if (eregi("Last-Modified: *([a-z0-9,: ]+)", $answer, $regs)) {
-
$status['date'] = $regs[1];
-
}
-
-
if (eregi("Content-Type:", $answer)) {
-
$content = $answer;
-
$answer = '';
-
break;
-
}
-
}
-
$socket_status = socket_get_status($fp);
-
if (eregi("Content-Type: *([a-z/]*)", $content, $regs)) {
-
if ($regs[1] == 'text/html' || $regs[1] == 'text/' || $regs[1] == 'text/plain') {
-
$status['content'] = 'text';
-
$status['state'] = 'ok';
-
} else if ($regs[1] == 'application/pdf') {
-
$status['content'] = 'pdf';
-
$status['state'] = 'ok';
-
} else if ($regs[1] == 'application/msword') {
-
$status['content'] = 'doc';
-
$status['state'] = 'ok';
-
} else {
-
$status['state'] = "Not text or html";
-
}
-
-
} else
-
if ($socket_status['timed_out'] == 1) {
-
$status['state'] = "Timed out (no reply from server)";
-
-
} else
-
$status['state'] = "Not text or html";
-
-
}
-
}
-
fclose($fp);
-
return $status;
-
}
-
-
-
$host = 'http://www.admin5.com';
-
$list_exp = '<div class="itembox"';
-
$url_start = '<a href="';
-
$url_end = '" target=';
-
$detail_title_start = '<h1>';
-
$detail_title_end = '</h1>';
-
$detail_summary_start = '<div id="arctext">';
-
$detail_summary_end = '<div id="arctext">';
-
-
$max_page = 179;
-
for($page=$max_page;$page>0;$page--){
-
-
$url = "http://www.admin5.com/browse/26/list_".$page.".shtml";
-
-
$status = url_status($url);
-
-
if($status['content'] == 'text' && $status['state'] == 'ok'){
-
-
$files = getFileContents($url);
-
-
$contents = $files['file'];
-
-
$arr = explode($list_exp, $contents);
-
-
for($i=1;$i<count($arr);$i++){
-
$detail_url = "";
-
$detail_url = strstr($arr[$i], $url_start);
-
$detail_url = str_replace($url_start, "", $detail_url);
-
$pos = strpos($detail_url, $url_end);
-
$detail_url = substr($detail_url, 0, $pos);
-
$detail_url = $host.$detail_url;
-
-
$summary = getFileContents($detail_url);
-
-
print_r($summary);
-
exit;
-
}
-
-
}
-
-
}
-
-
?>



先进 了。。。