Before going to know about the sessions and cookies lets see the difference between $_GET,$_POST , $_SESSION, $_COOKIE.
As we have already discussed about the $_GET and $_POST from this link http://phptag.blogspot.com/2011/11/difference-between-get-post-and-request.html
As it says that the data will be carried through GET and POST methods from one page to another page.
For example if we have abc.php, xyz.php and pqr.php , lets assume that abc.php has a form with input elements. Then if we need to pass that data to xyz.php we can pass it through URL get method or POST method and again if we want to pass the data in xyz.php to pqr.php we have to pass using GET and POST methods.
SESSIONS
Lets see how it works if we use the $_SESSION
In abc.php if we can set the session like this
<?php
session_start();
$_SESSION['name']='phptag';
?>
Then we can directly access $_SESSION['name'] variable in any php page with in the application by starting with session_start() like this
<?php
session_start();
echo $_SESSION['phptag'];
?>
we can delete the $_SESSION['name'] by using unset() and session_destroy() function like this
<?php
unset($_SESSION['name']);
//or
session_destroy(); //Finally, destroy the session.
?>
We can also set the session time limit in php.ini file or by using ini_set() function dynamically.
COOKIES
Cookies are always set in the client side for example Firefox, IE, Chrome and Opera etc...
That means whenever we set the cookie it will set in the browser for a particular time period.
How to set the cookie:
we can set th cookie using setcookie() function like this
<?php
$value = 'phptag';
setcookie("name", $value, time()+3600);
// name - cookie name
// $value - value that need to set
// time()+3600 - that means it will expire in 1 hour.
?>
How to access the cookie:
<?php
// It will print "phptag";
echo $_COOKIE['name'];
?>
How to delete the cookie:
<?php
setcookie ("name", "", time() - 3600);
// name - cookie name
// $value - should be empty
// time()-3600 - we should give expiration date is in the past
?>
As we have already discussed about the $_GET and $_POST from this link http://phptag.blogspot.com/2011/11/difference-between-get-post-and-request.html
As it says that the data will be carried through GET and POST methods from one page to another page.
For example if we have abc.php, xyz.php and pqr.php , lets assume that abc.php has a form with input elements. Then if we need to pass that data to xyz.php we can pass it through URL get method or POST method and again if we want to pass the data in xyz.php to pqr.php we have to pass using GET and POST methods.
SESSIONS
Lets see how it works if we use the $_SESSION
In abc.php if we can set the session like this
<?php
session_start();
$_SESSION['name']='phptag';
?>
Then we can directly access $_SESSION['name'] variable in any php page with in the application by starting with session_start() like this
<?php
session_start();
echo $_SESSION['phptag'];
?>
we can delete the $_SESSION['name'] by using unset() and session_destroy() function like this
<?php
unset($_SESSION['name']);
//or
session_destroy(); //Finally, destroy the session.
?>
We can also set the session time limit in php.ini file or by using ini_set() function dynamically.
COOKIES
Cookies are always set in the client side for example Firefox, IE, Chrome and Opera etc...
That means whenever we set the cookie it will set in the browser for a particular time period.
How to set the cookie:
we can set th cookie using setcookie() function like this
<?php
$value = 'phptag';
setcookie("name", $value, time()+3600);
// name - cookie name
// $value - value that need to set
// time()+3600 - that means it will expire in 1 hour.
?>
How to access the cookie:
<?php
// It will print "phptag";
echo $_COOKIE['name'];
?>
How to delete the cookie:
<?php
setcookie ("name", "", time() - 3600);
// name - cookie name
// $value - should be empty
// time()-3600 - we should give expiration date is in the past
?>
No comments:
Post a Comment