简体   繁体   English

PHP会话变量寿命

[英]PHP session variables life

Newbie question, but I'm wondering if I'm missing something elementary here. 新手问题,但我想知道我是否在这里缺少基本知识。

If I register a session variable in a page - isn't this variable supposed to be accessible from another page on the same site? 如果我在页面中注册会话变量-难道该变量不能从同一站点的另一个页面访问吗?

First, I register a variable in the file session_var_register.php: 首先,我在文件session_var_register.php中注册一个变量:

<?php
    $_SESSION["myusername"] = 'user';
    if (isset($_SESSION['myusername'])) {
        echo 'Session var myusername is set to '.$_SESSION['myusername'];
    }
?>

When I open this page, it writes: 当我打开此页面时,它写道:

Session var myusername is set to user

As expected. 如预期的那样。

Then I open another tab and another page, check_session_var.php: 然后,我打开另一个选项卡和另一个页面,check_session_var.php:

<?php
if (isset($_SESSION['myusername'])) {
    echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>

This page is blank. 此页为空白。

Isn't the point of a session variable that it should be accessible in the browser session, until the session is programatically destroyed or the browser closed? 在会话被编程破坏或关闭浏览器之前,不是应该在浏览器会话中访问会话变量的意义吗?

I'm using IE 8 and Firefox 24, btw. 我正在使用IE 8和Firefox 24。 Identical results. 结果相同。

You forgot 你忘了

session_start() 

On top, before using 最重要的是,使用之前

$_SESSION

PS: Remember to call session_start() in every page you want to use $_SESSION . PS:请记住在要使用$ _SESSION的每个页面中调用session_start()

The PHP docs state that you must call session_start() to start or resume a PHP session. PHP文档指出您必须调用session_start()才能启动或恢复PHP会话。 This must be done before you try to access or use session variables. 必须先完成此操作,然后再尝试访问或使用会话变量。 Read more here . 在这里阅读更多。

session_start();

First Of all start session on that page 该页面上的所有开始会话中的第一个

session_start();

your page like this way 您的页面是这样的

<?php

session_start();

if (isset($_SESSION['myusername'])) {
    echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>

Your session variables will be available on different pages of the same site but on top of each of these pages you must have at least: 您的会话变量将在同一站点的不同页面上可用,但在每个页面的顶部,您必须至少具有:

session_start();

It works but not in all cases. 它有效,但并非在所有情况下都有效。 You must also use the same session name (essentially a cookie name that stores id of your session) on all pages. 您还必须在所有页面上使用相同的会话名称(主要是存储会话ID的cookie名称)。 Moreover cookies (which are essential (mostly) for sessions to work) may be made visible only in specific directory. 此外,仅在特定目录中可以显示cookie(对于会话的正常运行来说,cookie是必不可少的)。 So if for example you share the same host with other guys that use sessions too you do not want to see their variables and vice versa so you may want to have sth like that: 因此,例如,如果您也与使用会话的其他人共享同一主机,则您不希望看到其变量,反之亦然,因此您可能希望拥有这样的东西:

1) session_name( 'my_session_id' );
2) session_set_cookie_params( 0, '/my_dir', $_SERVER['HTTP_HOST'], false, true );
3) session_start();

You may also want to see your session variables on other servers and in such case custom session handlers may be useful. 您可能还希望在其他服务器上查看会话变量,在这种情况下,自定义会话处理程序可能很有用。 Take a day or two to implement yourself - great way to understand how sessions work hence I recommend. 花一两天来实现自己-了解会话如何工作的好方法,因此我建议。

Method 方法

session_start();

Description 描述

session_start() creates a session or resumes the current one based on a session identifier >passed via a GET or POST request, or passed via a cookie. session_start()创建会话或基于通过GET或POST请求传递或通过cookie传递的会话标识符恢复当前会话。

Usage in your case (and in the most of cases): Put it before the $_SESSION usage. 在您的情况下(大多数情况下)的用法将其放在$_SESSION用法之前。

Reference: session_start() 参考: session_start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM