本文教你小偷程序之幾個(gè)基本函數(shù)(第二天)
發(fā)表時(shí)間:2024-02-11 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]第一天的教程連接地址http://www.vipcn.com/infoview/Article_3751.html今天教第二天.有個(gè)朋友說(shuō)的好,其實(shí)教功夫也是變相的教你怎么殺人,可是不說(shuō)出來(lái).所以這個(gè)教程從下一篇開始就改個(gè)名字叫<遠(yuǎn)程操作對(duì)方數(shù)據(jù)程序>有點(diǎn)土比小偷好聽第二天.教幾個(gè)函數(shù)...
第一天的教程連接地址
http://www.vipcn.com/infoview/Article_3751.html今天教第二天.
有個(gè)朋友說(shuō)的好,其實(shí)教功夫也是變相的教你怎么殺人,可是不說(shuō)出來(lái).
所以這個(gè)教程從下一篇開始就改個(gè)名字
叫<遠(yuǎn)程操作對(duì)方數(shù)據(jù)程序>有點(diǎn)土比小偷好聽
第二天.
教幾個(gè)函數(shù)給大家.
1 讀取判斷函數(shù)
2 更新cache函數(shù)
3 寫入文件函數(shù)
4 切割字符函數(shù)
5 讀取字符函數(shù)
---------------------------------------------------
在我們想寫之前我們先要做好準(zhǔn)備,構(gòu)思一下怎么去寫.
制作前構(gòu)思1
我們打開華軍首頁(yè)
http://www.onlinedown.net/
經(jīng)過(guò)我們統(tǒng)計(jì),是不是發(fā)現(xiàn)它的連接有個(gè)很相同的規(guī)則?
1 根目錄的index.htm文件
2 soft文件夾的 1.htm .......30000.htm
3 sort文件夾的 1_1.htm 200_1.htm
4 a-z文件夾 1.htm ....200.htm
到此我們可以想好一個(gè)打開函數(shù),不是根目錄 就是文件夾/名字
只有2中可能.
制作前構(gòu)思2
為了讓速度更快,我們最好把內(nèi)容讀過(guò)來(lái)存儲(chǔ)起來(lái).
1 減少了對(duì)對(duì)方站點(diǎn)的請(qǐng)求.
2 提供了速度.
這里我們判斷這個(gè)文件寫入的時(shí)間為準(zhǔn) 我們自己設(shè)置一個(gè)時(shí)間
當(dāng)寫入時(shí)間 和現(xiàn)在的時(shí)間比一下,如果在我們的設(shè)置時(shí)間內(nèi)的話.就是可以.
如果不在比如
文件寫入時(shí)間 2004年5月18好 06:00 我們現(xiàn)在時(shí)間是2004年5月19號(hào) 18:00
我們?cè)O(shè)置時(shí)間是1個(gè)小時(shí)
當(dāng)再次請(qǐng)求這個(gè)文件的時(shí)候 他發(fā)現(xiàn)已經(jīng)過(guò)期了.就會(huì)重新向?qū)Ψ秸?qǐng)求一次并且存入.
制作前構(gòu)思3
為了以后頻繁的操作簡(jiǎn)單話,把固定的操作寫進(jìn)一個(gè)函數(shù).
切割字符.
一般切割就是 從第一個(gè)位置 切割到第二個(gè)位置 然后取中間部分
比如:
<html>
<head>
<title>演示站點(diǎn)</title>
</head>
<body>
</body>
</html>
從<title>開始切割到</title>
那切割出來(lái)的 就是 "演示站點(diǎn)"4個(gè)字
如果說(shuō),可以找到 幾個(gè) <title>怎么辦?程序會(huì)從第一處開始切割
到這里構(gòu)思差不多..
程序要干凈明了才行,不要這一個(gè)文件不知道什么,那一個(gè)文件不知道哪來(lái).
所以,如果你以后有做大站的機(jī)會(huì)的話,文件夾,文件一定要寫的清楚,分的清楚.
既然明白了構(gòu)思,我們就開始動(dòng)手做了.
建立我們第一個(gè)PHP文件:
你可以用記事本,可以用Dreamweaver也可以用專用PHP編輯軟件
取名字為 commom.php
內(nèi)容為
------------------------
<?php
include './config.php';
include './global.php';
?>-----------------------
這個(gè)文件有什么用?就是在以后操作中直接 inclue 這個(gè)文件就可以用到所有的函數(shù)啊什么的
然后config.php是設(shè)置 URL 刷新時(shí)間 等等
global.php是 所有函數(shù)的文件
也就是今天要給教給大家的!
第一個(gè)個(gè)函數(shù) open
-------------
function open($file,$type=''){
global $fromurl,$referer;
$cachename=$file;
if($type){
$file=$fromurl.'/'.$type.'/'.$file;
}else{
$file=$fromurl.$file;
}
if($open=file($file)){
$count=count($open);
for($i=0;$i<$count;$i++){
$theget.=$open[$i];
}
}else{
die('請(qǐng)求過(guò)多,超時(shí),請(qǐng)刷新');
}
return $theget;
}
----------------
解釋過(guò)了,連接地址就2中請(qǐng)求,根目錄,和 文件夾/名字
函數(shù)怎么用法等等,不多說(shuō)了.建議大家下載譯本PHP中文手冊(cè)看看.
第二個(gè)函數(shù)
根據(jù)設(shè)置時(shí)間更新cache目錄文件函數(shù) update
---------------
function update($file,$type=''){
global $timestamp,$flush;
if(!file_exists("cache/$file")){
if($type){
$data=open($file,$type);
}else{
$data=open($file);
}
writetofile("cache/$file",$data);
}else{
$lastflesh=@filemtime("cache/$file");
if($lastflesh + ($flush * 60) < $timestamp ){
if($type){
$data=open($file,$type);
}else{
$data=open($file);
}
writetofile("cache/$file",$data);
}
}
}
--------
簡(jiǎn)單解釋
$data=open($file,$type);就是用到上面的 open函數(shù)了
如果我們用 udate("index.htm");
那不就是用到了 update函數(shù)嗎? 明白嗎?
上面出現(xiàn)了writetofile函數(shù) 下面是代碼
------------------------------
function writetofile($file_name,$data,$method="w") {
if($filenum=fopen($file_name,$method)){
flock($filenum,LOCK_EX);
$file_data=fwrite($filenum,$data);
fclose($filenum);
return $file_data;
}else{
return false;
}
}---------------------------------
切割字符函數(shù)
------------------------
function cut($file,$from,$end){
$message=explode($from,$file);
$message=explode($end,$message[1]);
return $message[0];
}
----------------------------
讀取函數(shù)
---------------------
function readfromfile($file_name) {
if($filenum=fopen($file_name,"r")){
flock($filenum,LOCK_SH);
$file_data=fread($filenum,filesize($file_name));
fclose($filenum);
return $file_data;
}else{
return false;
}
}
-------------------------------------
把所有函數(shù)寫成一個(gè)文件 保存起來(lái) 取名字叫 global.php
內(nèi)容如下:
------------------------------------------------------------------------------------------------
<?php
function open($file,$type=''){
global $fromurl,$referer;
$cachename=$file;
if($type){
$file=$fromurl.'/'.$type.'/'.$file;
}else{
$file=$fromurl.$file;
}
if($open=file($file)){
$count=count($open);
for($i=0;$i<$count;$i++){
$theget.=$open[$i];
}
}else{
die('請(qǐng)求過(guò)多,超時(shí),請(qǐng)刷新');
}
return $theget;
}
function update($file,$type=''){
//更新cache中的文件
global $timestamp,$flush;
if(!file_exists("cache/$file")){
if($type){
$data=open($file,$type);
}else{
$data=open($file);
}
writetofile("cache/$file",$data);
}else{
$lastflesh=@filemtime("cache/$file");
if($lastflesh + ($flush * 60) < $timestamp ){
if($type){
$data=open($file,$type);
}else{
$data=open($file);
}
writetofile("cache/$file",$data);
}
}
}
function readfromfile($file_name) {
if($filenum=fopen($file_name,"r")){
flock($filenum,LOCK_SH);
$file_data=fread($filenum,filesize($file_name));
fclose($filenum);
return $file_data;
}else{
return false;
}
}
function writetofile($file_name,$data,$method="w") {
if($filenum=fopen($file_name,$method)){
flock($filenum,LOCK_EX);
$file_data=fwrite($filenum,$data);
fclose($filenum);
return $file_data;
}else{
return false;
}
}
function cut($file,$from,$end){
$message=explode($from,$file);
$message=explode($end,$message[1]);
return $message[0];
}
function updatecache($file,$cache=''){
global $timestamp,$flush;
if(!file_exists($file)){
writetofile($file,$cache);
$return=$cache;
}elseif(@filemtime($file) < $timestamp - ($flush * 60)){
writetofile($file,$cache);
$return=$cache;
}else{
$return=readfromfile($file);
}
return $return;
}
?>
-----------------------------------------------------------------------------------------------------
其中有幾個(gè)變量在config.php中設(shè)置一下
我們建立config.php文件 內(nèi)容如下:
<?php
$fromurl = "http://www.onlinedown.net/";
$flush="120";//update函數(shù)中自動(dòng)同步更新時(shí)間
?>
------------------------
現(xiàn)在位置我們有了3個(gè)文件了 commom.php config.php global.php
有了3個(gè)文件 程序總體完成了.接下來(lái)如何去偷呢?
心急的人可以先試試
建立一個(gè)index.php文件 就是首頁(yè)
你先做好模板 的樣子
HTML先做好.
然后在
<html>
........
........
</html>
的上方插入PHP代碼
如下:
<?php
require './commom.php';
update("index.htm");
$file=readfromfile("cache/index.htm");
$gwrj = cut($file,"<TD width=\"307\" height=\"118\">","</TD>");
?>
<html>
.......
......
.......
</html>
在你想要插入的地方插入<?php echo $gwrj; ?>
就是從首頁(yè)中切割出來(lái)的國(guó)外軟件
自己試試
今天就到這里!
下一個(gè)接教如何讀取分類頁(yè)面.和簡(jiǎn)單介紹PHP模板技術(shù)的原理.再下節(jié)講模板,不需要HTML中出現(xiàn)PHP代碼了.分離開來(lái)