本节内容:
php路由
在php中实现简单的路由功能,需要用到二个方法:
$_SERVER['QUERY_STRING'] 获取URI?后面的参数
例子,index.php
<?php
$str=explode()("/", $_SERVER['PATH_INFO']); // /classname/method
if(isset()($str[1])){
require_once 'controllers.php';
$test=new test(); //加载类
if(isset($str[2])) //记载方法
{
$test->$str[2]();
}else{
$test->index();
}
}
2,控制器controllers.php代码
class test
{
public function __construct()
{
}
public function index()
{
echo '这是默认的方法';
}
public function test()
{
echo '这是test()方法';
}
public function __call($name,$ar)
{ // www.
//echo $name."<br>";
//echo $ar."<br>";
echo '不要乱调用方法';
}
}
访问:
http://localhost/index.php/class/test
http://localhost/index.php/class/test[]
本节内容:
PHP生成sitemap.xml
将以下代码保存为sitemap.php,上传至服务器,运行即可。
例子:
<?php
header('Content-type: application/xml; charset="GB2312"',true);
$website = "http://www."; /* 域名 */
$page_root = "/"; /*网站的目录地址*/
/* changefreq可自行设置 */
$changefreq = "weekly"; //"always", "hourly", "daily", "weekly", "monthly", "yearly" and "never".
/* 修改时间 */
$last_modification = date("Y-m-d\TH:i:s") . substr(date("O"),0,3) . ":" . substr(date("O"),3);
/* 需要生成的目录 */
$allow_dir[] = "web";
/* 需要过滤的目录(不列在SiteMap里面) */
$disallow_dir[] = "admin";
$disallow_dir[] = "_notes";
/* 设置列表的文件名,扩展名不在其中的话SiteMap则不会收录该扩展名的文件 */
$disallow_file[] = ".inc";
$disallow_file[] = ".old";
$disallow_file[] = ".save";
$disallow_file[] = ".txt";
$disallow_file[] = ".js";
$disallow_file[] = "~";
$disallow_file[] = ".LCK";
$disallow_file[] = ".zip";
$disallow_file[] = ".ZIP";
$disallow_file[] = ".CSV";
$disallow_file[] = ".csv";
$disallow_file[] = ".css";
$disallow_file[] = ".class";
$disallow_file[] = ".jar";
$disallow_file[] = ".mno";
$disallow_file[] = ".bak";
$disallow_file[] = ".lck";
$disallow_file[] = ".BAK";
/* simple compare function: equals */
function ar_contains($key, $array) {
foreach ($array as $val) {
if ($key == $val) {
return true;
}
}
return false;
}
/* better compare function: contains */
function fl_contains($key, $array) {
foreach ($array as $val) {
$pos = strpos($key, $val);
if ($pos === FALSE) continue;
return true;
}
return false;
}
/* this function changes a substring($old_offset) of each array element to $offset */
function changeOffset($array, $old_offset, $offset) {
$res = array();
foreach ($array as $val) {
$res[] = str_replace()($old_offset, $offset, $val);
}
return $res;
}
/* this walks recursivly through all directories starting at page_root and
adds all files that fits the filter criterias */
// taken from Lasse Dalegaard, http://php.net/opendir
function getFiles($directory, $directory_orig = "", $directory_offset="") {
global $disallow_dir, $disallow_file, $allow_dir;
if ($directory_orig == "") $directory_orig = $directory;
if($dir = opendir($directory)) {
// Create an array for all files found
$tmp = Array();
// Add the files
while($file = readdir($dir)) {
// Make sure the file exists
if($file != "." && $file != ".." && $file[0] != '.' ) {
// If it's a directiry, list all files within it
//echo "point1<br>";
if(is_dir($directory . "/" . $file)) {
//echo "point2<br>";
$disallowed_abs = fl_contains($directory."/".$file, $disallow_dir); // handle directories with pathes
$disallowed = ar_contains($file, $disallow_dir); // handle directories only without pathes
$allowed_abs = fl_contains($directory."/".$file, $allow_dir);
$allowed = ar_contains($file, $allow_dir);
if ($disallowed || $disallowed_abs) continue;
if ($allowed_abs || $allowed){
$tmp2 = changeOffset(getFiles($directory . "/" . $file, $directory_orig, $directory_offset), $directory_orig, $directory_offset);
if(is_array($tmp2)) {
$tmp = array_merge($tmp, $tmp2);
}
}
} else { // files
if (fl_contains($file, $disallow_file)) continue;
array_push($tmp, str_replace($directory_orig, $directory_offset, $directory."/".$file));
}
}
}
// Finish off the function
closedir($dir);
return $tmp;
}
}
$a = getFiles($page_root);
echo '<?xml version="1.0" encoding="UTF-8"?>';
?><urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>
<?
foreach ($a as $file) {
?>
<url>
<loc><? echo utf8_encode($website.$file); ?></loc>
<lastmod><? echo utf8_encode(date("Y-m-d\TH:i:s", filectime($page_root.$file)). substr(date("O"),0,3) . ":" . substr(date("O"),3));?></lastmod>
<changefreq><? echo utf8_encode($changefreq); ?></changefreq>
</url>
<?
}
?>
</urlset>
本节内容:
php生成sitemap.xml
例子:
<?php
/**
* 生成sitemap.xml文件
* by www.
*/
//网站根域名
$WebRoot = "http://www./";//网址
//XML文件名称
$XMLFile = "sitemaps.xml";
//要建虑的目录[区分大小写],注意:前面加号是因为0在PHP中表示假,这样取子串位置时就不会返回假
//以本程序所在的目录为当前目录,即扫描的根目录,所以目录前面不用加上"/"
$FilterDir = "+|admin|example|";
//要索引的文件扩展名[小写]
$IndexFileExt = "+|html|";
//XML头部
$XMLText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd\">";
//XML尾部
$XMLEndText = "</urlset>";
echo "开始构建文件XML索引...";
DealFP(".");
$XMLText .= $XMLEndText;
makeFile($XMLFile,$XMLText);
echo "ok!<br><br>";
$url = $WebRoot.$XMLFile;
echo "<a href="/blog_article/.$url">打开</a>:".$url;
//公用函数库:
//新建文件
function makeFile($fileName, $text){
$fp = fopen($fileName, "w+");
fwrite($fp, $text);
fclose($fp);
}
/**
* 将指定内容添加到XML中
* $f 含相对路径的文件名称
* $dt 日期时间型
*/
function addToXML($f, $dt){
$s = "<url><loc>".$GLOBALS["WebRoot"].$f."</loc>\n<lastmod>".$dt."</lastmod>\n<changefreq>daily</changefreq>\n<priority>1</priority></url>\n";
$GLOBALS["XMLText"] .= $s;
}
/**
* 遍历指定的目录以及子目录,将符合条件的文件加入XML
* $p 指定的目录
*/
function DealFP($p){
$FilterDir = $GLOBALS["FilterDir"];
$IndexFileExt = $GLOBALS["IndexFileExt"];
$handle=opendir($p);
if ($p==".") $path = "";
else $path = $p."/";
while ($file = readdir($handle))
{
$d = filetype($path.$file);
if ((($d=='file')||($d=='dir'))&&($file!='.')&&($file!='..'))
{
$pf = $path.$file;
//echo "[".$d."]".$pf."<br>";
if ($d=='dir')
{
if (!(strpos($FilterDir, "|".$pf."|")))
{
DealFP($pf);
}
}else{
$ext = "|".strtolower()(substr($file, strrpos($file, ".")+1))."|";
if (strpos($IndexFileExt, $ext))
{
$d = filemtime($pf);
$dt = date("Y-m-d",$d)."T".date("H:i:s",$d)."+00:00";
addToXML($pf, $dt);
}
}
}
}
closedir($handle);
}
?>