如何使用php在其他页面中访问会话变量? [英] How can I access my session variable in a different page with php?

查看:81
本文介绍了如何使用php在其他页面中访问会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每个人都在login.php页面顶部启动了会话变量.然后设置会话,然后我使用include语句调用第二页.我用if语句进行了测试,它似乎通过了.然后我尝试在新显示的页面中回显变量的内容.该页面显示,意味着它传递了if块,但会话变量的内容未显示.就像会话变量超出范围一样.如何在其他页面中访问会话变量.

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"); ?>

这是我的php文件调用的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!

推荐答案

请确保您使用

session_start();

在每个页面的开头,或需要访问该会话的任何PHP文件.

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

最简单的方法是使用 header.php 之类的文件,并在您网站的每个页面或普通页面的顶部都包含/要求该文件.

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.

在此header.php中,您将拥有

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;
    }
?>

只需拥有您的页面

 <?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>

这篇关于如何使用php在其他页面中访问会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆