PHP 标头未按预期工作
前面讨论过同样的问题,我也知道如果我们期望调用标头函数,那么之前什么都不能发送到浏览器,也不能发送空格。我在这里做错了,我之前打印了 HTML div 标签,所以解决方案是遵循基本策略,这样一切都会按照我想要的方式工作!仅供参考,我可以使用 AJAX(使用 jQuery)来做到这一点,只需从调用的 php 脚本返回 JSON 编码输出作为响应(它可能是我在下面的代码中显示的结果部分或错误)——使用 AJAX 一切都可以工作。但由于它是一个搜索页面,我为用户提供了一些便利,例如 URL 中的 search_query 的 URL 重写,因此如果用户愿意,则无需填写表单 - 因此使用 GET 方法。下面是我实现此目的的代码:
<div class="two-third">
<?php if($_GET['err'] == "no_results_found") {
echo "<p class='error-box'>No results found for your query. Please search using different specific keywords.</p>";
} ?>
<form method="GET" action="<?php echo BASE_URL; ?>/content.php" id="search_content_form">
<fieldset>
<div>
<label>Search this site: </label>
<input type="text" title="Enter your query" class="form-poshytip" id="search_query" name="search_query" value="<?php echo $search_query; ?>">
</div>
<p><input type="submit" id="search_content_submit_button" value="Search Content"></p>
</fieldset>
</form>
<?php if(isset($search_query)) {
$query = "SELECT * FROM (
(SELECT 'event' AS content_type, event_id AS content_id, event_title AS content_title, publishing_time AS content_publishing_time FROM event WHERE event_title LIKE '%".$search_query."%' OR content LIKE '%".$search_query."%')
UNION
(SELECT 'article' AS content_type, article_id AS content_id, article_title AS content_title, publishing_time AS content_publishing_time FROM article WHERE article_title LIKE '%".$search_query."%' OR content LIKE '%".$search_query."%')
) content_result
ORDER BY content_result.content_publishing_time DESC
LIMIT 500
";
$result = mysql_query($query);
if(!$result) {
header("location:".BASE_URL."/content.php");
}
if(mysql_num_rows($result) == 0) {
header("location:".BASE_URL."/content.php?err=no_results_found");
}
$i = 0;
echo "<div id='search_content_results' class='list'>";
echo "Found <strong>".mysql_num_rows($result)."</strong> result(s) for search query: \"<strong>".$search_query."</strong>\"";
while($row = mysql_fetch_assoc($result)) {
// extract all needed varaibles from $row
$content_type = $row['content_type'];
$content_id = $row['content_id'];
$content_title = $row['content_title'];
$content_publishing_time = $row['content_publishing_time'];
echo "
<ul>
<li><a href='".BASE_URL."/".$content_type."s.php?".$content_type."_id=".$content_id."'>".($i+1).") <strong>".$content_title."</strong> <span style='font-size: 12px; text-decoration: italic;'>(Published On: ".date('M jS Y', strtotime($content_publishing_time)).")</span></a></li>
</ul>
";
$i++;
}
echo "</div>";
}
?>
</div>
<!-- ENDS two-third -->
仅供参考:上面的 $search_query 是 $_GET['search_query'] 的别名
上面代码中的所有内容都有效,除了调用 header() 的 if 块之外。我知道原因,正如前面提到的。只需帮助我检查页面顶部我一直在使用的 IF 块中的所有内容,然后显示搜索表单,然后当 num_rows 大于 0 时回显搜索查询的输出,或者如果 num_rows 则显示错误(如上面的代码所示) MySQL 查询的结果是 0。抱歉我的语言很差(而且格式也不正确),因为我很着急。 ;)
编辑: 逻辑很简单:
- $_GET['search_query'] 然后执行查询并检查 num_rows
- num_rows==0 然后使用“?err=example_error_message”发送重定向到同一页面,并在表单上方显示该错误消息,并且同时不打印结果输出
- num_rows>0 然后打印 $row 值(比方说,最终输出),就像我在上面的代码中所做的那样
- 没有 $_GET['search_query'] 然后只搜索表单
所以请帮助我实现这些。
Same problem was discussed earlier and I also knew that nothing, not also a whitespace can be sent to browser before if we've expected call to header function. I am doing wrong here, I am printing HTML div tags before, so solution is to just follow a basic strategy so as everything would work that I wanted to be! FYI, I can do this with AJAX (Using jQuery) by just returning JSON encoded output from called php script as a response (it may be result part or error I am displaying in below code)--everything would work by using AJAX. But as it's a search page, I am availing facility to user like url rewriting for search_query in the URL so there will be no need to fill the form if user wants--Hence using GET method. Below is my code I'm achieving this:
<div class="two-third">
<?php if($_GET['err'] == "no_results_found") {
echo "<p class='error-box'>No results found for your query. Please search using different specific keywords.</p>";
} ?>
<form method="GET" action="<?php echo BASE_URL; ?>/content.php" id="search_content_form">
<fieldset>
<div>
<label>Search this site: </label>
<input type="text" title="Enter your query" class="form-poshytip" id="search_query" name="search_query" value="<?php echo $search_query; ?>">
</div>
<p><input type="submit" id="search_content_submit_button" value="Search Content"></p>
</fieldset>
</form>
<?php if(isset($search_query)) {
$query = "SELECT * FROM (
(SELECT 'event' AS content_type, event_id AS content_id, event_title AS content_title, publishing_time AS content_publishing_time FROM event WHERE event_title LIKE '%".$search_query."%' OR content LIKE '%".$search_query."%')
UNION
(SELECT 'article' AS content_type, article_id AS content_id, article_title AS content_title, publishing_time AS content_publishing_time FROM article WHERE article_title LIKE '%".$search_query."%' OR content LIKE '%".$search_query."%')
) content_result
ORDER BY content_result.content_publishing_time DESC
LIMIT 500
";
$result = mysql_query($query);
if(!$result) {
header("location:".BASE_URL."/content.php");
}
if(mysql_num_rows($result) == 0) {
header("location:".BASE_URL."/content.php?err=no_results_found");
}
$i = 0;
echo "<div id='search_content_results' class='list'>";
echo "Found <strong>".mysql_num_rows($result)."</strong> result(s) for search query: \"<strong>".$search_query."</strong>\"";
while($row = mysql_fetch_assoc($result)) {
// extract all needed varaibles from $row
$content_type = $row['content_type'];
$content_id = $row['content_id'];
$content_title = $row['content_title'];
$content_publishing_time = $row['content_publishing_time'];
echo "
<ul>
<li><a href='".BASE_URL."/".$content_type."s.php?".$content_type."_id=".$content_id."'>".($i+1).") <strong>".$content_title."</strong> <span style='font-size: 12px; text-decoration: italic;'>(Published On: ".date('M jS Y', strtotime($content_publishing_time)).")</span></a></li>
</ul>
";
$i++;
}
echo "</div>";
}
?>
</div>
<!-- ENDS two-third -->
FYI: $search_query above is a alias of $_GET['search_query']
Everything in above code works except the if blocks where header() is called. I know the reason, as mentioned earlier. Just help me to check everything at top of page which is in IF blocks I've been using, then display search form and then echoed output of search query when num_rows are more than 0 or display error (as in in above code) if num_rows of result of MySQL query is 0. Sorry for my poor language (also, not proper formating too) as I'm in a hurry. ;)
EDIT:
Logic is simple:
- $_GET['search_query'] then execute query and check for num_rows
- num_rows==0 then send redirect to same page with "?err=example_error_message" and display that error message above form and do not print the results output at the same time
- num_rows>0 then print $row values (let us say, final output) as I've done in above code
- No $_GET['search_query'] then only search form
So please help me to achieve these.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不完全确定我是否理解你的问题,但无论如何我都会尝试一下。
当您使用 PHP 的
header
函数时,您必须确保在调用header
函数之前不要写入任何输出。然而有时这很难做到。为了解决这个问题,您需要使用输出缓冲。将
ob_start()
(docs) 为脚本的第一行,以及ob_end_flush
(文档)或ob_end_clean
(文档)作为最后一行(或在使用标题函数之后)。这有效地缓冲了所有输出,允许您在实际回显任何输出之前调用标头函数(因为所有输出都会被缓冲,直到您调用 ob_end_flush 或 ob_end_clean )。I'm not entirely sure whether I understand your question, but I'll give it a go anyway.
When you are working with PHP's
header
functions, you must indeed make sure not to write any output before the call to theheader
function.Sometimes this is however difficult to do. In order to solve this, you need to use Output buffering. Put
ob_start()
(docs) as the very first line of your script, andob_end_flush
(docs) orob_end_clean
(docs) as the last line (or after the use of the header functions). This effectively buffers all output, which allows you to call the header functions before any output is actually echoed (as all output is buffered until you callob_end_flush
orob_end_clean
).正如 Joris 提到的使用
ob_start
一样,请记住在设置Location
后始终exit
或die()
标题(除非在某些情况下,如果您确切知道自己在做什么)。As well as using
ob_start
as mentioned by Joris, remember to ALWAYSexit
ordie()
after setting aLocation
header (except in certain circumstances if you know exactly what you're doing).