Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a variable in php using session

Tags:

php

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

like image 713
Asesha George Avatar asked Aug 03 '15 16:08

Asesha George


People also ask

How can we store variable in session in PHP?

Starting a PHP SessionSession variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session. The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.

How do you store values in a session?

The Session Storage basically consists of 4 main methods. setItem(key, value): This method is used to set the value into the Session Storage based on the key. getItem(key): This method is used to get the value that is stored into the Session Storage. It takes a key and returns the value.

How do I register a variable to a session?

We can create the session by writing session_start() and destroy the session by using session_destroy(). You can access the session variable by writing $_session[“name”]. Let us understand how the session works from the following examples. Example 1: In the following, you can create the session by entering the name.

Can I store object in session?

Yes, You can save the class objects/instance in session and access at other pages.


1 Answers

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

like image 189
Mubin Avatar answered Sep 20 '22 17:09

Mubin