【发布时间】:2018-09-01 23:30:45
【问题描述】:
我目前正在使用 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."
编辑:我添加了源代码示例。此代码无法编译。
【问题讨论】:
-
显示一些示例代码,您可以在其中处理该纯 html 字符串以及该 html 的字符串或文件内容。你会得到更好的答案,因为你要求解决的问题可能比它阻止你的问题更早开始,更进一步的解决方案是修补错误而不是删除它。
-
代码已添加。您要求的功能是
ob_postprocess。它目前只是一个占位符,用于将使用真正的 html 代码完成的操作(一旦它工作)。它作为参数传递给ob_start('ob_postprocess'),并在调用ob_end_flush()时立即发送给客户端(参见代码cmets 或object buffering)。 “我要问的问题......”是什么意思?对不起,我没有得到那部分。 -
你不能通过
file_get_contents()从文件中读取你的html吗?由于您可能希望在某个时候从数据库调用或其他任何内容中输入动态值,因此请使用令牌或占位符,然后通过对其中一个字符串替换函数的一系列调用来运行它 -
好吧,所以你建议我在他的回答中转义像 Cik Irvan 这样的字符串,然后将其放入一个名为 content 的变量中?或者这个代码
$form_content = file_get_contents($file_name,FALSE,NULL,62,157);会成功吗(这意味着它会自动转义我的html代码)?我会对此进行一些研究。 Ty fth(int) : 意外的文字游戏 -
唯一的问题是字符串替换hack,可以用直接的变量显示替换(如果数组键不存在,您可以使用字符串数组和带有空字符串后备的转义函数)。那是您在报价方面遇到麻烦的地方吗?尝试用硬编码的 html 字符串替换部分输出?
标签: php html string output-buffering