This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
I am getting the following error:
Warning: Cannot modify header information - headers already sent by..
Apparently the output is started from line 3 and cannot modify from line 11.
<html>
<body>
<?php
use google\appengine\api\users\User;
use google\appengine\api\users\UserService;
$user = UserService::getCurrentUser();
if ($user) {
echo 'Hello, ' . htmlspecialchars($user->getNickname());
}
else {
header('Location: ' . UserService::createLoginURL($_SERVER['REQUEST_URI']));
}
?>
Any ideas how to solve this?
You can't have any output before setting headers. In Your case <html> and <body> tags are already sent when you are trying to set
header('Location: ' . UserService::createLoginURL($_SERVER['REQUEST_URI']));
Your code should be something like:
<?php
use google\appengine\api\users\User;
use google\appengine\api\users\UserService;
$user = UserService::getCurrentUser();
if ($user) {
echo '<html>';
echo '<body>';
echo 'Hello, ' . htmlspecialchars($user->getNickname());
}
else {
header('Location: ' . UserService::createLoginURL($_SERVER['REQUEST_URI']));
}
?>
Related
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 11 months ago.
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp64\www\proj\index.php:2) in C:\wamp64\www\proj\menu\edit.php on line 8
index.php
<?php include_once "includes/functions.php";?>
<!DOCTYPE html>
<html>
<head>
edit.php
$id = $_GET['id'];
$menu = getmenu($id);
if (isset($_POST['btn']))
{
$data = $_POST['frm'];
edit_menu($data,$id);
header("location:index.php?m=menu&p=list");
}
Can Anybody Help Me With This?
BTW the database is updating and there is no problem with that.
I Found This Function Online, It Helped Me!
function safe_redirect($url, $exit=true) {
// Only use the header redirection if headers are not already sent
if (!headers_sent()){
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $url);
// Optional workaround for an IE bug (thanks Olav)
header("Connection: close");
}
// HTML/JS Fallback:
// If the header redirection did not work, try to use various methods other methods
print '<html>';
print '<head><title>Redirecting you...</title>';
print '<meta http-equiv="Refresh" content="0;url='.$url.'" />';
print '</head>';
print '<body onload="location.replace(\''.$url.'\')">';
// If the javascript and meta redirect did not work,
// the user can still click this link
print 'You should be redirected to this URL:<br />';
print "$url<br /><br />";
print 'If you are not, please click on the link above.<br />';
print '</body>';
print '</html>';
// Stop the script here (optional)
if ($exit) exit;
}
src : https://www.jonasjohn.de/snippets/php/secure-redirect.htm#:~:text=This%20little%20function%20ensures%20that,link%20to%20the%20new%20URL.
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 5 years ago.
I am experiencing some issues with php, on my localhost. I receive a err_too_many_redirects error from my browser.
I am recieving this error when I am trying to direct a user from accessing information from a different place on the web server.
Here is the code:
<?php
session_start();
if(isset($_SESSION['valid'])) {
$loggedIn = $_SESSION['name'];
if($loggedIn != basename(getcwd())) {
echo "You are in the wrong place.";
$url = '../../users/' . $loggedIn . '/index.php';
header('Location: ' . $url);
} else {
echo "Hello";
}
} else {
$url = 'index.php';
header('Location: ' . $url);
}
?>
Can someone please explain to me why it is doing this? I have tried multiple things such as re-arranging the order of the process.
Can someone please help me?
My bad, silly mistake.
I was re-directing them straight back to the INDEX page of the user
Code:
<?php
session_start();
if(isset($_SESSION['valid'])) {
$loggedIn = $_SESSION['name'];
if($loggedIn != basename(getcwd())) {
echo "You are in the wrong place.";
$url = '../../users/' . $loggedIn . '/index.php';
header('Location: ' . $url);
} else {
echo "Hello";
}
} else {
$url = 'index.php';
header('Location: ' . $url);
}
?>
I had to update the URL here in the else statement for the if(isset($_SESSION['valid]')) Here it is:
$url = '../../index.php';
header('Location: ' . $url);
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
PHP Header issue with ob_start() and ob_end_flush()
(5 answers)
Closed 7 years ago.
this is warning. even it is NOT allowing me to open the page admin.php and error is ":
Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\rail\index.php:446) in
<?php
ob_start();
error_reporting(E_ALL);
here is warning in this line . where i have started the session
session_start();
include("db.php");
if(isset($_POST['log']))
{
$user= $_POST['username'];
$pass= md5($_POST['password']);
$sql=mysql_query( "select * from reg where username= '$user' AND password='$pass' ") or die( mysql_error());
$data=mysql_num_rows($sql);
if ($data == 1) {
while($row = mysql_fetch_array($sql)){
$_SESSION['username']=$s1;
echo '<script>window.location="admin.php"</script>';
}
}
else {
echo '<script type="text/javascript">';
echo 'alert("Password Invalid!")';
echo '</script>';
}
}
ob_end_flush();
?>
enter image description here
In a nut shell. There are many things which could cause this error/warning. Most of which are narrowed down to a piece of output being sent to the browser before starting a session. So with that in mind, check for output in your script.. this can be anything from a simple white space or text before initialising the session.
another common scenario for this warning to be presented is triggered by your files encoding. There can be something called UTF8-BOM which is a hidden character (s) which some editors might not pick up, providing you have set the wrong encoding for yor file. So in that case, the solution would be to double check the files encoding.
The error message is clear, it states index.PHP and around line 446 is roughly the cause for this warning. So check for things I've mentioned above on or before that line
Try to use this may help you ,
<?php
session_start();
ob_start();
error_reporting(E_ALL);
include("db.php");
include("redirect.php"); // the file which is stored redirect()
if(isset($_POST['log']))
{
$user= $_POST['username'];
$pass= md5($_POST['password']);
$sql=mysql_query( "select * from reg where username= '$user' AND password='$pass' ") or die( mysql_error());
$data=mysql_num_rows($sql);
if ($data == 1) {
while($row = mysql_fetch_array($sql)){
$_SESSION['username']=$s1;
redirect('admin.php'); // HERE YOU NEED TO REPLACE THE FUNCTION
}
}
else {
echo '<script type="text/javascript">';
echo 'alert("Password Invalid!")';
echo '</script>';
}
}
ob_end_flush();
?>
Avoid the ?> if there is not any html tags in your code
EDIT :
If the above did not helped you then try use the below function instead of header()
function redirect($url) {
if (!headers_sent()) {
header('Location: '.$url);
exit;
} else {
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
Thanks to Rajat Singhal for the redirect()
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I have this problem being reported in one of my files;
Warning: Cannot modify header information - headers already sent by (output started at /home/*/public_html/addengine/parseclick.php:1) in /home/*/public_html/addengine/parseclick.php on line 40
Here is the code of the actual file;
<?php
include_once("../admin/database.php");
$today = date("Y-m-d");
$data = str_replace(' ', '+', $_GET['data']);
$de_data = decrypt($data,$ad_passcode='',$salt='');
$arr_data = explode(":",$de_data);
/*
Array: arr_data
arr_data[0] = pub_ad_id
arr_data[1] = adv_ad_id
arr_data[2] = compaign_id
*/
$pid = $arr_data[0];
$aid = $arr_data[1];
$cid = $arr_data[2];
/*$billing = getvalue("billing_type","compains",$cid);
if($billing == 'cpc' || $billing == 'both')
{
countClick($aid,"adds",$pid);
countClick($pid,"pub_adds",$pid);
}*/
$adv_ad_id = $aid;
$pub_ad_id =$pid;
countClick($adv_ad_id,$pub_ad_id);
$parenturl = $_SERVER["HTTP_REFERER"];
if( verifyLegitimateurl($parenturl,$pid) == 'Passed' && emailAlreadyExists($_SERVER['REMOTE_ADDR'],"banips","cip") == false)
{
$url = getvalue("url","adds",$aid);
header("Location: ".$url);
}
?>
The error is because of this line:
header("Location: ".$url);
You cannot redirect if you already started to write to the page.
So if you have something like this
<!Doctype html>
<?php
header("Location: ".$url);
?>
You will get that error, you have to make sure you dont have any html above the php , or you dont echo anything.
So if you place your php on top , you wont get the error:
<?php
header("Location: ".$url);
?>
<!Doctype html>
instead of
header("Location: ".$url);
use
<META http-equiv="refresh" content="0;URL=<?php echo $url;?>">
obviously you should close the php tag before this html code and start it again after this code
Use ob_start(); after <? at the beginning of file and place ob_flush(); before ?> at the end of the file.
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I am using following code. I get errors:
Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/mc/cpanel/Source/verifylogin.php:11)
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/public_html/mv/cpanel/Source/verifylogin.php:11)
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/public_html/mv/cpanel/Source/verifylogin.php:11)
<?php
error_reporting(E_ALL^ E_NOTICE);
ob_start();
require("../Lib/dbaccess.php");
$inQuery = "SELECT mhuserid, mhusername FROM cpanelusers WHERE mhusername = '". $_POST['UserName'] ."' AND mhpassword = '". hash('sha512', $_POST['Password']) ."'";
try
{
$Result = dbaccess::GetRows($inQuery);
echo $Result;
$NumRows = mysql_num_rows($Result);
if ($NumRows > 0)
{
header("Location: http://www.example.com/cpanel/mainwindow.php");
session_start();
}
else
{
header("Location: http://www.example.com/cpanel/");
echo "Last login attempt failed.";
exit;
}
}
catch(exception $e)
{
}
ob_clean();
?>
I did all the changes and reduced the code like below, still it is not working.
<?php
ob_end_clean();
error_reporting(E_ALL^ E_NOTICE);
require("../Lib/dbaccess.php");
ob_start();
$inQuery="SELECT mhuserid, mhusername FROM cpanelusers WHERE mhusername = '".$_POST['UserName']."' AND mhpassword = '".hash('sha512', $_POST['Password'])."'";
try
{
$Result=dbaccess::GetRows($inQuery);
$NumRows=mysql_num_rows($Result);
if ($NumRows>0)
{
header("mainwindow.php");
}
else
{
header("index.html");
}
}
catch(exception $e)
{
}
?>
Both session_start() and header() requires that nothing have been written to the page. You are using echo on row 11, thus writing things to the page.
For some reason your ob_start doesn't seem to work. Maybe output buffering is disabled in your PHP configuration?
Figured out the problem. It was the "require..." statement that generated errors. I added the connection string and the function to execute the query in the same file.