sessions n cookies

30

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 20-Aug-2015

181 views

Category:

Technology


0 download

TRANSCRIPT

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Sessions, Cookies, Get and Post

Jaseena A [email protected]/Jaseena

Muhammed A Ptwitter.com/usernamein.linkedin.com/in/profilename9539443588

What is a cookie?Cookies are a mechanism for storing data in the

remote browser and thus tracking or identifying users.

A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests for 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 create cookies.Note: The setcookie() function must appear BEFORE

the <html> tag.

setcookie(name, [value], [expire], [path], [domain],[secure]);

This sets a cookie named "uname" - that expires after ten hours.

<?php setcookie("uname", $name, time()+36000); ?><html> <body> …

How to retrieve a cookie value?

To access a cookie you just refer to the cookie name as a variable or use $_COOKIE arrayTip: Use the isset() function to find out if a cookie has been set.

<html> <body><?php

if (isset($uname)) echo "Welcome " . $uname . "!<br />";

else echo "You are not logged in!<br />"; ?>

</body> </html>

How to delete a cookie?

It will expireor

Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string (""), and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client.

Using cookies Cookies are small pieces of data that a server sends

to a browser for storage. When a browser contacts a server, it sends along any cookies for that server under the variable $_COOKIES. Similarly, a server can set one or more cookies on the browser for retrieval at a later time.

Using cookiesCookies are sent with Web page headers, so any setting

of cookies must take place BEFORE the DOCTYPE line in an HTML/PHP script.

PHP function setcookie specifies a cookie ID, a value, and a length of time for which the cookie will be kept by the browser.

PHP variable $_COOKIE is an associative array that maintains the list of cookies set previously.

SESSIONSSession support in PHP consists of a way to preserve

certain data across subsequent accesses. This enables us to build more customized applications.

The session support allows us to register arbitrary numbers of variables to be preserved across requests.

A visitor accessing our web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

How to create a session

The session_start() function is used to create sessions.

<?php session_start(); ?>

How to Retrieve a Session ValueRegister Session variable

session_register('var1','var2',...); // will also create a session

PS:Session variable will be created on using even if you will not register it!

Use it<?php session_start();

if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;

else $_SESSION['count']++;

?>

How to Delete a Session Value

session_unregister(´varname´);

How to destroy a session: session_destroy()

Session Variables

Effectively, session variables are cookies that remain active only while the browser is actively interacting with the server. When time elapses, or when we close our browser, the session variables disappear.

some notes regarding session variablesA script uses session_start() to initialize and register any

session variables.Session variables are sent with Web page headers, so any

setting of session information must take place before the DOCTYPE tag.

PHP variable $_SESSION is an associative array that maintains the list of session variables set previously.

PHP function isset determines whether a specific $_SESSION field has a designated value.

PHP function unset removes a session value that was sent previously, and session_unset() removes all session values.

GET&POST methods Using the GET and POST methods , the browser

client can send data to the web server.

PHP the GET and POST methods are used to retrieve information from forms , such as user input.

These methods are used data handling forms.

GET methodGET Method is less secure because information sent

from a form with the GET method is visible to every one.

The GET Method has limits on the amount of information to send because URL lengths are limited.

The browser appends the data to the URL.The GET method can’t be used to send binary

data,like images or Word documents,to the server In the GET method page and encoded information are

separated by the question mark (?) sign.

EXAMPLE<html><body><form name="form1" method="get" action="example1.php"><table border="0"><tr><td>username:</td><td><input type="text" name="user" size="15"></td></tr><tr><td>password:</td><td><input type="password" name="pass" size="10"></td></tr><tr><td><input type="submit" value=login></td></tr></table></form></body></html>

EXAMPLE

Example1.php

<?php echo "welcome".$_GET['user'];?>

EXAMPLE

EXAMPLE

POST methodPOST Method is Secure because information sent from

a form with the POST method is invisible to every one.

The POST Method has no limits on the amount of information to send because URL lengths are unlimited.

The browser doesn’t append the data to the URL.

The POST method can be used to send binary data,like images or Word documents,to the server

EXAMPLE<html><body><form name="form1" method="post" action="example2.php"><table border="0"><tr><td>username:</td><td><input type="text" name="user" size="15"></td></tr><tr><td>password:</td><td><input type="password" name="pass" size="10"></td></tr><tr><td><input type="submit" value=login></td></tr></table></form></body></html>

EXAMPLE

Example2.php

<?php echo "welcome".$_POST['user'];?>

EXAMPLE

EXAMPLE

THANK YOU

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]