This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 5 years ago.
IK this is prob a dupe, but I cant find any solutions to my code what so ever, no matter what code i add/change, it just wont budge... (NOTE im not using normal php, using a friends version, which yes, is fine and dandy)
<?php
include($_SERVER['DOCUMENT_ROOT'].'/phpAlphaDB/core.php');
?>
<html>
<head>
<link rel="icon" type="image/png" href="../logo.png">
<title> Xenoz Web - Users </title>
<link rel="stylesheet" type="text/css" href="../css/style.php" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<?php
if ($role==0) {
header('Location: http://xenozweb.tk/index.php');
} else {
echo '<script>alert("Hello, '.$username.'. Welcome to the userlist...");</script>';
}
?>
<body>
<div class="navigation">
<div class="navitem"> Home </div>
<div class="navitem"> Register </div>
<div class="navitem"> Login </div>
<div class="navitem"> Users </div>
<div class="navitem"> Jukebox </div>
</div>
</div>
<div class="pagecontent">
<div class="userblock">
<?php
$results = db_read('xenozweb-users', '', 'username');
foreach ($results as $result) {
$u_name = db_column($result, 0);
echo '<div class="users">',$u_name,'</div>';
}
?>
</div>
</div>
</body>
</html>
You're returning a partial response before you set the header. Headers must be sent before a response is sent back to the browser.
Try moving the header('Location: ') function call into the top <?php enclosure like so:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/phpAlphaDB/core.php');
if ($role==0) {
header('Location: http://xenozweb.tk/index.php');
}
?>
<html>
<head>
<link rel="icon" type="image/png" href="../logo.png">
<title> Xenoz Web - Users </title>
<link rel="stylesheet" type="text/css" href="../css/style.php" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<?php
if ($role != 0) {
echo '<script>alert("Hello, '.$username.'. Welcome to the userlist...");</script>';
}
?>
</head>
<body>
You've already sent data to the browser. you cannot send header() info properly once you've already started to send data payload.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am having trouble getting my for loop to work in php. I am trying to make my code loop the time ten times with my css formating
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="clockloop.css">
</head>
<body>
<div id="bodycontainer">
<h1> Clock Loop </h1><hr>
<?php for($i=0;$i<=10;$i++){
<div id="border">
<span id = "font">
<?php
echo date("G:i:s")
?>
</span>
</div>
<h3> Today is
<?php
echo date("F,j,Y")
?>
</h3>
}
?>
</div>
</body>
</html>
You messed up your php tags opening and closing in the wrong places
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="clockloop.css">
</head>
<body>
<div id="bodycontainer">
<h1> Clock Loop </h1><hr>
<?php for($i=0;$i<=10;$i++){?>
<div id="border">
<span id = "font">
<?php
echo date("G:i:s")
?>
</span>
</div>
<h3> Today is
<?php
echo date("F,j,Y")
?>
</h3>
<?php
}
?>
</div>
</body>
</html>
CONVERSELY .. You said you wanted 10 times .. This will output 11, as 0 is still a quantifiable number ..
You cannot just output HTML in PHP like that. You can echo or you can jump in and out like this:
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="clockloop.css">
</head>
<body>
<div id="bodycontainer">
<h1> Clock Loop </h1><hr>
<?php for($i=0;$i<=9;$i++){ ?><!-- note the closing PHP tag -->
<div id="border">
<span id = "font">
<?php
echo date("G:i:s")
?>
</span>
</div>
<h3> Today is
<?php
echo date("F,j,Y")
?>
</h3>
<?php } ?><!-- note the opening PHP tag -->
</div>
</body>
</html>
If you want 10 repeats you should end your count at 9 because 0 will be your first record. You could also start with $i = 1; and use 10 as the count to number.
Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1);this will reveal when you have errors in your syntax and alert you on where you should start debugging.
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="clockloop.css">
</head>
<body>
<div id="bodycontainer">
<h1> Clock Loop </h1><hr>
<?php for($i=0;$i<10;$i++){ ?> => look at this line
<div id="border">
<span id = "font">
<?php
echo date("G:i:s")
?>
</span>
</div>
<h3> Today is
<?php
echo date("F,j,Y")
?>
</h3>
<?php => look at this line
}
?>
</div>
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
I have programmed a web application, which requires you to login first, before having access to the actual content of the application.
I am setting the value 'logedIn' to false when a user enters the homepage like this:
<?php
$_SESSION['logedIn'] = "false";
?>
If the user tries to go to overview.php, the code checks if the value of logedIn equals 'true'. If it is not, a header relocates the window to the login page like this:
<?php
session_start();
?>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://jquery-ui.googlecode.com/svn-history/r3982/trunk/ui/i18n/jquery.ui.datepicker-nl.js"></script>
<script src="../javascript/overview.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/mainpage.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</head>
<body>
<?php
if($_SESSION['logedIn'] != "true") {
header("Location: ../index.php");
exit();
}
?>
<div class="container-fluid z-container">
<div class="row z-overview-top-menu">
<div class="col-md-2 z-background-dark z-top-nav z-border-right" id="menu_logo">
<h3>Logo</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover z-border-right z-selected" id="menu_diary" onclick="changeMenu('diary')">
<h3>Dagboek</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_pazo" onclick="changeMenu('pazo')">
<h3>Pazo</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_counter" onclick="changeMenu('counter')">
<h3>Tellers</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_overview" onclick="changeMenu('overview')">
<h3>Overzicht</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover" id="menu_comparison" onclick="changeMenu('comparison')">
<h3>Vergelijking</h3>
</div>
</div>
<div class="row">
<div class="col-md-2 z-overview-left-menu">
<ul class="z-left-list" id="userList"></ul>
</div>
<div class="col-md-10 z-overview-main-menu">
<ul id="overviewList" class="z-overview-list-main">
</ul>
</div>
</div>
</div>
</body>
When I replace the header with an echo, I get a response, indicating that the header is indeed executing when I run the code.
Also there are no .htaccess in any of the directories.
Any thoughts on what the problem might be?
http://php.net/manual/en/function.header.php
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Since you are outputting:
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://jquery-ui.googlecode.com/svn-history/r3982/trunk/ui/i18n/jquery.ui.datepicker-nl.js"></script>
<script src="../javascript/overview.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/mainpage.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</head>
<body>
before the header call, it will fail.
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
Just to give you a short background, I am working with a referral program and the main feature of it is referring someone.
In my website. I have registration page, login page, successful registration page, referral page and referral list.
For example, if you are a user, you have to register through registration page. If you successfully register on my page, the next page will be the successful registration page. There is a button there to go to the referral page to refer someone.
The problem I encounter is, I register as a new user. So definitely, I filled up all necessary information to registration page, after I successfully register, the next page after that is showing the successful registration page and in that page there is a button that once you click it, it will go to referral form page.
For example, I will now refer someone, so I filled up all information, so when I tried to submit my referral there is an error that prompt at my page
Can anyone tell me which line of my code is triggering this error:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/thecommissioner/public_html/crm/include/Webservices/Utils.php:880)
in
/home/thecommissioner/public_html/crm/modules/Webforms/capture_old.php
on line 97
but the referral added on my referral list, so when I tried to refer again, this error won't show anymore... even if I refer several times, this error didn't show anymore. It just shows on the first referral try of a new created account. I don't know what the problem is.
This just shows on my first try to referring somebody, but after that, when I tried to refer more, this wont show anymore and I already added my referral. Can someone help me to figure this out?
I know there are several lessons there and I've already read different posts in stackoverflow about this and I'm still vague about it. And would you mind telling me what new code I can use instead of my current code? I cannot test if my page is working because of this.
Here's my code:
<!DOCTYPE html>
<?php
include('../include/dbconnection.php');
include('../include/functions.php');
if(!isset($_SESSION))
{
session_start();
}
$empid = $_SESSION['SESS_EMP_ID'];
$conid = $_SESSION['SESS_CONID'];
$fName = $_SESSION['SESS_FIRSTNAME'];
$lName = $_SESSION['SESS_LASTNAME'];
$contactNo = $_SESSION['SESS_CONTACT_NO'];
$mobile = $_SESSION['SESS_MOBILE'];
$email = $_SESSION['SESS_EMAIL'];
$bday = $_SESSION['SESS_BDAY'];
if($conid == '')
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='index.php';
</SCRIPT>");
}
else
{
//Nothing
}
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../css/normalize.css">
<link rel="stylesheet" href="../css/skeleton.css">
<link rel="stylesheet" href="../css/successReg_style.css">
<link rel="icon" type="image/png" href="../images/cvglogo.png">
<link rel="short icon" href="../images/favicon.ico">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript"></script>
<link rel="stylesheet" href="../css/animate.css">
</head>
<body>
<!--Here the Header goes-->
<?php include('include/logoheader.php'); ?>
<!-- Overall Contect for Responsive Purposes-->
<div class="allcontent">
<div class="row">
<div class="twelve columns" style="margin-top:0%;">
<!-- Here the Main Content goes-->
<div="maincontent">
<center>
<h3>Congratulations and Welcome to the Circle!</h3>
<br>
<div class=" animated zoomIn socialmedia">
<ul>
<li>
<img class="logos" src="../images/tick.png">
</li>
</ul>
</div>
<!-- This is the welcome Introduction -->
<div class="welcome">
<p>Now that you’re part of the most awesome group on the planet, why not share the glory with your friends. There’s no happier
team
player than a team player with his friends.</p>
<span class="hit">Don’t be shy. Hit the button. Refer your friend!</span><br>
<!-- This is Just a GIF arrow down -->
<div class="demo-wrapper">
<div class="html5-dialog">
<div class="gif">
<img src="../images/scroll-down.gif">
</div>
<?php include('GlobalConstant.php'); ?>
<!-- But here goes the button for "I have someone in mind" -->
<a href="<?php echo employee_refer; ?>"><button class="navbutton">I have someone in mind</button>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- Here the footer goes-->
<center><?php include("include/footer.php"); ?></center>
</body>
</html>
The error:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/thecommissioner/public_html/crm/include/Webservices/Utils.php:880)
in
/home/thecommissioner/public_html/crm/modules/Webforms/capture_old.php
on line 97
just shows on my first attempt of referring then after that, when I referred more, the above warning/error didn't show again.
If those warning just shows on my first attempt of referring, why it doesn't show the next time I referring again? It just shows on my first try.
You need to send header before ANY output is sent. That means before that HTML, on top of page.
OR, as RamRider suggested, use output buffering.
Put
ob_start()
On very top of page and
ob_end_clean()
On very bottom
You need to put the session_start() function on top your code. Otherwise sometimes php will throw the exception headers already sent.
<?php
session_start();
include('../include/dbconnection.php');
include('../include/functions.php');
$empid = $_SESSION['SESS_EMP_ID'];
$conid = $_SESSION['SESS_CONID'];
$fName = $_SESSION['SESS_FIRSTNAME'];
$lName = $_SESSION['SESS_LASTNAME'];
$contactNo = $_SESSION['SESS_CONTACT_NO'];
$mobile = $_SESSION['SESS_MOBILE'];
$email = $_SESSION['SESS_EMAIL'];
$bday = $_SESSION['SESS_BDAY'];
if($conid == '')
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='index.php';
</SCRIPT>");
}
else
{
//Nothing
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../css/normalize.css">
<link rel="stylesheet" href="../css/skeleton.css">
<link rel="stylesheet" href="../css/successReg_style.css">
<link rel="icon" type="image/png" href="../images/cvglogo.png">
<link rel="short icon" href="../images/favicon.ico">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript"></script>
<link rel="stylesheet" href="../css/animate.css">
</head>
<body>
<!--Here the Header goes-->
<?php include('include/logoheader.php'); ?>
<!-- Overall Contect for Responsive Purposes-->
<div class="allcontent">
<div class="row">
<div class="twelve columns" style="margin-top:0%;">
<!-- Here the Main Content goes-->
<div="maincontent">
<center>
<h3>Congratulations and Welcome to the Circle!</h3>
<br>
<div class=" animated zoomIn socialmedia">
<ul>
<li>
<img class="logos" src="../images/tick.png">
</li>
</ul>
</div>
<!-- This is the welcome Introduction -->
<div class="welcome">
<p>Now that you’re part of the most awesome group on the planet, why not share the glory with your friends. There’s no happier
team
player than a team player with his friends.</p>
<span class="hit">Don’t be shy. Hit the button. Refer your friend!</span><br>
<!-- This is Just a GIF arrow down -->
<div class="demo-wrapper">
<div class="html5-dialog">
<div class="gif">
<img src="../images/scroll-down.gif">
</div>
<?php include('GlobalConstant.php'); ?>
<!-- But here goes the button for "I have someone in mind" -->
<a href="<?php echo employee_refer; ?>"><button class="navbutton">I have someone in mind</button>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- Here the footer goes-->
<center><?php include("include/footer.php"); ?></center>
</body>
</html>
If you are still experiencing the issue try ob_start() and ob_flush() .. Anyways this stackoverflow article about headers already sent is worth reading though Stackoverflow link
So I am trying to access a certain webpage within my site.
In my index, I use a isset($_GET() parameter to access another page. My code for the index.php is as follows:
<!DOCTYPE html>
<html>
<head>
<?php require ("includes/database.php"); ?>
<title>Offstreams Admin Panel</title>
<link rel="stylesheet" type="text/css" href="styles/admin_body.css" />
<link rel="stylesheet" type="text/css" href="styles/admin_header.css" />
<link rel="stylesheet" type="text/css" href="styles/admin_footer.css" />
<link rel="stylesheet" type="text/css" href="styles/admin_postspace.css" />
</head>
<body>
<header>
<!--Header Info Here -->
</header>
<div class="wrapper">
<div class="sidebar">
<ul>
<li>Post New Band</li>
</ul>
</div>
<article>
<?php
if (isset($_GET['post_new_band'])){
require ("includes/post_new_band.php");
echo "Page accessed";
} else {
echo "Page not accessible.";
}
?>
</article>
</div>
</body>
</html>
I have it echo page not available when "index.php?page" doesn't exist. But when it does exist, such as "index.php?post_new_band" (code below) nothing is posted. The sever and database work, that is for sure. MySQL isn't the problem since I am trying to get the html to work.
Code for "post_new_band.php":
<!DOCTYPE html>
<html>
<head>
<!-- Head Info Goes Here -->
</head>
<body>
<h1>Insert New Band</h1>
<form action='index.php?post_new_band' method='post'>
<b>Insert New Band</b><input type='text' name='band_name' />
<b>Insert Band Origin</b><input type='text' name='band_origin' />
<input type='submit' name='insert_band' value='Add Band' />
</form>
<?php
if (isset($_POST['post_new_band'])){
$band_name = $_POST['band_name'];
if($band_name==''){
echo "<script>alert ('Please Insert Band Name')</script>";
echo "<script>window.open('index.php?post_new_band','_self')</script>"
} else {
$insert_band_name = "insert into Band (band_name) values ('$band_name')";
$run_band_name = mysql_query("$insert_band_name");
echo "<script>alert ('New Category Added')</script>";
echo "<script>window.open('index.php?post_new_band','_self')</script>"
}
}
?>
</body>
</html>
You can only use a request key once - so dont use $_GET['post_new_band'] and $_POST['post_new_band'] in the same request. Change one of the keys.
This is pretty stupid, but it actually turned out to be a syntax error involving semi-colons. *Slap to the face.
Having a strange zend framework issue. Although it's most likely something simple, I've been unable to resolve it so far. I have scraped the bottom of stackOverflow and several other community sites, as well as the zend documentation.
Any ajaxLink that I include in my layout refuses to function. They are classed properly, with hash placeholder in href attribute, but the javascript to activate the link is not being included in the page .
echo $this->jQuery; statement in layout is also failing. Might be the cause of ajaxLink failure. I have verified that I am properly adding the jQuery view helper in my bootstrap. Have tried using both ZendX_JQuery::enableView($view); and $view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper"); methods.
Thanks in advance
Excerpts from the relevant files follow:
bootstrap.php:
protected function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('HTML4_STRICT');
$view->headMeta()->appendHttpEquiv('Content-type', 'text/html;charset=utf-8')
->appendName('description', 'Business Club');
$view->headTitle()->setSeparator(' - ');
$view->headTitle('SOU Business Club');
$view->setHelperPath(APPLICATION_PATH.'/views/helpers', '');
ZendX_JQuery::enableView($view);
}
dashboardLayout.phtml:
<?php echo $this->doctype();?>
<html>
<head>
<?php echo $this->headTitle();?>
<?php echo $this->headMeta();?>
<?php
echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/dashboardStyle.css');
echo $this->headLink()->prependStylesheet($this->baseUrl().'/js/jquery/css/smoothness/jquery-ui-1.8.11.custom.css');
?>
<?php
echo $this->headScript()->appendFile($this->baseUrl().'/js/paginator.js');
echo $this->jQuery();
?>
</head>
<body id="body">
<div id="wrapper">
<div><?php include "dashboardHeader.phtml"; ?></div>
<div id="bar">
Dashboard Home|
<?php
echo $this->ajaxLink('Club Roster | ',
'user/index',
array('update' => '#content',
'method' => 'post'),
array('format' => 'html')
);
?>
Google Docs|
Book Sale Management|
</div>
<div id="content" align="center">
<?php
echo $this->layout()->content;
?>
</div>
<div id="statusBar">
<?php
$userData = Zend_Registry::get('userData');
$name = $userData->name;
$role = $userData->role;
$status = 'Logged in as: '.' Name: '.$name.' Role: '.$role;
echo $status;
?>
<br />
Logout
</div>
<div><?php include "dashboardFooter.phtml"; ?></div>
</div>
</body>
</html>
HTML <head> output:
<head>
<title>SOU Business Club - Member Dashboard</title>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" >
<meta name="description" content="Business Club" >
<link href="/css/dashboardStyle.css" media="screen" rel="stylesheet" type="text/css" >
<link href="/js/jquery/css/smoothness/jquery-ui-1.8.11.custom.css" media="screen" rel="stylesheet" type="text/css" >
<link href="/css/dashboardStyle.css" media="screen" rel="stylesheet" type="text/css" >
<script type="text/javascript" src="/js/paginator.js"></script>
</head>
In your bootstrap you need to specify where jQuery is, either on your local machine or a CDN. Locally you use:
$view->jQuery()->setLocalPath(PATH);
Or you could use a CDN such as Google's: http://code.google.com/apis/libraries/devguide.html#jquery