php - 如何防止页面一起运行或在 PHP 中组合
问题描述
我有两个页面,都是用 PHP 编写的,但是在查看页面时,服务器将这两个页面组合在一起并将它们作为一个页面提供服务。我怎样才能防止这种情况,并收到两个单独的页面?SuperGlobals 在我的环境中受到限制,并且没有 javascript。
- 第一页 (index.php)
这是该站点的索引页面。我想要求用户在查看此页面之前正确输入验证码。
<?php
require_once __DIR__ . '/captcha.php';
?>
<!doctype html>
<html lang="en">
<head>
<title>You Win!</title>
<meta charset="utf-8" />
</head>
<body>
<h1>You Really Win!</h1>
<h2>Win Baby, Win!</h2>
</body>
</html>
- 第二页 (captcha.php)
这是验证码所在的页面,也是我希望在索引页面之前首先出现的页面。
<?php
//To save time and space, This page is highly abbreviated from the actual php file.
require_once __DIR__ . '/vendor/autoload.php';
use Gregwar\Captcha\PhraseBuilder;
use Gregwar\Captcha\CaptchaBuilder;
$captcha = new CaptchaBuilder;
$captcha->build();
$phrase = $captcha->getPhrase();
$phrase = $_SESSION['phrase'];
$check_phrase = PhraseBuilder::comparePhrases($_SESSION['phrase'], $_POST['phrase']);
if (isset($_SESSION['phrase']) && $check_phrase === true)
{
header('Location: ' . __DIR__ . '/index.php');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<form method="post">
<div>
Copy the CAPTCHA:
</div>
<div>
<img src="<?php echo $captcha->inline(); ?>" alt="Captcha"/>
</div>
<br>
<div>
<label>
<input type="text" name="phrase" />
</label>
<input type="submit" />
</div>
<br>
<div>
</div>
</form>
</html>
那么如何将页面彼此分开并使服务器将它们作为两个单独的页面提供服务呢?
解决方案
@Barmar 提出的建议是一个开始,但并没有让我成为大本营。通过使用管理两个文件之间的重定向的第三个文件解决了该问题。整个过程在下面提供的图表中进行了解释。
- 索引.php
require_once
为了使用条件来管理重定向,仍然有助于在此文件上使用。唯一改变的是所需的文件,它是重定向管理器文件start.php
。
require_once (start.php)
- 开始.php
该文件是该问题的新增内容和发现的解决方案。它使用条件来测试用户应该被转发到哪个页面。
if (strcmp($user_secret, $server_secret)
{
header('Location: index.php')
}
else //inferred not actual
{
header('Location: captcha.php')
}
- 验证码.php
文件中也几乎没有变化,只是便于使用require_once
其中的条件句来将使用引导到索引页面。
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Gregwar\Captcha\PhraseBuilder;
use Gregwar\Captcha\CaptchaBuilder;
$captcha = new CaptchaBuilder;
$captcha->build();
$phrase = $captcha->getPhrase();
$phrase = $_SESSION['phrase'];
$check_phrase = PhraseBuilder::comparePhrases($_SESSION['phrase'], $_POST['phrase']);
if (isset($_SESSION['phrase']) && $check_phrase === true)
{
require_once 'start.php';
}
?>
<form method="post">
<div>
Copy the CAPTCHA:
</div>
<div>
<img src="<?php echo $captcha->inline(); ?>" alt="Captcha"/>
</div>
<br>
<div>
<label>
<input type="text" name="phrase" />
</label>
<input type="submit" />
</div>
<br>
<div>
</div>
</form>
</html>
<?php
}
?>
而且,它完全按照我的预期工作。代码更简单,更直接,过程也更少混乱。
推荐阅读
- mongodb - 将 MongoDB Atlas 数据划分为领域
- php - 在 Laravel 中进行 ajax 验证后的电子邮件确认问题
- image - 如何在点击gridView tile时显示图像的原始大小
- indexing - 狮身人面像 index_sp 的缺点?
- linux - 如何在 Linux 上通过 bash 检查完整的 Firefox 浏览器或 FF 浏览器选项卡是否已崩溃或冻结
- sql - 输入末尾的 PostgreSQL 语法错误不知道为什么
- c++ - 将多类型向量的字符串部分与一种类型向量进行比较并擦除重复项的更快方法
- javascript - 将消息同步发布到 worker_thread。发送线程阻塞,worker_thread 解除阻塞
- matlab - 在 Julia 中将复数向量转换为稀疏对角线数组
- sql - 关于服务器版本的 MySQL 语法错误的解决方案?