标题位置在我的 php 代码中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12525251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
header location not working in my php code
提问by michael
i have this code,why my header location not working? its a form of updating and editing and deleting some pages in my control panel...and i have an index.php file in the same folder of form.php...any help please?()i tryed to put the header after the editing and deleting...and still go to the form page not the index...
我有这个代码,为什么我的标题位置不起作用?它是在我的控制面板中更新、编辑和删除某些页面的一种形式……我在 form.php 的同一文件夹中有一个 index.php 文件……有什么帮助吗?()我试着把标题放在后面编辑和删除...仍然转到表单页面而不是索引...
<?php
include "../../includes/site_includes.php";
//send
if ((isset($_POST["send"])) && ($_POST["send"] == 1)) {
$pageid = $_POST["page_id"];
$pagetitle = $_POST["page_title"];
$nameinmenu = $_POST["page_menu_name"];
$nameinurl = $_POST["page_name_url"];
$link = $_POST["page_link"];
$picture = $_POST["page_pic"];
$desc = $_POST["page_desc"];
$content = $_POST["page_content"];
}
if ((isset($_POST["act"])) && ($_POST["act"] == "add")) {
$sql = insertpage();
if ($result = $mysqli->prepare($sql)) {
$result->bind_param("sssssss", $pagetitle, $nameinmenu, $nameinurl, $link, $picture, $desc, $content);
$result->execute();
$result->store_result();
$rows = $result->num_rows;
}
}
////edit
if ((isset($_GET["act"])) && ($_GET["act"] == "edit")) {
$sql = getfrompages();
if ($result = $mysqli->prepare($sql)) {
$rekza = $_GET["id"];
$result->bind_param("i", $rekza);
$result->execute();
$result->store_result();
$rowsZ = $result->num_rows;
}
if ($rowsZ > 0) {
$row = fetch($result);
$pageid = $row[0]["page_id"];
$pagetitle = $row[0]["page_title"];
$nameinmenu = $row[0]["page_menu_name"];
$nameinurl = $row[0]["page_name_url"];
$link = $row[0]["page_link"];
$picture = $row[0]["page_pic"];
$desc = $row[0]["page_desc"];
$content = $row[0]["page_content"];
}
}
if ((isset($_GET["act"])) && ($_GET["act"] == "delete")) {
$thedelid = $_GET["id"];
$sql2 = delpage();
if ($result2 = $mysqli->prepare($sql2)) {
$result2->bind_param("i", $thedelid);
$result2->execute();
$result2->store_result();
$rowsZ2 = $result2->num_rows;
}
}
header('location: index.php');
exit();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> pages add </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<form method="post" action="">
<table>
<tr>
<td style="font-weight:bold;">title</td>
<td><input type="text" name="page_title" value="<?=$pagetitle?>" /></td>
</tr>
<tr>
<td style="font-weight:bold;">name in menu</td>
<td><input type="text" name="page_menu_name" value="<?=$nameinmenu?>" /></td>
</tr>
<tr>
<td style="font-weight:bold;">name in url</td>
<td><input type="text" name="page_name_url" value="<?=$nameinurl?>" /></td>
</tr>
<tr>
<td style="font-weight:bold;">link</td>
<td><input type="text" name="page_link" value="<?=$link?>" /></td>
</tr>
<tr>
<td style="font-weight:bold;">picture</td>
<td><input type="text" name="page_pic" value="<?=$picture?>" /></td>
</tr>
<tr>
<td style="font-weight:bold;">description</td>
<td><textarea name="page_desc"><?=$desc?></textarea></td>
</tr>
<tr>
<td style="font-weight:bold;">content</td>
<td><textarea name="page_content"><?=$content?></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="send" value="1" />
<input type="hidden" name="act" value="<?=$_GET["act"]?>" />
<input type="hidden" name="page_id" value="<?=$pageid?>" />
<input type="submit" value="add" /></td>
</tr>
</table>
</form>
</body>
</html>
solved:with @ Mihai Iorga code i added ob_start();
解决了:使用@ Mihai Iorga 代码,我添加了 ob_start();
回答by air4x
Try adding ob_start();at the top of the code i.e. before the includestatement.
尝试ob_start();在代码的顶部添加,即在include语句之前。
回答by Mihai Iorga
That is because you have an output:
那是因为你有一个输出:
?>
<?php
results in blank line output.
结果是空行输出。
header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP
header() 必须在发送任何实际输出之前调用,无论是通过普通的 HTML 标签、文件中的空行还是来自 PHP
Combine all your PHP codes and make sure you don't have any spaces at the beginning of the file.
合并所有 PHP 代码并确保文件开头没有任何空格。
also after header('location: index.php');add exit();if you have any other scripts bellow.
如果您有任何其他脚本,请在header('location: index.php');添加之后添加exit();。
Also move your redirect header after the last if.
还要在最后一个if.
If there is content, then you can also redirect by injecting javascript:
如果有内容,那么你也可以通过注入javascript来重定向:
<?php
echo "<script>window.location.href='target.php';</script>";
exit;
?>
回答by subindas pm
just use ob_start();before include function it will help
只是ob_start();在包含功能之前使用它会有所帮助
回答by JavaFWS
Remove Space
删除空间
Correct : header("Location: home.php");or header("Location:home.php");
正确:header("位置:home.php"); 或header("位置:home.php");
Incorrect : header("Location :home.php");
不正确:header("位置:home.php");
Remove space between Location and : -->header("Location(remove space): home.php");
删除 Location 和 : -->header("Location( remove space): home.php");
回答by Suyash Jain
The function ob_start() will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. So browser will not receive any output and the header will work.Also we should make sure that header() is used on the top of the code.
函数 ob_start() 将打开输出缓冲。当输出缓冲处于活动状态时,脚本不会发送任何输出(标题除外),而是将输出存储在内部缓冲区中。因此浏览器将不会收到任何输出并且标题将起作用。此外,我们应该确保在代码顶部使用 header()。
回答by Hemi
I use following code and it works fine for me.
我使用以下代码,它对我来说很好用。
if(!isset($_SESSION['user'])) {
ob_start();
header("Location: https://sitename.com/login.php");
exit();
} else {
// my further code
}
回答by Sashi
ob_start();
should be added in the line 1 itself. like in below example
应该添加到第 1 行本身。就像下面的例子
<?php
ob_start(); // needs to be added here
?>
<!DOCTYPE html>
<html lang="en">
// your code goes here
</html>
<?php
if(isset($_POST['submit']))
{
//code to save data in db goes here
}
header('location:index.php?msg=sav');
?>
adding it below html also doesnt work. like below
在 html 下方添加它也不起作用。像下面
<!DOCTYPE html>
<html lang="en">
// your code goes here
</html>
<?php
ob_start(); // it doesnt work even if you add here
if(isset($_POST['submit']))
{
//code to save data in db goes here
}
header('location:index.php?msg=sav');
?>
回答by morcibacsi
It took me some time to figure this out: My php-file was encoded in UTF-8. And the BOM prevented header location to work properly. In Notepad++ I set the file encoding to "UTF-8 without BOM" and the problem was gone.
我花了一些时间才弄明白:我的 php 文件是用 UTF-8 编码的。并且 BOM 阻止了标题位置正常工作。在 Notepad++ 中,我将文件编码设置为“没有 BOM 的 UTF-8”,问题就消失了。
回答by paulalexandru
I had same application on my localhost and on a shared server. On my localhost the redirects worked fine while on this shared server didn't. I checked the phpinfo and I saw what caused this:
我的本地主机和共享服务器上有相同的应用程序。在我的本地主机上,重定向工作正常,而在此共享服务器上却没有。我检查了 phpinfo,我看到了导致这种情况的原因:
While on my localhost I had this:
在我的本地主机上,我有这个:
So I asked the system admin to increase that value and after he did that, everything worked fine.
所以我要求系统管理员增加这个值,在他这样做之后,一切正常。
回答by Pankaj Agrawal
In my case i created new config file with function 'ob_start()' and added this to my .gitignore file.
就我而言,我创建了带有函数“ob_start()”的新配置文件,并将其添加到我的 .gitignore 文件中。


