I have this code as shown bellow:
<?php
session_start();
require_once 'classes/cookie.class.php';
if(!isset($_SESSION['logged']) && !Cookie::checkCookie($_COOKIE['smt_auth']))
{
header('Location: login.php');
exit;
}
?>
It's working right in localhost, as it redirects always to login.php if the user is not logged, neither has a cookie. But the problem comes when I try to put these files in the host, it stops working.
The two files using this are index.php and temario.php, when I try to introduce the url of index.php it redirects me to login.php but, in temario.php, the screen just gets blank and doesn't redirect.
Both files have the same PHP block (there are no white spaces as I copy&pasted from index.php to temario.php), so does somebody know why this could not be working in the host, but it does on localhost with xampp?
It's throwing the error:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /homepages/3/d507876316/htdocs/new/usuario/temario.php:1) in /homepages/3/d507876316/htdocs/new/usuario/temario.php on line 4
Also if I make a copy of index.php and rename it, it also works as intended.
Thanks in advance.
You need to session_start a cookie set before any body content sent to browser. So check if you have any echo, plain html or even whitespace before your code block. Also check for whitespaces before and after php tags. You should omit php end tags on end of php files.
Try after putting, <?php ob_start(); ?> at the beginning of code and <?php ob_flush(); ?> at the end of the page where you are writing header redirection code.
Related
I developed a website from a developer. My website was working fine. All of a sudden I discovered that whenever I click on Contact Us link, I get an error
"Warning: session_start() Cannot send session cache limiter - heards
already sent (output started at /home/xxx/public_html/contact.php:1)
in /home/xxx/public_html/connection/config.php on line 2"
This was not happenning before. The developer is not giving me support and is ignoring my emails. So I searched and researched for it a lot on the Internet and found that there is something wrong in the way session_start() is defined.
Here is the code of contact.php
<?php
include("connection/config.php");
session_start();
?>
<?php
if(isset($_POST['enquiry2']))
{
.
.
.
}
?>
<!DOCTYPE html PUBLIC
.
.
.
<?php include "footer.php"; ?>
</html>
Code of config.php
<?php
session_start();
error_reporting(0);
if ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == '192.168.1.111')
{
$con=
.
.
}
else
{
$con=
.
.
}
error_reporting(E_NOTICE ^ E_WARNING ^ E_ALL);
require_once("logic/class.config.php");
.
.
<?php date_default_timezone_set("Asia/Kolkata");?>
In lot of articles I read that session_start() should be right at the top of the page so I reversed the second and third line of contact.php but it gave me same error pointing to contact.php file.
"Warning: session_start() Cannot send session cache limiter - heards
already sent (output started at /home/xxx/public_html/contact.php:1)
in /home/xxx/public_html/connection/contact.php on line 2"
Earlier it was pointing to config.php fle
Same thing is happening in registration page. I have three files register.php, register1.php and register2.php. The same error occurs only on register3.php when it loads the page and once again when submit button is clicked.
I request if someone can help me identify the problem.
Thanks.
EDIT
As suggested by Premalatha:
Removed session_start(); from contact.php -> but same warning.
Placed session_start(); on top of include("connection/config.php"); -> but same warning.
As suggested by Alok:
I dont know how to check if I am sending content or calling header somewhere before session_start(). The session_start() line is either on top or in the second line with first line being include("connection/config.php"); that I tried interchanging upside down with no help.
One thing I fail to understand that other .php files also have the same coding then why this warning comes only on contact.php? Similarly, register1.php, register2.php also have same coding but why the warning comes only while loading and unloading register3.php. All was working before, this started all of a sudden and I dont remember doing any changes in any of these files so the issue may be something else. I also wanted to try suggestion from wangpeng but I could not find the php.ini file anywhere in cpanel. Moreover, I did not figure out how to check if the file's code if it is UTF8-BOM. Also where exactly should i put OB_START() because i am unable to identify where is the header.
EDIT2
By the way, this warning does not affect the functioning of the site. The error just appears and then disappears. However, it disturbs the css, all fonts and images become bigger and the look of the page goes for a toss. Can someone tell me how to ignore this error so that the system doesn't show it at all?
session_start();
is already included in config.php file.
You can remove in contact.php file and it would start working without any issues.
If you still face any issues then you can place session_start(); line of code in contact.php above the include statement.
1.php.ini session.auto_start = 0 to session.auto_start = 1 .
2.check the file's code is UTF8-BOM? yes,UTF8-BOM to UTF8.
3.OB_START();before header
Headers already sent means PHP script sent the headers to the server already, and it cannot be modified now.
Which means before session_start() your script must be sending content or calling header to the server and and after sending it script is calling session_start() function which tries to modify the headers.
Solution in your case is to check you're not sending content or calling header some where before session_start() in both the scripts.
Update:
Try this,
Locate all the *.php files pointed in contact.php and find session_start(); in it.
Replace it with the following,
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
Above code will make sure that you don't start the session again if it is already being started.
I never thought the solution would be this !
However, when I read same thing from four different people, I had to try this and it worked ! I had few leading, trailing and blank spaces in my include files. I got rid of them and my problem was solved.
Here is the article that helped me a lot
Troubleshooting "Warning: session_start(): Cannot send session cache limiter - headers already sent"
if you have blank space after php tag (or html or any output) in your include files then this error will come. -by Imdad
The reason for getting this issue is 99% likely to be trailing spaces at the end of your include files (yes - I know it sounds unlikely but just try it). Remove the trailing spaces, newlines etc... and all will be well. -by Chris B
a white-space before <?php also send for output and arise error. Conclusion: Do not output any character before calling session_start() or header() functions not even a white-space or new-line -by Biswadeep Sarkar
For others who may run across this - it can also occur if someone carelessly leaves trailing spaces from a php include file. Example:
<?php
require_once('mylib.php');
session_start();
?>
In the case above, if the mylib.php has blank spaces after its closing ?> tag, this will cause an error. This obviously can get annoying if you've included/required many files. Luckily the error tells you which file is offending. -by Ross
After login I'm trying to redirect the user based on their user role to their specified page. I tried redirect and header but both is not working for me in the server.
I tried,
$this->redirect('dashboard/profile');
$this->redirect(array('dashboard/profile'));
header('Location: dashboard/profile');
I'm getting this error
Cannot modify header information - headers already sent by (output started at /home/deheal/public_html/dev/index.php:2)
This all working perfectly in the local but not in the server please help me.
Thanks in advance.
If you want to use redirection somewhere in the middle of the page, you need to make sure there is no output to the browser before the redirection. You can use ob_start() function to buffer the output before sending it to browser so that headers are not sent before you do the redirection
ob_start() .....starts buffering the output
ob_flush()......flushes out the output that was buffered
Use / before URL
$this->redirect('/dashboard/profile');
You cannot use header() once text has been output to the browser. As your header.php include presumably outputs HTML, header() cannot be used.
You can solve this in a couple ways:
Move the if statement above the header include (this won't work, as you've indicated in comments that header.php sets the uid session and other vital stuff).
Call ob_start() at the top of the script to buffer the output.
As per your updated question you can use window.location instead of header("location:URL")
<?php
//Php code here
?>
<script type="text/javascript">
window.location= 'dashboard/profile';
</script>
<?php
//Php code here
?>
You can perform redirection by javascript, Because you cannot modify header after rendering. But this function can do that. Just call it and you will be redirected to page you want to.
<?php
function redirect_url($url)
{
?>
<script>
document.title="Loading";
document.getElementsByTagName("body")[0].innerHTML="Please Wait.....";
document.getElementsByTagName("body")[0].innerHTML+='<img src="../../images/loading_2.gif" style="height:20px;width:20px;"/>';
window.location.href="<?php
echo $url;
?>";
</script>
<?php
}
?>
I had also same problem before. It was showing an header error on my controller.
Copy all your codes in your controller, and create new file, paste
your code and save with same name.
It must help.
I have 2 pages one is index.php other is classes.php
should not this work classes.php:
<?php
ob_start();
session_start();
?>
index.php:
<?php
include "classes.php";
error_reporting(E_ALL);
echo "Loaded";
?>
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /public_html/index.php:1) in /public_html/includes/classes.php on line 2
Ensure there is no white space before the opening <?php tag in your classes and index files.
Make sure you do not send anything before anything regarding session is executed.
That means:
remove everything before <?php,
remove trailing ?> tags (they are not required and often harmful),
The problem you experienced is caused by something that is before "<?php" within index.php file (it may be a space, tab, end of line or anything else you did not show us), but if you follow the above rules, you should have this issue a lot less often during further development of your application.
It's not an include issue - you have whitespace somewhere, possibly in classes.php after the ?>.
output started at /public_html/index.php:1
Apparently you have white-space (or possibly content) before the PHP start tag in your index.php file which is being sent as output.
check your php.ini and see if you have the session.auto_start set to 1(or true, or yes).
If you want to start the session manually, it should be set to 0
More info here: http://www.php.net/manual/en/session.configuration.php#ini.session.auto-start
if it is already set to 0, then you have a space or a new line at the beginning of index.php, try to make sure that you do not see any weird char
I want to redirect from login page to my main page using php.
I use the following line of code: header('location:index.php');
inspite of redirection i received the error like:
Warning: Cannot modify header information -
headers already sent by (output started at C:\wamp\www\student\login.php:18)
in C:\wamp\www\student\login.php on line 19
This error occures if you print something before header() function.
For example:
<?php
echo 'test';
header('location:index.php');
exit;
?>
or even:
<html>
<head> .....
<?php
echo 'test';
header('location:index.php');
exit;
?>
You have to move this piece of PHP code before any operation that gives you an output.
You can also do the following trick but it is the second way you should try:
<?php
echo 'test';
ob_start();
flush();
header('location:index.php');
exit;
?>
you need to turn on the output buffer by inserting
ob_start();
at first line of php code
If you have already "echo'd" or "print'd" anything onto the page, either inside your script or outside of any set of PHP tags, then you cannot send any headers anymore. This is what your error message is stating.
Also, you should (try) to use full paths in location tags, it's better for SEO to use full URLs for every link on your website, let alone the redirects.
make sure that the header function is called before any response is outputted, e.g. header() function must be called before any echo functions or print_r, try removing the spaces before the <?php opening tag.
Its very difficult to decide what constitutes output to a page. I tried to eliminate my problem by removing all "echo's", "prints" etc but couldnt make the redirection work. I think there was a problem with a returned sql query. Adding the buffer and flushing it cured the problem.
You need to find out whether the header was sent already sent by checking line by line with header_sent() it will return true or false. If it's already sent you can't use header(). Try meta http-equiv="refresh" content="0;URL='your url'" /.Don't forget to add open and closing tags.<>
I can't handle this error, please help me. It worked on my laptop but did not work on my desktop.
Why?
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at F:\xampp\htdocs\etest\index.php:1) in F:\xampp\htdocs\etest\common\header.php on line 3
The code:
<?php
ob_start();
session_start();
include("constants.php");
include("includes.php");
?>
Thanks for your kindness!
the error was at F:\xampp\htdocs\etest\common\header.php on line 3 but output was already started at F:\xampp\htdocs\etest\index.php:1
I assume you posted the header.php, but your index.php either has whitespace before the
<?php include() or outputs something other than a header on the first line.
the error is caused by line one of index.php, whatever is there. I'd guess whitespace.
In general, when you see "headers already sent", it means that you are trying to do something that requires an HTTP header to be set, but you have already sent content to the client. When using HTTP, you have to send the headers, then the content comes after. If you've sent content, you've missed your opportunity to send headers.
Start the session on top of the file.
direct afer the php tag when you make an echo or send some header before you get this errormessage.
<?php
session_start();
ob_start();
include("constants.php");
include("includes.php");
?>
Enable output buffering by configuration. This way it will start before anything else, so it certainly solves your problem. Although you should look at your code and find the cause of the present error, because it seems you do not fully understand your site yet.
After enabling output buffering by configuration, you no longer need to call ob_start() manually.
Create a file called .htaccess with this content:
php_flag output_buffering on
... or add this line to your php.ini:
output_buffering = On
use_only_cookies=0 to use_only_cookies=1 in php.ini file
I encountered the same problem in xampp 7.0.1 and this fixed it. Read it here.
Yes start the session on top of the file amd make sure you do not have a space before
<?php
Plus you can call session_start(); only one time
session_start() must be called before there is ANY other output. The error is telling you that output started on index.php line 1. The call to session_start() must be placed before that.
Put this code inside includes.php
ob_start();
session_start();
And remove the same code from every other page.
Also make sure that in your files there is no whitespace before the initial
(specifically you are getting the error because something is wrong on the first line of index.php not of header.php)
I was getting this even when I had it at the top of the file.
Problem INVISIBLE UTF8 CHARACTER
Solution
Was to delete the file and make a new one starting with <?php and then copy everything after the php into the new one. (Don't copy the beginning of the file).