-1

I am trying to check whether a user is logged in or not on the index.php page. If user is not logged in I want to redirect the user to login.php page.

Below is error log:

> PHP Warning: Cannot modify header information - headers already sent
> by (output started at
> /home/consolem/public_html/megalibrary/index.php:2) in
> /home/consolem/public_html/megalibrary/session.php on line 6
> 
> PHP Warning: session_start(): Cannot send session cookie - headers
> already sent by (output started at
> /home/consolem/public_html/megalibrary/index.php:2) in
> /home/consolem/public_html/megalibrary/session.php on line 3
> 
> PHP Warning: session_start(): Cannot send session cache limiter -
> headers already sent (output started at
> /home/consolem/public_html/megalibrary/index.php:2) in
> /home/consolem/public_html/megalibrary/session.php on line 3

Kindly check my session.php code:

<?php
//Start session
session_start();
//Check whether the session variable SESS_MEMBER_ID is present or not
if (!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
    header('location: login.php');
    exit();
}
$session_id=$_SESSION['id'];
?>

Kindly check the line no. 2 of my index.php:

<?php include('session.php'); ?>

Whats wrong in my code ? I would really appreciate if anyone can guide to to solve these errors.

Thanks in advance.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Ashish Bafna
  • 43
  • 1
  • 10

2 Answers2

0

The problem is that you are sending the DOCTYPE on the first line, before the session starts.

<!DOCTYPE html> 
<?php include('session.php'); ?> 
<?php include('dbcon.php'); ?> 
<html lang="en">

Change to

<?php include('session.php'); ?> 
<!DOCTYPE html> 
<?php include('dbcon.php'); ?> 
<html lang="en">

Make sure the include is the first line

MortimerCat
  • 829
  • 9
  • 26
-1

Do you use header(); before session_start(); in index.php ??

If you do that session_start must be in top of the page or try to remove it from session.php to top of index.php

Mohamed Belal
  • 610
  • 7
  • 11