Matt
Matt

Reputation: 1063

website session not set

I'm kind of a website development nub so bear with me.

My problem is that the website php session doesn't seem to be set when I log in to my website. After ensuring that the username and password are correct, I have the following simple code:

session_start();
$_SESSION['username'] = $myusername;
$_SESSION['password'] = $mypassword;

Content that should display after logging in is not displayed.

For example:

if(isset($_SESSION['username'])){
echo "<a class=\"logout\" href=\"logout.php\"><img      src=\"Images\logout_button.png\"></a>";
} 
else{
echo "<a class=\"login\" href=\"main_login.php\"><img src=\"Images\login_button.png\"></a>";
}

The login button is always visible.

Thanks in advance,

Matt

Upvotes: 2

Views: 580

Answers (3)

user1861121
user1861121

Reputation: 1

Per W3Schools session_start() must be before the HTML div.

Upvotes: -2

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91963

You must have session_start() on every page that needs the session variables. Do you have session_start() on the page with the login/logout links?

Upvotes: 1

&#211;lafur Waage
&#211;lafur Waage

Reputation: 70001

You need to start the session before you set the session variables. session_start() should be the first thing that runs.

Upvotes: 2

Related Questions