php - 输出缓冲:用 HTML 代码制作字符串的简单方法
问题描述
我目前正在使用 Chatfuel 打开我网站的 index.php 文件,该文件将 html 代码发送到用户的浏览器中。他可以在那里注册并设置他的帐户。示例 URL 可能如下所示:
https://my.domain.com?key_value='123456789'
根据该用户是新用户还是现有用户,我想向他展示不同的表单。为了检查这一点,我对 MySQL 数据库进行了一个简单的查询,看看传递的 key_value 是否已经在数据库中,并且对于布尔值是安全的 true 或 false。显而易见:如果他不是现有用户,则应该显示没有值的“空”表单。如果他注册了,他应该会看到他上次填写的信息。
我的想法: 在我的 index.php 顶部,我检查他是否是现有客户(注意:这已经有效)。然后我想使用 outputbuffering 来根据布尔值更改 html 代码,然后再将其发送到客户端。
我的问题:
我用纯 html 开发了网站的蓝图(见下面的代码)。如果它在字符串中,OB 只会将其捕获为输出。由于我在文档中使用"
以及'
字符串每隔几行就会中断一次。有一个简单的解决方法吗?因为 OB 功能无法访问<html>...</html>
标签内的任何内容。或者我是否需要在检查后使用重定向(在我的 index.php 中)并为编辑客户数据和添加新客户数据创建单独的表单 + 脚本?
<?php
//Connection stuff
// Prepare statment: !TODO: string needs to be escaped properly first
$query_string = "SELECT * FROM tbl_customer WHERE unique_url = '$uniqueurl'";
$query_rslt = mysqli_query($conn, $query_string);
if($query_rslt == FALSE)
{
// Failure
echo "<br> Oops! Something went wrong with the querying of the db. " . $conn->connect_error;
//Handle error
}
else
{
if ($query_rslt->num_rows > 0)
{
// Set boolean
$existing_customer = TRUE;
// Create an array called row to store all tuples that match the query string
while($row = mysqli_fetch_assoc($query_rslt)) {
//...
}
}
}
// Custom post processing function
function ob_postprocess($buffer)
{
// do a fun quick change to our HTML before it is sent to the browser
$buffer = str_replace('Testing', 'Working', $buffer);
// Send $buffer to the browser
return $buffer;
}
// start output buffering at the top of our script with this simple command
// we've added "ob_postprocess" (our custom post processing function) as a parameter of ob_start
if (!ob_start('ob_postprocess'))
{
// Failure
echo "<br> Oops! Something went wrong with output buffering. Check that no HTML-Code is sent to client before calling this start function.";
// Handle error
}
else
{
// Success
// This is where the string should get accessed before sending to the client browser
echo "Testing OB.";
}
?>
<!--DOCTYPE html-->
<html lang="en">
<head>
<meta charset="utf-8">
//...
</body>
</html>
<?php
// end output buffering and send our HTML to the browser as a whole
ob_end_flush();
?>
输出: "Working OB."
编辑:我添加了源代码示例。此代码不会编译。
解决方案
因为,我不能发表评论,所以我会在这里提出一些问题。
我真的不明白这一点,但给我一个尝试,你的意思是escaping
字符串吗?您可以使用反斜杠\
来转义字符串。
像这样"select from ".$dbname." where id = \"".$id."\""
。
在将addslashes($var)
变量添加到sql
. 像这样
$id = addslashes($_POST['id']);
$sql = "select form db where id = '$id'";
如果您的意思是检查用户的存在以选择在页面中显示哪个表单,您为什么不这样做呢?
if(userCheck()) {
?>
// here write the html code if user passed
<?php
} else {
?>
// here write the html code if user not passed
<?php
}
您可以将userCheck()
其作为全局函数或放置在任何位置,只要在显示form
.
推荐阅读
- mqtt - 使用 caddy 2 的反向代理 websocket (paho)
- puppeteer-sharp - 我无法单击 Power BI 中的任何按钮
- latitude-longitude - 将经纬度四舍五入到大约 500 米以内
- javascript - 如何根据 React 和 JS 中的内容更改颜色文本
- visual-studio - 如何在 Visual Studio 中检查函数的返回值?
- kivy - Kivy MDDropDownMenu 回调函数未被调用
- file-upload - 使用表单数据发布 PDF 文件:文件损坏?
- r - 将趋势线添加到 DT 表
- sql - 使用 sql (oracle) 识别列中的重复项
- ruby-on-rails - Rails 将一行添加到只有标题且没有数据的 csv 删除标题