problem while using sessions...

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
iso-latte-d
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Jul 22, 2003 5:38 pm
Location: Melbourne, Australia

problem while using sessions...

Postby iso-latte-d » Tue Jul 22, 2003 5:50 pm

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?

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue Jul 22, 2003 7:12 pm

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.

User avatar
iso-latte-d
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Jul 22, 2003 5:38 pm
Location: Melbourne, Australia

Postby iso-latte-d » Tue Jul 22, 2003 7:54 pm

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

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue Jul 22, 2003 9:10 pm

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)?

User avatar
iso-latte-d
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Jul 22, 2003 5:38 pm
Location: Melbourne, Australia

Postby iso-latte-d » Tue Jul 22, 2003 11:15 pm

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

User avatar
iso-latte-d
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Jul 22, 2003 5:38 pm
Location: Melbourne, Australia

I've found it...

Postby iso-latte-d » Wed Jul 23, 2003 1:23 am

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: Select all

<?php
     session_start();
?>

<html>

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

</html>


thanks again...

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Wed Jul 23, 2003 5:29 am

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.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests

cron