View Single Post
  #1 (permalink)  
Old 09-03-2008, 10:28 PM
shiyam198 shiyam198 is offline
Newbie
 
Join Date: Sep 2008
Posts: 11
Credits: 0
Rep Power: 0
shiyam198 is on a distinguished road
Default Question on Sessions

John | Please use [code], [php], and [quote] tags when appropriate.
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 Code:
$_SESSION["logged"]=$_POST["username"]; 
Now, in all the pages I have, i have included the following code segment,
PHP Code:
<?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
?>
where the passwords.php is
PHP Code:
<?
$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");
     };
};
?>
Question:

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

Like this:
PHP Code:
<?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
(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
John | Please use [code], [php], and [quote] tags when appropriate.

Last edited by John; 09-03-2008 at 11:38 PM.
Reply With Quote

Sponsored Links