2010-10-25 38 views
17

我想知道如何使用PHP创建站点地图& MySQL,并且是否有任何您知道的站点地图设计示例?如何使用PHP和MySQL创建站点地图

+2

什么意思是“网站地图”究竟是 – 2010-10-25 21:15:56

+0

“sitemap.xml”的搜索引擎?还是一个供用户查看?如果是后者,那么这些东西(大部分)在上个千年就失去了风格。 – ceejayoz 2010-10-25 21:16:50

+0

一个用于搜索引擎。谢谢。 – HELP 2010-10-25 21:17:43

回答

0

我不认为你需要MySQL,使用它很重要吗? 你有动态或静态页面吗? 你的问题有点含糊。你想创建一个服务,创建站点地图或只是试图索引你自己的?

Here's a little something to get you started

+0

我有动态页面。我只是想创造我自己的。 – HELP 2010-10-25 21:21:29

+0

那么,为什么还有几十种免费的在线服务可以在几分钟内创建一个网站地图呢? 看看这个:http://www.xml-sitemaps。com/ – Alex 2010-10-25 21:27:12

0

这关乎您的网站的结构。您的网页是否包含在mysql数据库中,并附带一个网站结构,并通过PHP脚本提供给用户,或者您的网站是否仅包含静态.html文件?如果第一个,你只需要将包含在数据库中的网站结构渲染成一个很好的人类可读链接列表。如果第二个,你可以,嗯,粘在一起的工具,通过您的网络文件夹中的所有文件和目录,并制作一个链接列表,可能通过解析html文件中的网站标题:P

2

问题是非常含糊的 - 我们需要知道更多关于您的网站的其他部分,然后才能得到很好的答案。

这取决于您的页面结构以及您想要包含在地图中的内容。

如果您的网站是相对静态的,那么您应该将您的网站地图保存为一个静态页面,因此每次加载时都不会导致额外的处理。但是,如果您的网站经常更新,那么您可能需要经常刷新地图,因此每次加载时刷新的动态地图可能会更好。

如果你的PHP站点有一个CMS结构,并且你的所有页面都包含在CMS中,那么它应该相对简单地运行数据库并将链接拉出到你的所有页面(当然,取决于结构的CMS)。另一方面,如果您的网站的结构不是可以让您这样做,或者您想要限制地图上显示的网页,那么您可能会发现在整个网页上运行蜘蛛更容易。网站并存储结果。

已经有很多已经写好的现有网站地图程序。我搜索了php sitemap generator,并得到了一大堆结果,其中很多看起来可能对你有用,即使只有你可以下载它们来研究它们的源代码。

+0

我可以请你看看我的XML站点地图相关的问题在这里 - tinyurl.com/pgqjdeo? – 2015-10-24 13:34:44

20

我在我的网站上使用它,它运行良好,您可以将Google的网站站长工具指向“this_file.php”,它可以创造奇迹!

 
<?php 
header("Content-type: text/xml"); 
echo'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'; 
echo' <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; 

include '../include.php'; 
$sql = mysql_query("select blah from bleh"); 

while ($string = mysql_fetch_array($sql)){?> 
      <url> 
       <loc>http://www.domain.com/dir/<?echo $string['value'];?>/index.php</loc> 
       <changefreq>weekly</changefreq> 
      </url> 
<?php } ?> 
</urlset> 
+2

将适用于小型网站。但对于拥有大量用户生成数据的网站,它会哭泣。 – 2012-10-06 06:47:33

+0

失去了很多时间试图这个,然后我看到PHP标签不包含<?PHP和我的服务器只接受这样的。也许对某人有帮助。 – 2014-10-02 14:00:46

+0

@RahulPrasad - 缓存结果适用于中型或大型网站。 – Westy92 2017-11-19 23:45:15

0

下面是我用来从数据库表中生成大型站点地图的一些代码。这有点混乱,它与现有的遗留网站交织在一起。我现在正在研究新版本,但可能会帮助某人。

<?php // vim:ai:et:sw=4:ts=4 

/* 
* Generates a sitemap from the database. 
*/ 

include('shared/global.cfg'); 
define('DB_DSN', 'mysql:dbname='.DB_DATABASE.';host='.DB_HOSTNAME); 

/* 
* The page paramter picks out which sitemap to return, or, 
* if no page is specified, returns an index of pages. 
*/ 
$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT); 
if ($page == '') { 
    $page = 'index'; 
} 

define('URLROOT', 'http://la.indymedia.org/'); 
define('SM_PATH', SF_CACHE_PATH.'/sitemap/'); 
$path = sitemap_page_path($page); 
$index = sitemap_index_path(); 

/* 
* If the index file is missing, or is old, regenerate the entire 
* sitemap over. This takes several seconds. 
*/ 
if (file_exists($index)) { 
    $stat = stat($index); 
    $mtime = $stat['mtime']; 
    $diff = time() - $mtime; 
    if ($diff > 24*60*60) { 
     regenerate_sitemap(); 
    } 
} else { 
    regenerate_sitemap(); 
} 

/* 
* Read the cached file, and spit it out. 
*/ 
$text = file_get_contents($path); 
header("Content-type: application/xml"); 
echo $text; 
exit(); 

/* 
* The main function to call to regen the sitemaps. 
* It reads from the database. 
* Paginates the results. 
*/ 
function regenerate_sitemap() { 
    global $index; 
    $pdo = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); 
    $stmt = $pdo->prepare('SELECT id,created FROM webcast WHERE display<>"f" and parent_id=0'); 
    $stmt->execute(); 
    $count = 0; 
    $page = 1; 
    $urls = []; 
    $pageurls = []; 
    while($row = $stmt->fetch()) { 
     $created = $row['created']; 
     $y = substr($created,0,4); 
     $m = substr($created,5,2); 
     $id = $row['id']; 
     $urls[] = URLROOT."news/$y/$m/$id.php"; 
     $count++; 
     if ($count==50000) { 
      write_sitemap_page($page, $urls); 
      $pageurls[] = URLROOT.'sitemap.php?page='.$page; 
      $page++; 
      $urls = []; 
      $count = 0; 
     } 
    } 
    // fixme - we need to make a sitemaps of events, right here. 
    if (count($urls) > 0) { 
     write_sitemap_page($page, $urls); 
     $pageurls[] = URLROOT.'sitemap.php?page='.$page; 
    } 
    write_sitemap_index($pageurls); 
} 

function sitemap_page_path($page) { 
    return SM_PATH.'sitemap_'.$page.'.xml'; 
} 
function sitemap_index_path() { 
    return SM_PATH.'sitemap_index.xml'; 
} 

/* 
* Writes a single sitemap from an array of URLs. 
*/ 
function write_sitemap_page($page, $urlarray) { 
    $path = sitemap_page_path($page); 
    $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n"; 
    $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; 
    foreach($urlarray as $url) { 
     $xml .= '<url><loc>'.$url.'</loc></url>'; 
    } 
    $xml .= '</urlset>'; 
    file_put_contents($path, $xml); 
} 

/* 
* Writes the index of sitemaps. 
*/ 
function write_sitemap_index($urlarray) { 
    $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n"; 
    $xml .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; 
    foreach($urlarray as $url) { 
     $xml .= '<sitemap><loc>'.$url.'</loc></sitemap>'; 
    } 
    $xml .= '</sitemapindex>'; 
    file_put_contents(sitemap_index_path(), $xml); 
}