dongsuoxi1790 2016-09-15 16:56
浏览 139
已采纳

在PHP中为Dynamic页面创建临时URL

I am generating a php response to a post request as follows

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

This works and is OK.

But I have a different use case.

I want to get the output of this page and create a page.

Then send the user the link to that page.

So the reponse will be a link only.

The page will be destroyed after the user is done with it and need not be stored.

How can this be done in PHP.

  • 写回答

1条回答 默认 最新

  • doutaoer3148 2016-09-15 18:17
    关注

    A straight forward technique to achieve what you want:

    1. Use mod_rewirte and .htaccess to setup "pretty URLs"
    2. Have an index.php to receive the requests and process them:
    3. Create a hash key dynamically for each "page", store it in a database (e.g. MySQL).
    4. The "special" URL will be constructed from your domain path and the hash key for each page (e.g. http://www.my-domain.com/pages/azXi39444)
    5. When a user enters the URL, he will get the output that is related to the key that he has used in the URL, and the hash key will be deleted from the database (of flagged as "deleted")

    So you create the page, build the URL (as mentioned above) - and send it back as a response to the user.

    I will not get into how to setup mod_rewirte and .htaccess but here is a link to a simple guide: https://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049

    An example of an .htaccess file:

    RewriteEngine ON
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url_params=$1 [L,QSA]
    

    This can be a simple example of an index.php (pseudo code):

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

    If you want the pages to be completely virtual, you can ditch the concept of including actual files and store the pages content in a database table, then just pull the data from the database and display accordingly with the page hash\id that was given.

    A pseudo example for a database table to store virtual pages:

    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;
    

    With this approach you use the same concept of .htaccess, url rewrite, index.php and generated hash keys, but instead of including actual files - you are fetching the page content from the database and echo it.

    Hope it helps a bit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配