我正在开发一个PHP项目,我尝试使用header(Location:something.php)从一个页面重定向到另一个页面。它在本地计算机上运行正常,但在上载到服务器时,会发生以下错误:
Warning: Cannot modify header information - headers already sent by (output started at /homepages/4/d404449574/htdocs/yellowandred_in/newTest/editHome.php:15) in /homepages/4/d404449574/htdocs/yellowandred_in/newTest/editHome.php on line 43
我已尝试使用include
,include_once
,require
和require_once
,但它会出现其他错误:
Cannot redeclare logged_in() (previously declared in C:\wamp\www\ynrNewVersion-1\editHome.php:3) in C:\wamp\www\ynrNewVersion-1\adminIndex.php on line 5
我的代码
<?php
session_start();
function logged_in() {
return isset($_SESSION['username']);
}
function confirm_logged_in() {
if (!logged_in()) {
include "error404.php";
exit;
}
}
confirm_logged_in();
require_once("connection.php");
$query="select * from home";
$homeInfo = mysql_query($query, $connection);
$result = mysql_fetch_array($homeInfo);
$content = $result['content'];
$rssFeeds = $result['rssFeeds'];
$message = "";
if(isset($_POST['submit'])){
$content = $_POST['content'];
$rssFeeds = $_POST['rssFeeds'];
if($content == "" || $rssFeeds == ""){
$message = "Please enter into all the fields";
} else {
$query = "UPDATE home SET content='$content',rssFeeds='$rssFeeds' WHERE id=1 LIMIT 1";
$result = mysql_query($query,$connection);
if(!$result){
echo "error".mysql_error();
}
if ($result == 1) {
header("Location:adminIndex.php");
} else {
$message = "Error Occurred";
}
}
}
if(isset($_POST['cancel'])){
header("Location:adminIndex.php");
}
?>
答案 0 :(得分:0)
在PHP页面顶部使用ob_start();
答案 1 :(得分:0)
你需要在header函数之后放一个exit()或die() - 否则脚本的其余部分将继续执行。
重定向可以采用相对或绝对URL。问题在于结肠前的空间。试试这样:
header("Location: adminIndex.php");
die();
答案 2 :(得分:0)
问题是你的标题功能之前有回声。
if(!$result){
echo "error".mysql_error();
}
在你的两个标题(“Location:adminIndex.php”)之前,你无法回应任何东西。