how to avoid getting error on session start [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
"Cannot send session cache limiter - headers already sent" [duplicate]
(1 answer)
Closed 5 years ago.
<?php
if(session_id() == '' || !isset($_SESSION))
session_start();
?>
i have this line of code in all my pages... and it is located at the very top of my script.. but unfortunately im getting an error on one of my script but the other pages didn't get error... i tried to search it but they said that it is needed to be at the top of script before HTML.. just what i did... but i dont have any idea why im getting error on one of my pages.. i found this error when i start to upload it to my cpanel server.. but on my local server its totally working..
this is the warning message that i got.. and when i tried to remove this on one of my script which gives me the error message my page did not functioning well
Warning: session_start(): Cannot send session cache limiter - headers already sent

Posting as an asnwer as per the comment made by OP
This occurs if you have output before session_start() is called. The most typical culprit is a space (or any character) before the <?php tag (be aware of zero-width space characters also).

The session_start() function needs to be the first one
<?php
session_start();
if(session_id() == '' || !isset($_SESSION))
?>

Related

Session Start Error [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
"Cannot send session cache limiter - headers already sent" [duplicate]
(1 answer)
Closed 8 years ago.
I know this has been posted before but none of the other answers worked for me. Here is my problem: I am making a login page for my website. I keep getting this error and I cant get rid of it:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/howto570/public_html/admin/index.php:1) in /home/howto570/public_html/admin/index.php on line 6
now here is the first bit of code
<?php
$session_prefix = "henry_";
$default_title = "Admin Panel";
$folder_root = "/admin";
session_start();
require("../auth.php");
$userdata = array();
?>
Even when I slash out all the other code like so:
<?php
session_start();
//$session_prefix = "henry_";
//$default_title = "Admin Panel";
//$folder_root = "/admin";
//require("../auth.php");
//$userdata = array();
?>
I still get the error.
So im at a loss not sure what to do, you can see the site itself here: www.howtotech.ca/admin/ . I dont know what to do.
From looking at your link, it looks like you are probably including this script in another script. There is HTML being outputted before your error message (ie. the head section). You can fix this by moving the session_start() to the top of the script that is including your script.
This mostly happens because something is outputted before the session_start(). Even if you have single space before the starting of php remove it, the session_start should come before any output. If this doesn't work than most probably your editor can't see the blank space so you have to create a new file and paste the content in it.
<?php
^^^^
session_start();
?>
Hope this helps you

Cannot modify header information [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
my site works well in localhost (everything works fine)
but when I upload it in web and try to redirect the page after updating the data it says -> Cannot modify header information
95% you are trying to set a cookie, mime-type or redirection (short: http header) after you have sent http body, i.e. content. Check if you you have anything including whitespaces outside the php delimeters.
If you use the header function you have to be careful... You can't send any header before the "header location".
ob_start('mypage');
<html>
// Your code ...
</html>
ob_end_flush();
What's the error ? (php.ini, active the errors)
Else you can use the ob_start() function !

Getting Session error on live site [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I have started my session in config.php file & i have included that file on every page on my site,
my site works fine in localhost,
but when i have done live to it,it shows error like this,
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
Any suggestions...
Thanks,
check that you don't send content before calling session_start. Just make session_start the first thing you do in your PHP file.
<?php session_start(); ?>
If you will have space after closing PHP Tags in your config.php at the end of your code then also these warning will come.
So make sure you don't have any space after closing of PHP Tags which is ?>

Inserting PHP script using include(''); causes headers already sent error [duplicate]

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 insert a form script into my home page index.php by using the following line of code.
<?php include('email-system/index.php'); ?>
and continue to get the two error messages below. The form shows up, but I get this error below quite often when trying to insert script into my home page using the include statement. I have read in a number of different places that it could be white space. Since the file works fine if you go to www.mysite.com/email-system/index.php I'm assuming that it's not white space. Any ideas on what my problem is and how I could insert the code and correct the errors? Thank you in advance.
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/84/8661784/html/header.php:7) in /home/content/84/8661784/html/email-system/index.php on line 6
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/84/8661784/html/header.php:7) in /home/content/84/8661784/html/email-system/index.php on line 6
You will only get that if you are echoing output before the include statement.
Make sure whatever you need is included / loaded before you send output to the user.

Php redirect to url [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 4 years ago.
I am trying to show some page that automatically redirect to another page, when using header(location) I get this error:
Warning: Cannot modify header information - headers already sent by (output started at public_html/headerFile.php)
What else should I try?
The longer, and more correct way, is to reorganize your logic so that it knows that it should redirect the user before it tries outputting anything to the page. Do all of your logic first, then display.
The quick easy way around is to add ob_start() to the beginning of your script. This turns on output buffering, so nothing gets sent to the browser until everything is finished, which means you can still modify the headers.
Try this:
echo '<meta http-equiv="Refresh" content="1;URL=http://www.google.nl">';
That should work, it's not ideal. But it will do the trick.
When using header(), you have to make sure that you have not sent any output to the user. This includes print_r, echo or any errors or warnings generated by PHP. Even whitespace counts as output.

Categories