PHP header issue within my file [duplicate] - php

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.

Related

Redirect and header aren't working [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 11 months ago.
The code below shows me "111" and "222". But doesn't redirect me anywhere. What can I do now?
<?php
echo "111";
header("Location: http://www.google.com");
echo "222";
return Redirect::to('http://www.google.com');
echo "333";
?>
Two things ...
You need to put exit after your header("Location: ... ")
Example:
header("Location: http://www.google.com");
exit;
But more importantly ... your header won't redirect if you've already written to the output buffer with echo.
You can't have any output before the header tag is set, so remove the echo statement and exit the script after the header:
header("Location: http://www.google.com");
exit;
It's not working because you've already sent data to the client (the echo). Either send your headers before you echo anything, or buffer it like this:
ob_start();
echo "111"; //Note: You're missing a semicolon here
header("Location: http://www.google.com");
echo "222";
$obj = Redirect::to('http://www.google.com');
echo "333";
ob_flush();
return $obj;
To Continue Reading...
Hope this helps!

PHP redirect with URL variables

I'm very new to php and trying to build a redirect which uses parameters from a URL which will differ from person to person.
The URL a person will access looks like: www.website1.com/redirect.php?p=p123&r=4&s=567
The p, r, and s will change for each person
I then want to redirect them to some other site that looks like this:
www.website2.com/p123.aspx?r=4&s=567
Here is what I have so far, but it's giving me an error "Cannot modify header information - headers already sent by ..."
<html>
<?php
$fpvalue = $_GET['p'];
$frvalue = $_GET['r'];
$fsvalue = $_GET['s'];
header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);
?>
</html>
I would really appreciate the help for a beginner.
Thanks!
You can't do a redirection when you have been sent a "content" (text, html, space, wherever).
You should NOT do this before calling the header() function.
As you can see, you have a "" before calling the header() function.
Change that:
<html>
<?php
$fpvalue = $_GET['p'];
$frvalue = $_GET['r'];
$fsvalue = $_GET['s'];
header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);
?>
</html>
For that:
<?php
$fpvalue = $_GET['p'];
$frvalue = $_GET['r'];
$fsvalue = $_GET['s'];
header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);
exit;
?>
<html>
</html>
And remember: Check if there is another previous space or "new line" before the "< ?php " tag.
The error "Cannot modify header information - headers already sent by ..." caused when you place session_start or php header below other codes, e.g. html
then you should change into:
<?php
//your php codes
//....
?>
<!DOCTYPE html>
....etc.
This must work
This instructions works for me to fix seo redirects, but you use for all
example url: http://yourdomain.com/index2.php?area=xpto
example index2.php file:
<?php
$fpvalue = $_GET['area'];
if ($fpvalue == 'xpto') {
header("Location: http://newurl.com/", true, 301);
}
else if ($fpvalue == 'loren') {
header("Location: http://otherurl.com/", true, 301);
}
else if ($fpvalue == '') {
header("Location: http://otherurl.com/", true, 301);
}?>

Warning: Cannot modify header information. Any ideas? [duplicate]

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']));
}
?>

Cannot modify header information...again [duplicate]

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">';

on localhost redirect works on hostgator error Cannot modify header information (php) [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
I've this code:
<?php
//Search logic for Holiday.GE
$search_variables = $_GET['s']. '+' .$_GET['cat'];
$var_lang_now = $_GET['lang_current_search'];
if ($var_lang_now == ru OR $var_lang_now == en) {
$refering_page = 'http://dev.holiday.ge/' . $var_lang_now . "/";
} else {
$refering_page = 'http://dev.holiday.ge';
}
//This sends http post to url without curl
header("Status: 301 Moved Permanently");
header("Location:$refering_page?s=$search_variables");
exit;
?>
on localhost where I was testing it worked fine. But on live server it errors out with:
Warning: Cannot modify header information - headers already sent by (output started at /home/sandrodz/public_html/devholidayge/wp-content/themes/sweetholiday/searchlogic.php:3) in /home/sandrodz/public_html/devholidayge/wp-content/themes/sweetholiday/searchlogic.php on line 15
Warning: Cannot modify header information - headers already sent by (output started at /home/sandrodz/public_html/devholidayge/wp-content/themes/sweetholiday/searchlogic.php:3) in /home/sandrodz/public_html/devholidayge/wp-content/themes/sweetholiday/searchlogic.php on line 16
15 and 16 are lines before exit;
I tried as suggested in answers, but I get same error!
<?php
ob_start();
//Search logic for Holiday.GE
$search_variables = $_GET['s']. '+' .$_GET['cat'];
$var_lang_now = $_GET['lang_current_search'];
if ($var_lang_now == ru OR $var_lang_now == en) {
$refering_page = 'http://dev.holiday.ge/' . $var_lang_now . "/";
} else {
$refering_page = 'http://dev.holiday.ge';
}
//This sends http post to url without curl
header("Status: 301 Moved Permanently");
header("Location:$refering_page?s=$search_variables");
exit;
ob_end_flush();
?>
Put ob_start(); in beginning, ob_end_flush(); at end
use this at start of script
ob_start();
and this at end
ob_end_flush();
it will solve this problem. Some time ago I face same problem after inserting these everything is fine.
header("Location: $refering_page?s=$search_variables");
^ //space should present.
Please Use this code top of your page:
<?php ob_start(); ?>
and below code Bottom of your page
<?php ob_flush(); ?>

Categories