如何使用 php 在不同的页面中访问我的会话变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28403784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:50:55  来源:igfitidea点击:

How can I access my session variable in a different page with php?

phphtmlsessionredirect

提问by CloudyKooper

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.

我在login.php 页面的顶部启动了会话变量。然后设置会话,我使用 include 语句调用第二页。我用 if 语句进行了测试,它似乎通过了。然后我尝试在新显示的页面中回显变量的内容。页面显示意味着它通过了 if 块,但会话变量的内容没有显示。就好像会话变量已经超出了范围。如何在不同的页面中访问我的会话变量。

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

这是我的 php 文件调用的 html 页面:

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

似乎无法理解为什么我无法访问另一个页面上的会话变量。任何帮助将不胜感激。谢谢!

采纳答案by Angry 84

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.

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

The easiest way to do this, is have something like a header.phpfile 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

在这个 header.php 你会有类似的东西

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

回答by Bender

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

  • 你忘记 echo
  • 您需要在访问会话变量之前启动会话

    放在session_start()你的页面顶部放回声 Hi. I'm <?php echo $loggenOnUser; ?>

回答by Raghav

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

检查这个网站:http: //php.net/manual/en/function.session-start.php

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

您需要写入session_start()所有 php 文件,最好在顶部。

回答by Amit

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

如果你已经完成了 echo 和 session start,这绝对需要完成,但仍然没有,那么看起来你在 $found_admin 数组中针对该用户名没有任何内容

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

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

你确定那个数组中有东西吗?你能打印 $found_admin 数组吗?

回答by Adrian Cid Almaguer

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

试试这个,你忘记了开头的 session_start()

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

回答by banana_Ai

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

您应该session_start()在使用会话数组之前放置。此外,而不是<?php $loggenOnUser?>try <?=$loggenOnUser?>