[英]Create Temporary url for a Dynamic page in PHP
我正在生成对发布请求的php响应,如下所示
<?php
ob_start();
include_once('includes/headers.php');
require_once('includes/connection.php');
require_once('includes/functions.php');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
</head>
<?php
include($filePath);
?>
这可以正常工作。
但是我有一个不同的用例。
我想获取此页面的输出并创建一个页面。
然后向用户发送指向该页面的链接。
因此,响应仅是一个链接。
用户使用完该页面后,该页面将被破坏,无需存储。
如何在PHP中完成此操作。
一种实现所需目标的简单方法:
因此,您创建了页面,构建了URL(如上所述)-并将其作为响应发送回给用户。
我不会介绍如何设置mod_rewirte和.htaccess,但这里有一个简单指南的链接: https ://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
.htaccess文件的示例:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_params=$1 [L,QSA]
这可以是index.php(伪代码)的简单示例:
<?php
$params = explode('/',$_GET['url_params']);
$hash = $params[0];
// not a real database adapter - but you get the idea..
$db = new DatabaseAdapter('username','password','database');
$result = $db->querySingle("SELECT pageFileName FROM tb_Pages WHERE hash = %1", $hash);
if($result){
// delete the used hash
$db->query("DELETE FROM tb_Pages WHERE hash = %1", $hash);
// include the path of the page for display
include('/mySecretFolder/'.$result->pageFileName );
}else{
echo 'Page not found';
exit;
}
?>
如果希望页面是完全虚拟的,则可以放弃包含实际文件的概念,并将页面内容存储在数据库表中,然后仅从数据库中提取数据,并使用给定的页面hash \\ id进行显示。
用于存储虚拟页的数据库表的伪示例:
CREATE TABLE tb_Pages
(
PageID INT AUTO_INCREMENT,
CreateDate Timestamp,
HashKey VARCHAR(100) NOT NULL,
HtmlContent TEXT,
INDEX(HashKey),
PRIMARY KEY(PageID)
)ENGINE=InnoDB CHARACTER SET utf8;
通过这种方法,您将使用.htaccess,URL重写,index.php和生成的哈希键的相同概念,但是不是包括实际文件,而是从数据库中获取页面内容并回显它。
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.