Professional Documents
Culture Documents
Session Cookie
Session Cookie
Session-Cookies
Prepared
9974846446(M)
Session-Cookies
operation, but it has been updated and this is the correct way to do it.
Example
<?php
session_start();
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>
Output:
Pageviews = 1
In this example we learned how to store a variable to the session associative
array $_SESSION and also how to retrieve data from that same array.
Q:-4 How to unregister session in php?
It is used to Free all session variables which are currently registered in our
application. session_unset function is used for removing all variables in a session.
For unsetting the session, it must be opened
Example:<?php
session_start();
$_SESSION["email"] = "vimalvaiwala@gmail.com";
echo "Your Email is ".$_SESSION['email'].".<br />";
#session delete
unset($_SESSION["email"]);
echo "After Session variable delete <br>";
#check Session variable after unset session
echo "Your Email Id is: ".$_SESSION["email"].".";
?>
$_SESSION["email"] = "vimalvaiwala@gmail.com";
session_destroy();
Prepared
9974846446(M)
?>
Session-Cookies
down at their computer for however long to do work on the site. When the
leave, the session ends.
Disadvantages:Performance overhead in case of large volume of user, because of session
data stored in server memory.
Overhead involved in serializing and De-Serializing session Data.
Q:-8 Explain Cookies with Example.
Prepared
9974846446(M)
Session-Cookies
A cookie is often used to identify a user. A cookie is a small file that the
server embeds on the user's computer. Each time the same computer
requests a page with a browser, it will send the cookie too. With PHP,
you can both create and retrieve cookie values.
How to Create a Cookie?
The setcookie() function is used to set a cookie.
Syntax
setcookie(name, value, expire);
1. name: The name of your cookie. You will use this name to later
retrieve your cookie, so don't forget it!
2. value: The value that is stored in your cookie. Common values are
username(string) and last visit(date).
3. expiration: The date when the cookie will expire and be deleted. If
you do not set this expiration date, then it will be treated as a session
cookie and be removed when the browser is restarted.
Example 1
In the example below, we will create a cookie named "user" and assign
the value "vimal" to it. We also specify that the cookie should expire
after one hour:
<?php
setcookie("user", "vimal", time()+3600);
?>
Example 2
You can also set the expiration time of the cookie in another
way. It may be easier than using seconds.
<?php
$expire=time()+60*60*24*30;
setcookie("user", "Alex Porter", $expire);
?>
Prepared
9974846446(M)
display it on a page:
Session-Cookies
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>
How to Delete a Cookie?
When deleting a cookie you should assure that the expiration date is in the
past.
<?php
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
?>
Prepared
9974846446(M)