繁体   English   中英

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

[英]How to generate a HTML page dynamically using PHP?

我有一个页面,该页面根据url中的唯一ID显示有关属性的信息,在mysql数据库中搜索该ID,从该行中获取所有信息,等等,这确实很标准。

我想知道是否/如何为每个数据库行“创建”一个html页面,因为我认为这对SEO更好? 有多个带有关键字的页面,而不是一个动态页面?

我的属性是通过网站上的表单/上载系统添加到数据库的,我当时认为创建上载页面可能是最简单的,但欢迎提出建议!

我想知道是否/如何为每个数据库行“创建” html页面

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

我以为这对于SEO会更好?

Google将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更好;)

万一有人想生成/创建实际的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页面。 可以通过.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文件,只需更改文件名的扩展名

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

看起来很有趣,但是有效。

<?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);
?>

我建议您使用URL重写mod足以解决您的问题,我也遇到同样的问题,但是使用URL重写mod并获得良好的SEO响应。 我可以举一个小例子。 例如,您考虑使用WordPress,这里的数据存储在数据库中,但是使用URL重写mod,许多WordPress网站都得到了Google的良好响应并获得了排名。

示例:不带url重写mod的wordpress url-url重写模式后domain.com/?p=123-domain.com/ {文章标题}如domain.com/seo-url-rewrite-mod

我想你已经了解了我想说的你

我一直在从事与此类似的工作,并且我有一些代码可能会对您有所帮助。 在活生生的例子是在这里和下面,是我用你把它作为参考的代码。

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');

?>

希望此文件可以帮助并作为启动和增强您的项目的参考。

我将仅更新代码以包含所做的更改,并对其进行注释,以便您可以清楚地了解正在发生的事情...

<?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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM