如何使用 PHP 从一个页面到另一个页面继续会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2542427/
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 06:54:18  来源:igfitidea点击:

How do I continue a session from one page to another with PHP

phpsession

提问by MrEnder

Ok I set up a session... but now how do I make it work on my other pages?

好的,我设置了一个会话...但现在我如何让它在我的其他页面上工作?

I tried doing

我试着做

@session_start();

if(isset($_SESSION['$userName'])) {

 echo "Your session is running " . $_SESSION['$userName'];

}

回答by Benoit

If your PHP setup is clear (session writing ok) and cookie normally sent to browser (and preserved), you should be able to do something like this

如果您的 PHP 设置清晰(会话写入正常)并且 cookie 通常发送到浏览器(并保留),您应该能够执行以下操作

On first page :

在第一页:

session_start();
$_SESSION['userName'] = 'Root';

On a second page :

在第二页:

session_start();
if(isset($_SESSION['userName'])) {
  echo "Your session is running " . $_SESSION['userName'];
}

Be careful session_start() must be called before any output is sent, so if you had to use the @ for session_start it can hide warnings.

注意 session_start() 必须在发送任何输出之前调用,所以如果你必须使用 @ 作为 session_start 它可以隐藏警告。

As these are warnings, if given example doesn't work try to add this before calling session_start :

由于这些是警告,如果给定的示例不起作用,请尝试在调用 session_start 之前添加它:

error_reporting(E_ALL);

回答by hemanth

On the first page (test1.php),

在第一页(test1.php),

<?php
session_start();

$_SESSION['userName'] = 'This is Ravi';

?>

On the second page (test2.php),

在第二页(test2.php)上,

<?php
session_start();
if(isset($_SESSION['userName'])) {
echo "Your session is running " . $_SESSION['userName'];
}

?>

回答by user3225121

Make sure both pages are on the same domain. Even www.site.com is different than site.com

确保两个页面都在同一个域中。甚至 www.site.com 也不同于 site.com

Using echo session_id(); also helps identifying your session_id on each page, make sure there are the same

使用 echo session_id(); 还有助于在每个页面上识别您的 session_id,确保有相同的

回答by Parviz

Be aware of case sensitivity in the $_sessionname variables.

请注意$_session名称变量中的区分大小写。

Therefore $_SESSION['userName']different with $_SESSION['username'].

因此$_SESSION['userName']与 不同$_SESSION['username']

回答by user7522043

  1. Create a login page, the user must not login without correct id and passowrd.
  2. After logging in the user comes to the home, here user can logout and goes back to the login page
  1. 创建一个登录页面,用户必须在没有正确的 id 和密码的情况下登录。
  2. 登录后用户进入首页,这里用户可以退出并返回登录页面

NOTE: User must not access home page without going through login page.

注意:用户不得在未通过登录页面的情况下访问主页。

回答by Syno

first page (hello1.php) - Storing Session

第一页 (hello1.php) - 存储会话

$userName = "Nick";
session_start();
$_SESSION['username'] = $userName;

second page (hello2.php) - Output Session

第二页 (hello2.php) - 输出会话

session_start();
$userName = $_SESSION['username'];
echo "$userName";

Output: Nick

输出:尼克

回答by John Conde

Make sure session_start() is at the top of every page you wish to use sessions on. Then you can refer to session variables just like in your example.

确保 session_start() 位于您希望使用会话的每个页面的顶部。然后您可以像在您的示例中一样引用会话变量。

回答by symcbean

Check that the session cookie is being sent to the browser on the first hit and getting returned by the browser in subsequent requests. There are lots of reasons why this may be failing - typically PHP has flushed the headers before the call to session_start() (which may be due to UTF-8 BOM chars or just messy programming).

检查会话 cookie 是否在第一次点击时发送到浏览器,并在后续请求中由浏览器返回。这可能失败的原因有很多 - 通常 PHP 在调用 session_start() 之前已经刷新了标头(这可能是由于 UTF-8 BOM 字符或只是混乱的编程)。

Make sure you've got error reporting enabled.

确保您已启用错误报告。

C.

C。