top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to create a session in PHP? How to set a value and remove data from a session?

+4 votes
How to create a session in PHP? How to set a value and remove data from a session?
posted Jan 13, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

$_SESSION is a special array used to store information across the page requests a user makes during his visit to your website or web application.

Starting a Session and Storing a value in Session
Before you can to store information in a session, you have to start PHP’s session handling. To start the session session_start() is used -

// start them engines!
// store session data
$_SESSION["username"] = "QueryHome";

session_start() starts the session between the user and the server, and allows values stored in $_SESSION to be accessible in other scripts later on.

Retrieving a Session Variable
You call session_start() again which continues the session in another file and you can then retrieve values from $_SESSION.

// continue the session
// retrieve session data
echo "Username = " . $_SESSION["username"];

Ending a Session Variable
To delete a single session value, you use the unset() function:

// delete the username value

Ending all the session variable
To unset all of the session’s values, you can use the session_unset() function:

// delete all session values
answer Jan 16, 2016 by Vrije Mani Upadhyay
Similar Questions
+1 vote

I am trying to figure out if I can run my sessionpersistence script that I run with Apache to see if sessions persist between two tomcat sites on two separate servers. The load balancer has session persistence enabled but Iam unsure how to test it. My thought was that I would just add the following php script to check, but apparently I am doing something wrong. The script is this:

but it seems not to like it if I add it from the webapps directory or from inside a directory under that. Is there some special trick to running php from inside tomcat ? I have tomcat set up the same on both servers and am fronting it with Apache if that adds options for me.

0 votes

I am having trouble with session vars. I'm trying to implement the credit card direct pay method outlined here...

Basically, page 1 is my form that goes outside my site to the cc gateway company then comes back with a result... (PG2)

Problem: if I try to create session vars on page 1 - they don't work on page 2.

Am I correct in thinking that when this process leaves my site and goes to the gateway, then returns, it is similar to creating a new session and that is why the session vars don't remain active?

0 votes

I want to write online user module for my site and I want to check $_SESSION['userID'] to find all users id who loged in but when I echo this code its return only current user detail how I can see all sessions?

foreach($_SESSION as $k => $v)
echo $k."----------".$v;

or how I handle online users?