一、设已知目录/data1/somedir, 写一个函数, 遍历取得该目录下包含子目录所在后缀为txt的文件.
function get_dir($dir){
if(!is_dir($dir) || !file_exists($dir)){
exit(‘不是目录或目录不存在’);
}
$dd=opendir($dir);
readdir($dd);
readdir($dd);
while($f=readdir($dd)){
$file=rtrim($dir,’/’).’/’.$f;
if(pathinfo($file,PATHINFO_EXTENSION)==’txt’){
$str.=$file.’,’;
}
if(is_dir($file)){
$str.=$file.’,’;
$str.=get_dir($file);
}
}
close($dd);
return $str;
}
$str=rtrim(get_dir(‘/data1/somedir’),’,’);
print_r(explode(‘,’,$str));
$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
getpathinfo($a, $b);
function getpathinfo( $a, $b ) {
$a2array = explode('/', $a);
$b2array = explode('/', $b);
$pathinfo = '';
for( $i = 1; $i <= count($b2array); $i++ ) {
$pathinfo.=$a2array[$i] == $b2array[$i] ? '../' : $b2array[$i].'/';
}
print_R($pathinfo);
}
答:采用curl模拟登陆操作
第一:分析登陆字段
第二:登陆后保留COOKIE
第三:读取COOKIE并跳转到相关页
第四:抓取数据
<?php
$bbs_url = 'http://test.com/ ';//论坛地址
$login_url = $bbs_url .' login.php ';//登录页地址
$post_fields = array();
//以下两项不需要修改
$post_fields['loginfield'] = 'username';
$post_fields['loginsubmit'] = 'true';
//用户名和密码,必须填写
$post_fields['username'] = 'tianxin';
$post_fields['password'] = '111111';
//安全提问
$post_fields['questionid'] = 0;
$post_fields['answer'] = '';
//@todo验证码
$post_fields['seccodeverify'] = '';
//获取表单FORMHASH
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);
if(!empty($matches)) {
$formhash = $matches[1];
} else {
die('Not found the forumhash.');
}
//POST数据,获取COOKIE,cookie文件放在网站的temp目录下
$cookie_file = tempnam('./temp','cookie');
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);
//取到了关键的cookie文件就可以带着cookie文件去模拟发帖.
$send_url = $bbs_url." thread.php?id=100";
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);
//这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性
preg_match('/<input\s*type="hidden"\s*name="formhash"\s*id="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);
if(!empty($matches)) {
$formhash = $matches[1];
} else {
die('Not found the forumhash.');
}
$post_data = array();
//帖子标题
$post_data['subject'] = 'test2';
//帖子内容
$post_data['message'] = 'test2';
$post_data['topicsubmit'] = "yes";
$post_data['extra'] = '';
//帖子标签
$post_data['tags'] = 'test';
//帖子的hash码,这个非常关键!假如缺少这个hash码,会警告你来路的页面不正确
$post_data['formhash']=$formhash;
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_REFERER, $send_url); //伪装REFERER
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$contents = curl_exec($ch);
curl_close($ch);
//清理cookie文件
unlink($cookie_file);
?>
1.用文件存储用户 信息,用户注册输入用户 名,密码和电子邮件;2.注册后需要通过发送电子邮件来验证用户的信息真实和有效;3.密码需要加密.保证安全性4.用户可以登录,退出和注销,并将用户的这些操作行为记录到日志中5.如果用户没有退出 下次登录自动显示用户名和最后一次登录的信息
Class manage{
public function login(){
}
public function logout(){
}
public function zhuxiao(){
}
private function log(){
}
private function info(){
}
private function mail(){
}
private function safe(){
}
private function my_cookie(){
}
}
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function tool(){
this.mail=function(mail){
var pre="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
var res=mail.exec(pre);
if(!res){
return false;
}else{
return ture;
}
}
this.date=function(date){
var pre="/^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/";
var res=date.exec(pre);
if(!res){
return false;
}else{
return ture;
}
}
.
.
.
}
</script>
</body>
</html>