有时,很想知道每天爬行了网站些什么页面,爬了几次,但本人网站都是静态页面,而且空间商的记录要每天最设保留日志第二天才会保留。
以下代码是通过伪静态的逆向,实现的有关静态页面的蜘蛛爬行记录的代码。
供大家学习参考。
php文件:bot.php
<?php
$useragent = addslashes(strtolower()($_SERVER['HTTP_USER_AGENT']));
if (strpos($useragent, 'googlebot')!== false){$bot = 'Google';}
elseif (strpos($useragent,'mediapartners-google') !== false){$bot = 'Google Adsense';}
elseif (strpos($useragent,'baiduspider') !== false){$bot = 'Baidu';}
elseif (strpos($useragent,'sogou spider') !== false){$bot = 'Sogou';}
elseif (strpos($useragent,'sogou web') !== false){$bot = 'Sogou web';}
elseif (strpos($useragent,'sosospider') !== false){$bot = 'SOSO';}
elseif (strpos($useragent,'yahoo') !== false){$bot = 'Yahoo';}
elseif (strpos($useragent,'msn') !== false){$bot = 'MSN';}
elseif (strpos($useragent,'msnbot') !== false){$bot = 'msnbot';}
elseif (strpos($useragent,'sohu') !== false){$bot = 'Sohu';}
elseif (strpos($useragent,'yodaoBot') !== false){$bot = 'Yodao';}
elseif (strpos($useragent,'twiceler') !== false){$bot = 'Twiceler';}
elseif (strpos($useragent,'ia_archiver') !== false){$bot = 'Alexa_';}
elseif (strpos($useragent,'iaarchiver') !== false){$bot = 'Alexa';}
elseif (strpos($useragent,'slurp') !== false){$bot = '雅虎';}
elseif (strpos($useragent,'bot') !== false){$bot = '其它蜘蛛';}
if(isset()($bot)){
$fp = @fopen('bot.txt','a');
fwrite($fp,date('Y-m-d H:i:s')."\t".$_SERVER["REMOTE_ADDR"]."\t".$bot."\t".'http://'.$_SERVER['SERVER_NAME'].$_SERVER["HTTP_X_REWRITE_URL"]."\r\n");
fclose($fp);
}
$file=".".$_SERVER[HTTP_X_REWRITE_URL];
$f_head=substr($file,-5);
if($f_head==".html")
{
if(file_exists($file))
{
echo file_get_contents($file);
}else
{
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
echo "该页面无法找到";
}
}
else
{
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
echo "该页面无法找到";
}
?>
伪静态文件内容:
[ISAPI_Rewrite]
# 3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
# Protect httpd.ini and httpd.parse.errors files
# from accessing through HTTP
RewriteRule /index.html /index.php
RewriteRule ^/article/(.*) /bot.php [L]
RewriteRule ^/list/(.*) /bot.php [L]
有关php中循环跳出的一个问题,现整理在这里,供路过的朋友参考哦。
<?php
//php当前循环为1,循环由里到外依次递增,break默认为1,例如跳出第2层循环
for ($i=0;$i<3;$i++){
foreach (array(1,2,3) as $val){
foreach (array(1,2,3) as $val){
echo "1层循环<br/>";
break 2; //跳出第2层循环
}
echo "2层循环<br/>";
}
echo "3层循环<br/>";
}
//结果:
//1层循环
//3层循环
//1层循环
//3层循环
//1层循环
//3层循环
?>
php写的登录时用户名与密码验证器,有需要的朋友可以参考下。
1、登录时对用户输入的用户名、密码进行验证
<?php
/**
* Validator for Login.
*/
final class LoginValidator {
private function __construct() {
}
/**
* Validate the given username and password.
* @param $username and $password to be validated
* @return array array of {@link Error} s
*/
public static function validate($username, $password) {
$errors = array();
$username = trim($username);
if (!$username) {
$errors[] = new Error('username', '用户名不能为空。');
} elseif (strlen($username)<3) {
$errors[] = new Error('username', '用户名长度不能小于3个字符。');
} elseif (strlen($username)>30) {
$errors[] = new Error('username', '用户名长度不能超过30个字符。');
} elseif (!preg_match('/^[A-Za-z]+$/',substr($username, 0, 1))) {
$errors[] = new Error('username', '用户名必须以字母开头。');
} elseif (!preg_match('/^[A-Za-z0-9_]+$/', $username)) {
$errors[] = new Error('username', '用户名只能是字母、数字以及下划线( _ )的组合。');
} elseif (!trim($password)) {
$errors[] = new Error('password', '密码不能为空。');
} else {
// check whether use exists or not
$dao = new UserDao();
$user = $dao->findByName($username);
if ($user) {
if (!($user->getPassword() == sha1($user->getSalt() . $password))) {
$errors[] = new Error('password', '用户名或密码错误。');
}
} else {
$errors[] = new Error('username', '用户名不存在。');
}
}
return $errors;
}
}
?>
Error是自己写的一个类:
<?php
/**
* Validation error.
*/
final class Error {
private $source;
private $message;
/**
* Create new error.
* @param mixed $source source of the error
* @param string $message error message
*/
function __construct($source, $message) {
$this->source = $source;
$this->message = $message;
}
/**
* Get source of the error.
* @return mixed source of the error
*/
public function getSource() {
return $this->source;
}
/**
* Get error message.
* @return string error message
*/
public function getMessage() {
return $this->message;
}
}
?>
2、调用验证器进行验证
<?php
$username = null;
$password = null;
$msg = "";
if (isset()($_POST['username']) && isset($_POST['password'])) {
$username = addslashes(trim(stripslashes()($_POST ['username'])));
$password = addslashes()(trim(stripslashes($_POST ['password'])));
// validate
$errors = LoginValidator::validate($username, $password);
if (empty($errors)) {
// save the latest ip or login time into database, then processing page forwarding
$dao = new UserDao();
$user = $dao->findByName($username);
$last_login_ip = Utils::getIpAddress();
$user->setLastLoginIp($last_login_ip);
$now = new DateTime();
$user->setLastLoginTime($now);
$dao->save($user);
UserLogin::setUserInfo($user);
Flash::addFlash('登录成功!');
Utils::redirect('welcome');
}
foreach ($errors as $e) {
$msg .= $e->getMessage()."<br>";
}
?>