elgg 1.6.1 session problem

Following the blog plugin, I have made a plugin similar to it. Everything seems to work fine except for some problems which I think is due to session.

In my index.php I have the following.

// Get the current page's owner
$page_owner = page_owner_entity();

if($page_owner === false || is_null($page_owner))
{
  $page_owner = $_SESSION['user'];
  set_page_owner($_SESSION['guid']);
}

I login as user1 then I go to my plugin's index page I had a system_message($_SESSION['user']->username) so I could see the current user's username, the username is correct. Logged out.

Then I login as user2 then I go again to my plugin's index page this time system_message($_SESSION['user']->username) output was user1's username.

 

Any help would be greatly appreciated.