How to store a variable in php using session

Solution 1:

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

Solution 2:

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?