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
How to store a variable in php using session
提问by Asesha George
i want to store a value in to session using PHP
我想使用 PHP 将值存储到会话中
for example $id=10
i 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 session
variable
然后设置session
变量
$_SESSION['id'] = $someID;
To retrieve the value of id
检索值 id
$pid = $_SESSION['id'];
回答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 会话,存储和访问会话数据?