php 如何使用会话在php中存储变量

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

How to store a variable in php using session

php

提问by Asesha George

i want to store a value in to session using PHP

我想使用 PHP 将值存储到会话中

for example $id=10i want to store this value in a session

例如$id=10我想将此值存储在会话中

i tried

我试过

$pid= session_id($id);

echo $pid;

and

$pid = $_SESSION['$id'];   

but not working

但不工作

回答by Mubin

at the top of page

在页面顶部

session_start();

then to set sessionvariable

然后设置session变量

$_SESSION['id'] = $someID;

To retrieve the value of id

检索值 id

$pid = $_SESSION['id'];

Further more read more about session here

在此处进一步了解有关会话的更多信息

回答by Itz Raghu

Try this..

尝试这个..

<?php
session_start();

$id = 10;  //store 10 in id variable
$_SESSION['id'] = $id;  // now, store $id i.e, 10 in  Session variable named id. 

echo $_SESSION['id'];   // now, print the Session variable

回答by Faruque Ahamed Mollick

Here is the right code to store the variable in PHP session:

这是在 PHP 会话中存储变量的正确代码:

<?php
session_start();
$id = 10;
$_SESSION["user_id"] = $id;
?>

Now to get the session data:

现在获取会话数据:

<?php
session_start();
echo $_SESSION["user_id"];
?>

Also, I have write a complete guide on PHP session on one of my blog: How to start a PHP session, store and accessing Session data?

另外,我在我的一个博客上写了一篇关于 PHP 会话的完整指南:如何启动 PHP 会话,存储和访问会话数据?