php session not working

Hi
ive been trying to get my sessions to work and im unable to echo the username.
code on userlogin;
[php] /*redirect browser */
header(“location: homepage.php”);
//set session variables
$_SESSION[“username”] = $username;
exit;
}
else {
//redirect back to logon form if not authorised
header(“Location: userlogin.html”);
exit; [/php]
code on my homepage
[php]p><?php echo 'Welcome ' . $_SESSION["username"] . 'to City Cars.'?>

[/php]

Any help would be great thanks

First you should be getting an error make sure you have error reporting turned on.
Here’s what I do ->
[php]/* Turn on error reporting */
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
if (filter_input(INPUT_SERVER, ‘SERVER_NAME’, FILTER_SANITIZE_URL) == “localhost”) {
error_reporting(-1); // -1 = on || 0 = off
} else {
error_reporting(0); // -1 = on || 0 = off
}[/php]
(Put this in a utilities file (config.php or utilities.inc.php))
I’m always forgetting to turn off error reporting when I put my files on the server, plus I’m lazy. ;D

Second you’re sending the header before setting the session, it should be the other way around.

Hi thanks for your reply.

I have swapped them around like you said but it still wont show the username when directed to this page, the code is showing no errors.

Thankyou

You are directing to the page, before you set that value.

Hi
The issue has been resolved

Thankyou for your help

Sponsor our Newsletter | Privacy Policy | Terms of Service