Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Q:-1 what is session?

Session-Cookies

A PHP session variable is used to store information about, or change


settings for a user session. Session variables hold information about one
single user, and are available to all pages in one application.
A PHP session solves this problem by allowing you to store user
information on the server for later use (i.e. username, shopping cart items,
etc). However, this session information is temporary and is usually deleted
very quickly after the user has left the website that uses sessions.
When you are working with an application, you open it, do some changes
and then you close it. This is much like a Session. The computer knows who
you are. It knows when you start the application and when you end. But on
the internet there is one problem: the web server does not know who you
are and what you do because the HTTP address doesn't maintain state.
A PHP session solves this problem by allowing you to store user
information on the server for later use (i.e. username, shopping items, etc).
However, session information is temporary and will be deleted after the user
has left the website. If you need a permanent storage you may want to store
the data in a database.
Sessions work by creating a unique id (UID) for each visitor and store
variables based on this UID. The UID is either stored in a cookie or is
propagated in the URL.
Q:-2 How to start Session?
Before you can begin storing user information in your PHP session, you must
first start the session. When you start a session, it must be at the very
beginning of your code, before any HTML or text is sent.
Below is a simple script that you should place at the beginning of your PHP
code to start up a PHP session.
Example:<?php
session_start(); // start up your PHP session!
?>

Q:-3 How to store a session variable?


When you want to store user data in a session use the $_SESSION
associative array. This is where you both store and retrieve session data. In
previous versions of PHP there were other ways to perform this store
D.C.Shah BCA College,Mandvi
By:- Vimal Vaiwala M.sc(I.T)

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"].".";
?>

Q:-5 How to destroy session in php?


It is used to Destroys all data registered to a session.
session_destroy() destroys all of the data associated with the current
session. It does not unset any of the global variables associated with the
session, or unset the session cookie. To use the session variables again,
session_start() has to be called.
Example:<?php
session_start();

$_SESSION["email"] = "vimalvaiwala@gmail.com";
session_destroy();

D.C.Shah BCA College,Mandvi


By:- Vimal Vaiwala M.sc(I.T)

Prepared
9974846446(M)

?>

Session-Cookies

Q:-6 Difference between session_unset and session_destroy in php.


session_unset just clears out the sesison for usage. The session is still on
the users computer. Note that by using session_unset, the variable still
exists.
Using session_unset in tandem with session_destroy however, is a much
more effective means of actually clearing out data. As stated in the example
above, this works very well, cross browser:
Q:-7Advantage and disadvantages of Session in php.
Advantages: As HTTP is stateless, PHP (and others) offer the ability to simulate a state
machine in your application through the use of a Session. If you did not do
this, you would have to use POST/GET between every page to keep the data
consistent, and if the user goes to another page on their own that data will
be lost! So without a SESSION, you wouldn't be able to have a user logged
in to your site .. at least not very consistently.
SESSION is used to keep data between multiple pages of your site without using
HTTP for an extended period of time.
Sessions should be just that -- the session that the user has when they sit

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.

D.C.Shah BCA College,Mandvi


By:- Vimal Vaiwala M.sc(I.T)

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);
?>

How to Retrieve a Cookie Value?


The PHP $_COOKIE variable is used to retrieve a cookie value.
In the example below, we retrieve the value of the cookie named "user" and
D.C.Shah BCA College,Mandvi
By:- Vimal Vaiwala M.sc(I.T)

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);
?>

D.C.Shah BCA College,Mandvi


By:- Vimal Vaiwala M.sc(I.T)

Prepared
9974846446(M)

You might also like