首页 > 解决方案 > 我想在我的 URL 后面创建一个唯一的 id 并在其上回显一个页面怎么做?

问题描述

我有一个表格,网址是例如:www.site.com/index.php. 但是当我单击表单按钮时,我希望它创建一个这样的链接www.site.com/index.php?1wejf23912,然后在其上回显一个页面,您可以在其中输入用户名和密码。如何执行此操作并从示例页面上的该页面获取输入,例如www.site.com/index.php.

谢谢您的帮助。


<?php
    require 'includes/header.php';
?>

<main>
    <form action="includes/create-page.inc.php">
        <button type="submit" name="create-submit" id="create-submit">Create Link
            With Page</button>
    </form>

    <div class="show-input">
        <a href="new-link">This is your link: <?php echo $newLink; ?></a>
        ...input from created page
        <button type="submit" name="delete-url" id="delete-url">Delete New URL</button>
    </div>
</main>

<?php
    require 'includes/footer.php';
?>

标签: php

解决方案


创建一个文件夹c,您将添加它。

<?php
        function randomURL() {
            $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
            $pass = array(); //remember to declare $pass as an array
            $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
            for ($i = 0; $i < 8; $i++) {
                $n = rand(0, $alphaLength);
                $pass[] = $alphabet[$n];
            }
            return implode($pass); //turn the array into a string
        }
        $rpass = randomURL();
        echo "<h2>You link is <a target='_blank' href='/c/". $rpass. "'>". $_SERVER["REQUEST_SCHEME"]. "://". $_SERVER["HTTP_HOST"]. "/c/";
        echo $rpass;
        echo "</a></h2>";
        $myfile = fopen("../c/". $rpass .".php", "w") or die("Unable to create file!");
        $txt = "Your text or variable or php code" ."\n";
        fwrite($myfile, $txt);
        fclose($myfile);
?>

在c文件夹中使用 .htaccess 。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

谢谢


推荐阅读