I'm getting the following warnings when trying to initiate a session:
Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent
(output started at /www/zxq.net/l/i/b/librarymanagement/htdocs/public/admin/index.php:2)
in /www/zxq.net/l/i/b/librarymanagement/htdocs/includes/session.php on line 9
I've made sure all white space is clear, session_start()
should be the first thing being called. I don't think anything else is being sent before session_start()
is called.
Here is my code:
class Session {
private $logged_in=false;
public $user_id;
public $message;
func开发者_运维技巧tion __construct() {
session_start();
$this->check_message();
$this->check_login();
if($this->logged_in) {
} else {
}
}
public function is_logged_in() {
return $this->logged_in;
}
public function login($user) {
if($user) {
$this->user_id = $_SESSION['user_id'] = $user;
$this->logged_in = true;
}
}
public function logout() {
unset($_SESSION['user_id']);
unset($this->user_id);
$this->logged_in = false;
}
public function message($msg="") {
if(!empty($msg)) {
$_SESSION['message'] = $msg;
} else {
return $this->message;
}
}
private function check_login() {
if(isset($_SESSION['user_id'])) {
$this->user_id = $_SESSION['user_id'];
$this->logged_in = true;
} else {
unset($this->user_id);
$this->logged_in = false;
}
}
private function check_message() {
if(isset($_SESSION['message'])) {
$this->message = $_SESSION['message'];
unset($_SESSION['message']);
} else {
$this->message = "";
}
}
}
Here is how it is used:
$session = new Session();
$message = $session->message();
What could be the problem?
You're trying to send headers after the body of response or in the middle part of the body, it's not allowed in HTTP...
the reason is if you start your code
\s
<?php
you space is the body part of your response... and when you finish your code with
?>
\s
this space is output too....
You need to call session_start()
before sending any data to the client
You have output before the call to session_start(). No doubt about it. Check if there is not an error message or a warning thrown before that, be weary of any whitespace at beginning of file.
This is your error message:
(output started at /www/zxq.net/l/i/b/librarymanagement/htdocs/public/admin/index.php:2)
Please read it.
Look at the mentioned file and line number. There is your problem.
精彩评论