如何使用PHP动态生成HTML页面?

15
我有一个页面,根据来自URL的唯一ID显示有关物业的信息,通过在MySQL数据库中搜索该ID并获取该行的所有信息等方式获得,非常标准。我在想是否/如何可以为每个数据库行“创建”一个HTML页面,因为我认为这对于SEO来说可能更好?与一个动态页面相比,拥有多个包含关键字的页面会更优秀吗?我的物业是通过网站上的表单/上传系统添加到数据库的,我想在上传时创建页面可能是最简单的方法,但欢迎提供建议!

4
我会尽力(有点儿)创造我自己的东西,主要是出于学习目的。 - rpsep2
你在使用哪种编程语言来编写网页? - Pete
3
你想搜索关于PHP URL重写的内容。 - Pete
8个回答

28

以防有人想要生成/创建实际的HTML文件...

$myFile = "filename.html"; // or .php   
$fh = fopen($myFile, 'w'); // or die("error");  
$stringData = "your html code php code goes here";   
fwrite($fh, $stringData);
fclose($fh);

享受!


我有一个文档,它将打印一个 html 文档。随着 html 文档一起,它会带来来自主 php 文档的变量。我该如何将主 php 文档中的变量打印到动态创建的 html 文档中? - Bruno Francisco

25

我想知道如何为每个数据库行创建html页面

你只需要创建一个php文件生成一个html模板,改变的是该页面上基于文本的内容。在该页面中,您可以通过POSTGET获取参数(例如 行ID),然后从数据库中获取信息。

我假设这样对SEO会更好吗?

搜索引擎例如Google解释example.php?id=33example.php?id=44是不同的页面,从SEO的角度来看,这种方式比单个列表页面更好,所以您至少需要两个php文件 (listing.phpsingle.php),因为最好从listing.php链接这些页面。

额外建议:

example.php?id=33很丑并且不太符合seo友好性,也许您需要一些URL重写代码。像example/properties/property-name这样的形式更好;)


1
接受了这个,因为它提供了一些额外的有用信息,谢谢,我回家后会尝试一些。 - rpsep2

10
根据您的要求,您不需要动态生成HTML页面。这可以通过.htaccess文件完成。
仍然有一个示例代码可用于生成HTML页面。
<?php

 $filename = 'test.html';
 header("Cache-Control: public");
 header("Content-Description: File Transfer");
 header("Content-Disposition: attachment; filename=$filename");
 header("Content-Type: application/octet-stream; ");
 header("Content-Transfer-Encoding: binary");
?>

你可以创建任何 .html 或 .php 文件,只需更改文件名中的扩展名


1
如果您解释一下您的答案而不仅仅提供示例代码,那将会很有帮助。 - oldboy
请提供更多解释 - answerSeeker

4

看起来很有趣,但它确实有效。

<?php 
$file = 'newpage.html';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "<!doctype html><html>
<head><meta charset='utf-8'>
<title>new file</title>
</head><body><h3>New HTML file</h3>
</body></html>
";
// Write the contents back to the file
file_put_contents($file, $current);
?>

3

您不需要生成任何动态 HTML 页面,只需使用 .htaccess 文件并重写 URL。


你必须完成你的回答。 - mercury

3

我一直在从事类似的工作,我有一些代码可能会对你有所帮助。实时示例在这里,下面是我正在使用的代码,供您参考。

create-page.php

<?php

// Session is started.
session_start();

// Name of the template file.
$template_file = 'couples-template.php';

// Root folder if working in subdirectory. Name is up to you ut must match with server's folder.
$base_path = '/couple/';

// Path to the directory where you store the "couples-template.php" file.
$template_path = '../template/';

// Path to the directory where php will store the auto-generated couple's pages.
$couples_path = '../couples/';

// Posted data.
$data['groom-name'] = str_replace(' ', '', $_POST['groom-name']);
$data['bride-name'] = str_replace(' ', '', $_POST['bride-name']);
// $data['groom-surname'] = $_POST['groom-surname'];
// $data['bride-surname'] = $_POST['bride-surname'];
$data['wedding-date'] = $_POST['wedding-date'];
$data['email'] = $_POST['email'];
$data['code'] = str_replace(array('/', '-', ' '), '', $_POST['wedding-date']).strtoupper(substr($data['groom-name'], 0, 1)).urlencode('&').strtoupper(substr($data['bride-name'], 0, 1));

// Data array (Should match with data above's order).
$placeholders = array('{groom-name}', '{bride-name}', '{wedding-date}', '{email}', '{code}');

// Get the couples-template.php as a string.
$template = file_get_contents($template_path.$template_file);

// Fills the template.
$new_file = str_replace($placeholders, $data, $template);

// Generates couple's URL and makes it frendly and lowercase.
$couples_url = str_replace(' ', '', strtolower($data['groom-name'].'-'.$data['bride-name'].'.php'));

// Save file into couples directory.
$fp = fopen($couples_path.$couples_url, 'w');
fwrite($fp, $new_file);
fclose($fp);

// Set the variables to pass them to success page.
$_SESSION['couples_url'] = $couples_url;
// If working in root directory.
$_SESSION['couples_path'] = str_replace('.', '', $couples_path);
// If working in a sub directory.
//$_SESSION['couples_path'] = substr_replace($base_path, '', -1).str_replace('.', '',$couples_path);

header('Location: success.php');

?>

希望这个文件能够帮助您并作为启动和推进项目的参考。

我尝试了这个解决方案.. 但是占位符没有被替换.. 我需要这种类型的解决方案.. - Devi A

1
我会更新代码以包含更改,并添加注释,这样您可以清楚地看到发生了什么…
<?php   
include("templates/header.htm");   

// Set the default name 
$action = 'index'; 
// Specify some disallowed paths 
$disallowed_paths = array('header', 'footer'); 
if (!empty($_GET['action'])) { 
    $tmp_action = basename($_GET['action']); 
    // If it's not a disallowed path, and if the file exists, update $action 
    if (!in_array($tmp_action, $disallowed_paths) && file_exists("templates/{$tmp_action}.htm")) 
        $action = $tmp_action; 
} 
// Include $action 
include("templates/$action.htm"); 

include("templates/footer.htm");

1
我建议您使用URL重写模块解决您的问题,这已经足够了。我也遇到过同样的问题,但是使用URL重写模块并获得良好的SEO反应。我可以给您一个小例子。例如,您考虑WordPress,在这里数据存储在数据库中,但是许多WordPress网站使用URL重写模块获得了Google的良好响应并且排名也很高。
例如: WordPress没有使用URL重写模块的URL - domain.com/?p=123 使用URL重写模式后 - domain.com/{文章标题},如domain.com/seo-url-rewrite-mod
我认为您已经理解了我的意思。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接