简体   繁体   中英

How to store a variable in php using session

i want to store a value in to session using PHP

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

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['id'] = $someID;

To retrieve the value of 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
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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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