PHP会话错误和警告

时间:2015-05-25 03:54:22

标签: php session syntax-error warnings

我正在尝试检查用户是否在index.php页面上登录。如果用户未登录,我想将用户重定向到login.php页面。

以下是错误日志:

> 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

请检查我的session.php代码:

<?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'];
?>

请检查行号。我的index.php中有2个:

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

我的代码有什么问题?如果有人能指导解决这些错误,我将非常感激。

提前致谢。

2 个答案:

答案 0 :(得分:0)

问题是您在会话开始之前在第一行发送了DOCTYPE。

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

更改为

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

确保包含是第一行

答案 1 :(得分:-1)

你使用header();在session_start()之前;在index.php ??

如果您这样做,session_start必须位于页面顶部或尝试将其从session.php删除到index.php的顶部