0% found this document useful (0 votes)
84 views3 pages

III B.SC Wad Using PHP & Mysql 13 Unit 4 Sem 5 Final

Uploaded by

mkarthikgcp1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views3 pages

III B.SC Wad Using PHP & Mysql 13 Unit 4 Sem 5 Final

Uploaded by

mkarthikgcp1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

III B.

Sc Honours Semester - V Web Application Development using PHP and MYSQL


UNIT – III
Working with Cookies and User Sessions: Introducing Cookies, setting a Cookie
with PHP, Session Function Overview, starting a Session, working with session
variables, passing session IDs in the Query String, Destroying Sessions and
Unsetting Variables, Using Sessions in an Environment with Registered Users.
**********
1. Explain about working with cookies.
PHP cookie is a small piece of information which is stored at client browser. It
is used to recognize the user. It can up to 20 cookies be stored by a user’s browser.
Cookie is created at server side and saved to client browser. Each time when
client sends request to the server, cookie is embedded with request. Such way,
cookie can be received at the server side.

g
or
s.
te
In short, cookie can be created, sent and received at server end.
da

Setting Cookies with PHP:


PHP provided setcookie() function to set a cookie. This function requires upto
up

six arguments and should be called before <html> tag. For each cookie this function
has to be called separately.
nu

setcookie(name, value, expire, path, domain, security);


.a

Here is the detail of all the arguments −


 Name − This sets the name of the cookie and is stored in an environment
w

variable called HTTP_COOKIE_VARS. This variable is used while accessing


w

cookies.
 Value − This sets the value of the named variable and is the content that you
w

actually want to store.


 Expiry − This specify a future time in seconds since 00:00:00 GMT on 1st Jan
1970. After this time cookie will become inaccessible. If this parameter is not set
then cookie will automatically expire when the Web Browser is closed.
 Path − This specifies the directories for which the cookie is valid. A single
forward slash character permits the cookie to be valid for all directories.
 Domain − This can be used to specify the domain name in very large domains
and must contain at least two periods to be valid. All cookies are only valid for
the host and domain which created them.
 Security − This can be set to 1 to specify that the cookie should only be sent by
secure transmission using HTTPS otherwise set to 0 which mean cookie can be
sent by regular HTTP.

Prepared by Nageswara Rao Kadiri 1


Department of Computer Science, TJPS College, Guntur.
III B.Sc Honours Semester - V Web Application Development using PHP and MYSQL
PHP Delete Cookie: -
If you set the expiration date in past, cookie will be deleted.
File: cookie1.php
<?php
setcookie ("CookieName", "", time() - 3600);// set the expiration date to one hour ago
?>
----------------------XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-------------------------

2. Explain about user sessions.


 PHP session is used to store and pass information from one page to another
temporarily (until user close the website).
 PHP session technique is widely used in shopping websites where we need to
store and pass cart information e.g. username, product code, product name,
product price etc from one page to another.
 PHP session creates unique user id for each browser to recognize the user and
avoid conflict between multiple browsers.

Starting a PHP Session and working with session variables, passing session

g
IDs in the Query String:

or
A PHP session is easily started by making a call to the session_start()
function. This function first checks if a session is already started and if none is

s.
started then it starts one. It is recommended to put the call to session_start() at
te
the beginning of the page.
da

<?php
session_start();
up

if( isset( $_SESSION['counter'] ) ) {


nu

$_SESSION['counter'] += 1; //Session variable


}
.a

else {
$_SESSION['counter'] = 1; //session variable
w

}
w

$msg = "You have visited this page ". $_SESSION['counter'];


w

$msg .= "in this session.";


?>

<html>
<head>
<title>Setting up a PHP session</title>
</head>

<body>
<?php
echo ( $msg );
?>
</body>
</html>

Prepared by Nageswara Rao Kadiri 2


Department of Computer Science, TJPS College, Guntur.
III B.Sc Honours Semester - V Web Application Development using PHP and MYSQL
Destroying a PHP Session and Unsetting variables:
A PHP session can be destroyed by session_destroy() function. This function
does not need any argument and a single call can destroy all the session variables. If
you want to destroy a single session variable then you can use unset() function to
unset a session variable.

Here is the example to unset a single variable −


<?php
unset($_SESSION['counter']);
?>
Here is the call which will destroy all the session variables −
<?php
session_destroy();
?>
-------------------------------XXXXXXXXXXXXXXXXXXXXX---------------------
3. Explain the Using Sessions in an Environment with Registered Users.
1. Working with Registered Users:
The process usually involves a registration form, where the user creates a

g
username and password and completes an identification profile. From that point forward, each

or
time a registered user logs in to the system, you can grab the user’s identification information
and store it in the user’s session.

s.
2. Working with User Preferences:
If you are feeling adventurous in the design phase of a user-based application, you
te
might build a system in which registered users can set specific preferences that affect the way
they view your site. For example, you might allow your users to select from a predetermined
da

color scheme, font type and size, and so forth.


up

**********
The Following are Important Questions from UNIT-4
nu

1. Explain about working with cookies.


.a

2. Explain about user sessions.


w

3. Explain the Using Sessions in an Environment with Registered Users.


w

**********
w

Prepared by Nageswara Rao Kadiri 3


Department of Computer Science, TJPS College, Guntur.

You might also like