Skip to main content
May 31, 2009
Question

Accessing session variables with php

  • May 31, 2009
  • 3 replies
  • 1046 views

I am trying to access the  session variable  $_SESSION['MM_Username']  in one of my php pages, but it is somehow showing empty (i.e it does not contain the username that was entered during login).

I checked and confirmed that my login.php function is properly setting the MM_Username session variable by echoing it  from the login function.

So why can't I read it from another php file in the same session?  Do I need to do something else before the session variables can

be properly read from any php file in the same session?  Any help would be appreciated.

This topic has been closed for replies.

3 replies

June 1, 2009

Answered.

DwFAQ
Participating Frequently
June 1, 2009

So what was the problem then I'm curious.

June 1, 2009

I found out in Dreamweaver documentation that I had to bind the MM_Username session variable from with the login page. A very simple procedure I was not aware I had to do. But most probably, I also needed what you had suggested. So, it looks that I was missing two things.

But everything is OK now. Thanks again for the help!

June 1, 2009

Here is a test code I am using to access the session variable $_SESSION['MM_username']  from  the php page  test.php. But it is not working.

I get an empty string all the time for $username. Can any one see something wrong with this code?

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?php require_once('Connections/MyTestingConn.php'); ?>

<?php session_start(); ?>

<?php

$username = "-1";

if (isset($_SESSION['MM_username'])) {

  $username = $_SESSION['MM_username'];

  echo $username;

}

else

   echo "Eror: can not access session variable";

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Test</title>

</head>

<body>

</body>

</html>

------------------------------------------------------------------------------------------------------------

June 1, 2009

I found what the problem was.  This question can be considered as answered.

DwFAQ
Participating Frequently
May 31, 2009

You need to start a session on the other page to show the session variable.

Put this code on the first line of your page:

<?php
session_start(); // start up your PHP session! 
?>
May 31, 2009

Thanks for the suggestion.  I tried it but I still can't get the username session variable. Is there anything else that need to be done?