简体   繁体   English

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

[英]How to store a variable in php using session

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但不工作

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'];

Further more read more about session here在此处进一步了解有关会话的更多信息

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

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 会话,存储和访问会话数据?

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

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