Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Question on Sessions

session

  • Please log in to reply
1 reply to this topic

#1 shiyam198

shiyam198

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 03 September 2008 - 06:28 PM

John | Please use [code=auto:0],
, and [quote] tags when appropriate.[/COLOR][/B]
Hi,

First time using sessions.

I created a Userlogin functionality to the website I am creating.
I had the index.php page where it takes the username and password and checks against a file where I have the password and when it is correct assigns the username to a variable called "logged".
[php]
$_SESSION["logged"]=$_POST["username"];[/php]

Now, in all the pages I have, i have included the following code segment,
[php]
<?php
session_start(); /// initialize session
include("passwords.php");
check_logged(); /// function checks if visitor is logged. If user is not logged the user is redirected to login.php page
?>[/php]

where the passwords.php is
[php]
<?
$USERS["user1"] = "pass1";
$USERS["user2"] = "pass2";
$USERS["user3"] = "pass1";
function check_logged(){
global $_SESSION, $USERS;
if (!array_key_exists($_SESSION["logged"],$USERS)) {
header("Location: index.php");
};
};
?>[/php]

Question:

In those pages I already had two includes so I uncluded the code segment after that.

Like this:
[php]
<?php
include("top.php");
include("conn.php");

session_start(); /// initialize session
include("passwords.php");
check_logged(); /// function checks if visitor is logged. If user is not logged the user is redirected to login.php page
?>

When I did that, I got the following error message.
[quote]
"session_start(): Cannot send session cache limiter - headers already sent[/quote] (output started at top.htm............"

I took the code segment out from all the pages and put on top of top.php, it works fine.

Why does that session_start() should be on top the file. Or is it some other problem caused this error ?

Thanks for your time
Shiyam
[B]John | [COLOR="Red"]Please use [code=auto:0], [php], and [quote] tags when appropriate.

Edited by John, 03 September 2008 - 07:38 PM.

  • 0

#2 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 03 September 2008 - 07:36 PM

When I did that, I got the following error message.
(output started at top.htm............"

I took the code segment out from all the pages and put on top of top.php, it works fine.

Why does that session_start() should be on top the file. Or is it some other problem caused this error ?


The cache limiter warning is issued when content is outputted prior to the session_start() directive being called. That said, it must be placed at the top of the page (or before you output any content). I'm not sure the reasoning behind it - I just accepted it.
  • 0





Also tagged with one or more of these keywords: session

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download