php函数serialize()与unserialize()

serialize()和unserialize()在php手册上的解释是:

serialize — Generates a storable representation of a value

serialize — 产生一个可存储的值的表示

unserialize — Creates a PHP value from a stored representation

unserialize — 从已存储的表示中创建 PHP 的值

<?php 
//声明一个类 
class dog { 

    var $name; 
    var $age; 
    var $owner; 

    function dog( $in_name = “unnamed”, $in_age = “0”, $in_owner = “unknown”) { 
        $this -> name = $in_name; 
        $this -> age = $in_age; 
        $this -> owner = $in_owner; 
    } 

    function getage() { 
        return ( $this -> age * 365); 
    } 
    
    function getowner() { 
        return ( $this -> owner); 
    } 
    
    function getname() { 
        return ( $this -> name); 
    } 

//实例化这个类 
$ourfirstdog = new dog( “Rover”, 12, “Lisa and Graham”); 
//用serialize函数将这个实例转化为一个序列化的字符串 
$dogdisc = serialize( $ourfirstdog); 
print $dogdisc; //$ourfirstdog 已经序列化为字符串 O:3:”dog”:3:{s:4:”name”;s:5:”Rover”;s:3:”age”;i:12;s:5:”owner”;s:15:”Lisa and Graham”;} 

print ‘<BR>’; 

/* 
—————————————————————————————– 
    在这里你可以将字符串 $dogdisc 存储到任何地方如 session,cookie,数据库,php文件 
—————————————————————————————– 
*/ 

//我们在此注销这个类 
unset( $ourfirstdog); 

/*    还原操作   */ 

/* 
—————————————————————————————– 
    在这里将字符串 $dogdisc 从你存储的地方读出来如 session,cookie,数据库,php文件 
—————————————————————————————– 
*/ 


//我们在这里用 unserialize() 还原已经序列化的对象 
$pet = unserialize( $dogdisc); //此时的 $pet 已经是前面的 $ourfirstdog 对象了 
//获得年龄和名字属性 
$old = $pet -> getage(); 
$name = $pet -> getname(); 
//这个类此时无需实例化可以继续使用,而且属性和值都是保持在序列化之前的状态 
print “Our first dog is called $name and is $old days old<br>”; 
print ‘<BR>’; 
?>
本文地址:php函数serialize()与unserialize()

Nginx防蜘蛛爬虫处理

假定一个场景:某个网站它可能不希望被网络爬虫抓取,例如测试环境不希望被抓取,以免对用户造成误导,那么需要在该网站中申明,本站不希望被抓取。有如下方法:

方法一:修改nginx.conf,禁止网络爬虫的ua,返回403。

server { 

listen 80; 

server_name 127.0.0.1; 

#添加如下内容即可防止爬虫

if ($http_user_agent ~* “qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot”) 

return 403; 

方法2:网站更目录下增加Robots.txt,放在站点根目录下。

http://tool.chinaz.com/robots/站点可以针对现在的搜索引擎按照想要的规则生成robots.txt文件。

知识扩展:

       robots.txt是搜索引擎中访问网站的时候要查看的第一个文件。robots.txt文件告诉蜘蛛程序在服务器上什么文件是可以被查看的。

当一个搜索蜘蛛访问一个站点时,它会首先检查该站点根目录下是否存在robots.txt,如果存在,搜索机器人就会按照该文件中的内容来确定访问的范围;如果该文件不存在,所有的搜索蜘蛛将能够访问网站上所有没有被口令保护的页面。百度官方建议,仅当您的网站包含不希望被搜索引擎收录的内容时,才需要使用robots.txt文件。如果您希望搜索引擎收录网站上所有内容,请勿建立robots.txt文件。

Robots协议是国际互联网界通行的道德规范,基于以下原则建立:

1、搜索技术应服务于人类,同时尊重信息提供者的意愿,并维护其隐私权;

2、网站有义务保护其使用者的个人信息和隐私不被侵犯。

当然,如果搜索引擎不遵守约定的Robots协议,那么通过在网站下增加robots.txt也是不起作用的。(在正式环境中,可以适当允许搜索引擎抓取收录)