problem while using sessions...

A

Anonymous

Guest
hi all...
i've started using php for my computing project.
so i'm still very much a newbie...hope you don't mind with my question.
i learned from books about creating session and stuff, and i've got a problem with it.
it displayed something like this:

"Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at ..."

could anyone help me out with this? i mean, i can display information from the session variables, but unfortunately it also display the warning. what is it exactly i must do?
 
For a session to initialize itself, it must send data with the HTTP header. The HTTP header is sent automatically by the server (so you don't need to worry about it), but PHP's session functions (and also the header() command and others) add data to the headers. But in order to add data to the headers, such calls must be made before any other data is sent to the browser. You may not even send a space or a blank line before you try to send a header.

This means that in your PHP script, you must have your session_start() command before any output (e.g. echo, print, etc.), and it must be contained in a block of code that begins at the first character of the first line of the file (that is, the first <? has to be at the very beginning of the file).

There are ways around this, namely output buffering (refer to the documentation), but I recommend that you just make sure you're following the rules and everything will work out.
 
thanks for the reply...
i've checked my script again, and my session_start() was right in the beginning of the php script.

here's what i'm trying to do...
imagine 2 php files: login.php & details.php

login.php checks the username, and if it's the right username, it will register the username into a session variable and then redirect to another page (details.php).

some parts of the script:
session_start();
...
if login correct, then
session_register("verifieduser");
$verifieduser = $_POST[username];
header("Location:details.php");
else
go back to loginform


details.php gets this userlogin from the session variable and performs a query in which to display the details of the user with that userlogin.

parts of the script:
session_start();
...
sql query to display all records where username='$_SESSION[verifieduser]', then display.


both files have session_start() at the very beginning of the script, just after the <?php tag.

i hope you can enlighten me...

thanks again
 
Is there anything before the <?php tag, even an empty line or a space? Is the script generating any other errors (errors create output, which trips up session_start() for the same reason)?
 
before the <?php tag, there is other script...
cos actually i'm embedding the php script within an html script.
but the html script is only to produce table for the layout.
i know i should've used php templates, but i find it to be very confusing at the time being due to the number of files as a result of templates.

there is no other error message besides the one that i pasted.
"Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at ...bla bla file path)"

thanks
 
hi swirlee,
i just figured out my mistake...
i misunderstood your post, i thought what you meant was only before anything inside <?php ?> tags.
but the session_start() must be before ANYTHING displayed in the web browser and that includes any html scripts.
i didn't know that having scripts embedded can also affect this...

so, what i did was
Code:
<?php
     session_start();
?>

<html> 

<?php
     sql query to display all records where username='$_SESSION[verifieduser]', then display.
?>

</html>

thanks again...
 
Glad you got it working. For the record, you can use other PHP before the session_start(), you just can't output anything the the browser before it.
 
Back
Top