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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分