I am not able to set php session

for starting the session I am using below code

session_start();
$_SESSION['username'] = $username;

For checking the session I m using

<?php
if (!isset($_SESSION['username'])) {
header('Location: index.html');
}
?>

But the session is not being set as I am not able to grep it. Please help urgent.

Recommended Answers

All 4 Replies

If you try to make a session after you have sent text you'll get a message like this:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at
Is that your problem? If you're receiving an error message, please show it.

No I am not geeting any error. I am simply redirected to index.html page

Are you starting and setting the session at the VERY top of your page before anything else?

Print the session variable and see,i think so $username is empty,that's why it is behaving like this.
Do one thing:-
echo $username;
and after setting into session,i.e.,
$_SESSION['username'] = $username;
echo $_SESSION['username'];
If this is also showing correct ,then the probleem is this:-
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: index.html');
}
?>

As the example you are showing doesn't start the session.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.