PHP Session

In case of Cookie, the information is stored in user's browser while in caseof Session, information or data is stored on the server and the same is then associated through a short session ID string, called as SID. Session also overcomes many drawbacks related with the Cookies like Cookies are not much secure as using form data and query strings, any hacker can easily change a cookie's contents in order to destroy your application and even affect your hardware badly while in case of a session, SIDs generated by PHP are unique and impossible to guess or access and hence it is very difficult for a hacker to access and affect the session data. You can store more data in a session as compared to a cookie.

To create a session

Syntax

session_start();

To set the variables in a Session

Syntax

$_SESSION["variablename"] = "value";

Example

<?php
session_start();
$_SESSION["yourname"] = "John";
?> 

To display the value of variables in a Session

Syntax

echo $_SESSION["variablename"];

Example

<?php
session_start();
$_SESSION["yourname"] = "John";
echo $_SESSION["yourname"];
?> 

This will produce following result

John

To change the value of variables in a Session

Syntax:

$_SESSION["variablename"]="value1";
$_SESSION["variablename"]="value2";

Example

<?php
session_start();
$_SESSION["yourname"] = "John";
echo $_SESSION["yourname"];
$_SESSION["yourname"] = "Bob";
echo $_SESSION["yourname"];
?> 

This will produce following result

John
Bob

To remove all the variables in a Session

Syntax

session_unset()";

Example

<?php
session_start();
$_SESSION["yourname"] = "John";
session_unset();
?> 

To destroy a Session

Syntax

session_destroy();

Example

<?php
session_start();
$_SESSION["yourname"] = "John";
session_unset();
session_destroy();
?> 


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.