5

everyone I started the session variable at the top of the login.php page. the session is then set and I call the second page using the include statement. I tested with an if statement and it seemed to pass ok. then I try to echo the contents of the variable in the newly displayed page. The page displays meaning that it passes the if block but the contents of the session variable does not show. It's as if the session variable has gone out of scope. How can I access my session variable in a different page.

 <?php
 session_start();
 $username = "";

 if (isset($_POST['submit']))
     {
     $username = $_POST["username"];
     $password = $_POST["password"];
     $_SESSION["username"] = $found_admin["username"];
     if (isset($_SESSION["username"]));
     {
     redirect_to("index.php");
     }
      else
    {
    $_SESSION["message"] = "Username/password not found.";
    }
    }

 ?>


 <?php include("login.html"); ?>

Here's the html page that's called by my php file:

 <!doctype html>

 <head>

 </head>
 <body>
 <?php

 echo $_SESSION["username"];
 $loggenOnUser=$_SESSION["username"];
 ?>
<div class="gridContainer clearfix">
    <div id="div1" class="fluid">
This page is being called by my login.php file.</div><div id="LoggedInUser" class="fluid ">Hi.  I'm <?php $loggenOnUser?> </div>
      <img id="homeImage"  src="images/home.gif" /> </div>
</div>
 </body>
 </html>

Can't seem to get why I can't get access to the session variable on another page. Any help would be greatly appreciated. Thanks!

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
CloudyKooper
  • 727
  • 3
  • 19
  • 47

6 Answers6

6

Make sure that you use

session_start();

In the start of every page, or any PHP file that needs to have access to the session.

The easiest way to do this, is have something like a header.php file and include/require this at the top of every page of your site or common pages.

In this header.php you would have something like

<?php
    session_start();
    if (isset($_SESSION['username'])) {
      // This session already exists, should already contain data
        echo "User ID:", $_SESSION['id'], "<br />"
    } else {
        // New PHP Session / Should Only Be Run Once/Rarely/Login/Logout

        $_SESSION['username'] = "yourloginprocesshere";
        $_SESSION['id'] = 444;
    }
?>

The simply have your page like this

 <?php require "header.php"; ?>
 <!doctype html>
 <head></head>
 <body>
 <?php
     if (isset($_SESSION["username"])) {
         $loggenOnUser = $_SESSION["username"];
         echo "Found User: ", $loggenOnUser, "<br />"
     } else {
         $loggenOnUser = " a public user";
     }
 ?>
     <div class="gridContainer clearfix">
         <div id="div1" class="fluid">
             This page is being called by my login.php file.
         </div>
         <div id="LoggedInUser" class="fluid ">
             Hi.  I'm <?php echo $loggenOnUser; ?> 
         </div>
         <img id="homeImage"  src="images/home.gif" /> </div>
     </div>
 </body>
 </html>
Angry 84
  • 2,935
  • 1
  • 25
  • 24
  • Your welcome ;) just a hint.. with the require "header.php"... expand that to multiple includes/requires... sessions.php/database.php/user.php/content.php or which ever.. its a handy use with php. – Angry 84 Feb 09 '15 at 07:31
2

*edit

  • You forget echo
  • You need to start session before accessing session variables

    put session_start() on top of your page put echo Hi. I'm <?php echo $loggenOnUser; ?>

Bender
  • 705
  • 11
  • 25
2

Check this site: http://php.net/manual/en/function.session-start.php

You need to write session_start() in all your php files, best at the top.

Chasevanb
  • 418
  • 3
  • 10
Raghav
  • 570
  • 1
  • 13
  • 24
1

You should put session_start() before you use session array. Also, instead of <?php $loggenOnUser?> try <?=$loggenOnUser?>.

banana_Ai
  • 54
  • 3
  • I started the session on the first page. I thought it only needed to be started once where ever it's initiated right? – CloudyKooper Feb 09 '15 at 06:23
  • Nop. You should start it on every page. It is not like really "start", but more like "create new or load". – banana_Ai Feb 09 '15 at 06:40
1

If you have done echo and session start, which absolutely need to be done, and still nothing, then looks like you have 'nothing' in the $found_admin array against that user name

$_SESSION["username"] = $found_admin["username"];

Are you sure there is something in that array? Can you print the $found_admin array?

Amit
  • 1,836
  • 15
  • 24
1

Try this, you forget the session_start() at the beginning

 <?php
 session_start();
?>
    <!doctype html>

     <head>

     </head>
     <body>
     <?php

     echo $_SESSION["username"];
     $loggenOnUser=$_SESSION["username"];
     ?>
    <div class="gridContainer clearfix">
        <div id="div1" class="fluid">
    This page is being called by my login.php file.</div><div id="LoggedInUser" class="fluid ">Hi.  I'm <?php $loggenOnUser?> </div>
          <img id="homeImage"  src="images/home.gif" /> </div>
    </div>
     </body>
     </html>
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63