For some reason my post id session is not carrying over to my grading script page. I grab the post_id from the posts url in my posts page and turn it into a $_SESSION to carry it to my grading script page. My $_SESSION that holds the logged in users id $_SESSION['user_id'] if logged in carries over to the grading script but not the post $_SESSION. How can I have my posts id carry over to my grading script page?
I have session_start(); at the top of both of my pages.
$_SESSION['post_id'] = $_GET['pid'];
You may have some output before you started the session. If that is the case, the session might not be able to be set correctly. Enable debugging mode and search for an error regarding the session_start() function:
ini_set('display_errors', 'on');
error_reporting(E_ALL);
Or check the error_log if you can't switch debugging on.
The error might look like this:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\phptests\test.php:1) in C:\xampp\htdocs\phptests\test.php on line 4
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\phptests\test.php:1) in C:\xampp\htdocs\phptests\test.php on line 4
The most common reason for having problems with php sessions is that the php script is outputting something before the session is started.
Ok, you might not have any echo statements, or you might not even have ANY php code apart from reading the session ... but take time to check what happens BEFORE your code even begins.
For example, let's play spot the difference: (only one code will work)
<?php
session_start();
$_SESSION["test"] = "hello";
if($_SESSION["test"] == "hello";
{
echo "Session is working!";
}
else
{
echo "Session is NOT working!";
}
?>
and...
<?php
session_start();
$_SESSION["test"] = "hello";
if($_SESSION["test"] == "hello";
{
echo "Session is working!";
}
else
{
echo "Session is NOT working!";
}
?>
The difference is that there is a single space before the opening ANY data before session_start() will stop the script from working.
I've been there - having an accidental space or blank line at the top of the document will create serious issues, but enabling the php error outputs should show you where you are going wrong if this is the case.
Related
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 6 years ago.
home screen
Error: ( ! )
Warning: Session_start(): Cannot Send Session Cache Limiter - Headers
Already Sent (Output Started At
C:\Wamp64\Www\Needheee\Userheader.Php:192) In
C:\Wamp64\Www\Needheee\Userheader.Php On Line 193
<li>
<a> Welcome
<?php
session_start();
echo $_SESSION['username'];
?>
</a>
</li>
By default, the PHP sessions feature use a cookie to propagate the session ID between requests.
The cookie is set by session_start(). Because the cookies are sent in the header of the HTTP response, session_start() must be called before any output is sent to the browser.
You call session_start() after some HTML is already generated. You can either move the call to session_start() before anything else in the file of use output buffering to capture the output (instead of sending it to the browser immediately) and send it when it's appropriate for your application's flow.
Place session_start() at the very beginning of the page
<?php
session_start();
?>
//html codes
<li>
<a> Welcome
<?php
echo $_SESSION['username'];
?>
</a>
</li>
This means you have too many session_starts. to remove this error, check for a valid session on every page. Do this:
<?php
if(!isset($_SESSION)){
session_start();
}
//this would check for a session before starting it. if session has started, php would not attempt to start a new one
?>
Cannot Send Session Cache Limiter - Headers Already Sent
This warning is displayed when a text was written (Buffer created)
and this can happen when you do a redirect when your script has created some text.
The Solution : move your session_start() function into the top of the file before your script output any text.
Headers Already Sent Error mostly occur when
You have white space in you script at very first line of you script
If you are sending the output of page before executing the php script.
Solution of Error
write you php code first then write html.
place this code at first line <?php ob_start();?>
I am having trouble with my sessions.
I get the error
Warning: session_start(): Cannot send session cache limiter
I have this code in 2 pages
if (!isset($_SESSION)) {
ini_set('session.gc_maxlifetime', 3*24*60*60);
session_set_cookie_params(3*24*60*60);
session_start();
}
I can only find instances of people who haven't does the isset check, so I have no idea why this is happening :(
I've checked for whitespace, my php tag is on line 1 in both files.
This is the first page, which loads dbmgmt which has the above code. I need the session code in both files because dbmgt is not always included from a page that creates a session.
<?php
if (!isset($_SESSION)) {
ini_set('session.gc_maxlifetime', 3*24*60*60);
session_set_cookie_params(3*24*60*60);
session_start();
}
require("dbmgmt.php");
?>
You are trying to call session_start(), which is trying to send an HTTP Header to the output of the page.
But somewhere earlier in your script, you've already started outputting the content of the page. The headers must come before the content.
Try calling this code before any HTML or content (even spaces or whitespace outside of the ?<php and ?> tags).
TRY
<?php
session_start();
ob_start();
?>
This will solve your issue.
A novice php learner. I read in a book, and continue to see this at certain forums and tutorials that the statement: session_start() is required to access all global session variables. And yet, multiple solutions offered at stackoverflow suggest using a block of this sort:
if(!(_isset($_SESSION['user']))){
session_start()
}
to be able to access the session variables. Based on my understanding, the session variable $_SESSION['user'] could only have been set at a previous php file by starting a session, and is "only" visible to the current page after the session_start() statement is called. Yet it produces the notice:
Notice: A session had already been started - ignoring session_start().
what am i missing?
Thanks everybody!
Your first block of code should be checking if the session variable is set, rather than the user variable exists in the session:
if(!isset($_SESSION)) {
session_start();
}
However, if you just ensure that you only have a single session_start() per page then you can avoid the "A session had already been started" notice.
session_start() is required to read / set any session variables.
Generally, I would think your code should look like this:
session_start()
if(!(_isset($_SESSION['user']))){
// do stuff here
}
However, the error message implies that you have already started the session elsewhere in your file.
You might have auto_start turned on somewhere (php.ini, .htaccess, etc)?
http://www.php.net/manual/en/session.configuration.php#ini.session.auto-start
Here is a scenario where your error would be triggered :
index.php:
<?php session_start();
require_once('some-page.php'); ?>
some-page.php:
<?php session_start(); // this would make an error when included to index.nl ?>
some-page.php should not have session-start in it as index.php already has started the session.
Also note that going to another page or even closing the tab will not reset your session variables ! so if you set S_SESSION['user'] = 'someuser'; , you close the tab and go to the website again, the session is still there and $_SESSION['user'] would still have someuser as value ! to manualy destroy the session , use session_destroy();
I have a login page that I register two sessions username and password. then redirect to another page. Once at this page
$_SESSION['username'] = "";
$_SESSION['password'] = "";
after login check I have the next page check if the session is registered
session_start();
if(isset($_SESSION["username"]){
continue
}else go back to login page
Once I'm logged in I want to go to another page that depending on if the session variable is set I display something different on the page.
So on the galery page I do
at the very top of page I do
<?php
session_start();
?>
then further down where I want the button to be I do
<?php
if (isset($_SESSION['username'])){
show a new button
}
?>
I get the button to show but at the top of the page I have
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
and it messes up how my page is displayed. Any ideas? I have the session_start(); at the very begging of page I don't know why this is happening. Any ideas?
You'll get that error if anything outputs to the browser before you call session_start(). For example, you can't do:
<?php
echo "Test";
session_start();
You also can't do:
<?php session_start();
(note the space before the <?php)
Make sure nothing - no HTML, no blank lines, no spaces - is written out prior to your session_start() calls and you'll be fine.
You need to ensure there's nothing (whitespace, UTF-8 BOM) before your <?php. This also applies to any files you include before the session_start() call.
Is the gallery page being included in another file?
<?php
// lots of php code
include('/path/to/gallery.php');
?>
If anything is being sent to the browser before the session_start(); it will create this error.
Session is a header, headers can't be sent after any output (even a single space). You got 2 options, place session_start() before ANY output or you could also use output buffering, this allows you to send headers after output.
Place this at the top of your script
ob_start();
And this at the end
echo ob_get_clean();
i have the following code:
if (!isset($_SESSION)) {
ob_start();
}
$_SESSION['views'] = 1; // store session data
echo $_SESSION['views']; //retrieve data
i tried to break it up in 2 parts like this:
//Page 1
if (!isset($_SESSION)) {
ob_start();
}
$_SESSION['views'] = 1; // store session data
.
//page 2
echo $_SESSION['views']; //retrieve data
it echos nothing, what am I doing wrong?
as Gumbo mentioned, you need to call session_start() on every page you want to use the session..
You also mentioned you were getting the error:
Warning: session_start(): Cannot send session cache limiter - headers already sent
This is due to data being output on the page before the session_start() is being called, you have to include this before anything is echo'd to the browser.
Make sure to call session_start on every page you want the session to be available. ob_start is not the session handler but the output buffer handler.
session_start() in 2 files before any output.