This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
Sorry I read some topics about this already..
But it didn't worked or make it clear to me.
I want to make sure that when my MySQL says that maintenance = 1 he redirect with header to maintenance.
In maintenance the same thing happens but then when maintenance = 0 he redirect with header back to the index.
This works fine..
BUT now I want to have an IP whitelist so that when its in maintenance and I visit the page I still will be able to see the website it self.. But this isn't working correctly..
I hope someone could help me and get an light on what I'm doing wrong.
The code of checkmaintenance.php:
<head><link rel="stylesheet" type="text/css" href="../css/style.css"></head>
<?php include('config-connection.php'); ?>
<?php
$whitelist = mysql_query("SELECT * FROM whitelist_ip");
while ($ip = mysql_fetch_assoc($whitelist)) {
$checkip = $ip['ip'];
}
?>
<?php
if($checkip == $_SERVER['REMOTE_ADDR']) {
echo "<center><div class='error'>The website is in maintenance. But your IP has been whitelisted so you can see the website!</div></center>";
}
?>
<?php
if($maintenance == 1){
flush(); // Flush the buffer
ob_flush();
header('Location: maintenance.php');
}else{
echo "";
}
?>
I Know that I'm a noob with PHP and I try to learn and understand it better than I do now..
To make clear what is in maintenance it self I post the part of that code beneath this:
<?php include('config/config-connection.php'); ?>
<?php
$whitelist = mysql_query("SELECT * FROM whitelist_ip");
while ($ip = mysql_fetch_assoc($whitelist)) {
$checkip = $ip['ip'];
}
?>
<?php
if($checkip == $_SERVER['REMOTE_ADDR']) {
flush(); // Flush the buffer
ob_flush();
header('Location: index.php');
}
?>
<?php
if($maintenance == 0){
flush(); // Flush the buffer
ob_flush();
header('Location: index.php');
}else{
echo "";
}
?>
Index only contains this line:
<?php include('config/checkmaintenance.php'); ?>
I would be very happy if someone could explain to me why it doesn't work.
Or how I should do this better than it currently is.
Kind regards,
Brian
You cant't echo text before the header() function!
Try somehting like
echo '<meta http-equiv="refresh" content="0; URL=YOUR URL">';
Related
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()
I am trying to display an error message when there is a username-password mismatch. I set a php session variable if username and password dont match. Then i header back to the same page, with an if conditioned php statement to display an error if the variable is set. But when i unset the variable after error display, there is no error displayed on the page.
I have seen similar problems mentioned in this forum. But i seem to be doing everything right as suggested in questions.. Please help me out...
This is part of my code flow...
<?php
ob_start();
session_start();
.
.
if ($result = $sth->fetch(PDO::FETCH_ASSOC)){
$_SESSION['admin_user'] = $result['id'];
header('Location: admin_user.php');
} else {
$_SESSION['user_found'] = 0;
header('Location: index.php');
}
.
.
//in html body
<?php
if (isset($_SESSION['user_found'])){
if($_SESSION['user_found'] == 0){
?>
<div>
<p class = "bg-danger text-danger">Username Password Mismatch</p>
</div>
<?php
unset($_SESSION['user_found']);
}
}
?>
Now, if unset is removed..it works fine. If it is there, there is no display of error message.
Try not reloading the same page.. remove the header redirect.
if ($result = $sth->fetch(PDO::FETCH_ASSOC)){
$_SESSION['admin_user'] = $result['id'];
header('Location: admin_user.php');
} else {
$_SESSION['user_found'] = 0;
//header('Location: index.php');
}
When I tried the your code, things seem to work fine. Something should be wrong with the code you've not mentioned here..
To troubleshoot the problem instead of
unset($_SESSION['user_found']);
try changing the value of the variable.. say
$_SESSION['user_found'] = -1;
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 trying to make a redirect to page based on its referrer. the layout is like
<?php
$URL_REF = $_SERVER['HTTP_REFERER'];
if($URL_REF == "url of my site") {
header("Location: url of another site");
exit();
}
else {
header("Location: my home page");
exit();
}
?>
I am getting the error : Cannot modify header information - headers already sent by.
I have to modify my php.ini to output_buffering = On but I can't do this as I have a shared hosting account in hostgator.
can anyone suggest me what options now I have? can I do it by changing my code? If yes, than what changes?
<?php
function custom_redirect() {
$URL_REF = $_SERVER['HTTP_REFERER'];
if($URL_REF == "url of my site") {
header("Location: url of another site");
exit();
}
else {
header("Location: my home page");
exit();
}
}
add_action('init','custom_redirect');
?>
Try hooking your function to INIT HOOK
Hi actually header("Location: url of another site"); will not support in wordpress. Instead of this you can use
<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>
and for home page just use <?php wp_redirect( home_url() ); exit; ?>
this will solve your problem of redirection.
try using javascript instead
<?php
$URL_REF = $_SERVER['HTTP_REFERER'];
if($URL_REF == "url of my site") {
?>
<script type="text/javascript">window.location = "http://www.yoururl.com";</script>
<?php
}
else {
?>
<script type="text/javascript">window.location = "http://www.anotherlocation.com";</script>
<?php
}
?>
check the reference here
Header Information already sent is a wordpress's common error, like as you mentioned you want to redirect the web site then you have to options to follow.
Create a wordpress Template file do not call header in that file
<?php /* Template Name:Redirect*/ ?> <?php //get_header();?> <!-- Do not un comment this function -->Write your code here Write your code here Write your code here
Write your code here <?php get_footer();?>
Or you can redirect with java script's window.location function
<script>document.write(location.http://google.com);
</script>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
Greets All...
I am new to PHP and i am trying to make a login page .the problem which i am facing is the
header in If condition gives me error and so i cant redirect to the next page if $reg and $password matches with that in database.
secondly do you think that its the best way which i am doing in this code?
sorry for my bad english
Here is my code..
<?php
$reg=$_POST["reg"];
$password=$_POST["pwrd"];
$result=mysql_query("Select * from student where reg='$reg' and password='$password'");
if(!$result){
die('errrrrrrrrrrrrror'.mysql_error());
}
while ($row = mysql_fetch_array($result)) {
$db_reg=$row['reg'];
$db_password=$row['password'];
}
if(isset($_POST['login'])){
if($reg!=$db_reg || $password!=$db_password){
echo 'login failed';
}
//else if($reg=="" || $password!=""){
// echo 'empty';
//}
else if($reg==$db_reg && $password==$db_password){
header("Location:welcome.php");
//echo 'weclome here'." ".$reg;
}
}
?>
you can't echo after sending headers out.
move the welcome msg to welcome.php and exit after header function
header("Location: welcome.php");
exit();