Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Accessing session variables with php

Guest
May 31, 2009 May 31, 2009

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.

TOPICS
Server side applications
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
May 31, 2009 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! 
?>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 31, 2009 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 31, 2009 May 31, 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>

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 31, 2009 May 31, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 31, 2009 May 31, 2009

Answered.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 01, 2009 Jun 01, 2009

So what was the problem then I'm curious.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 01, 2009 Jun 01, 2009
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines