+ All Categories
Home > Technology > Introduction to php web programming - sessions and cookies

Introduction to php web programming - sessions and cookies

Date post: 18-Nov-2014
Category:
Upload: baabtracom-mentoring-partner-first-programming-school-in-india
View: 555 times
Download: 2 times
Share this document with a friend
Description:
 
17
Introduction to PHP Web programming Week 11, day2
Transcript
Page 1: Introduction to php   web programming - sessions and cookies

Introduction to PHPWeb programming

Week 11, day2

Page 2: Introduction to php   web programming - sessions and cookies

Cookies

Page 3: Introduction to php   web programming - sessions and cookies

Cookies

• HTTP is a stateless protocol; this means that the web server does not know

(or care) whether two requests comes from the same user or not; it just

handles each request without regard to the context in which it happens.

• Cookies are used to maintain the state in between requests—even when

they occur at large time intervals from each other.

• Cookies allow your applications to store a small amount of textual data

(typically,4-6kB) on a Web client browser.

• There are a number of possible uses for cookies, although their most

common one is maintaining state of a user

Page 4: Introduction to php   web programming - sessions and cookies

Creating cookie

• setcookie(‚userid", "100", time() + 86400);

• This simply sets a cookie variable named ‚userid‛ with value ‚100‛

and this variable value will be available till next 86400 seconds

from current time

Cookie variable name

variable value

Expiration time.

Page 5: Introduction to php   web programming - sessions and cookies

Accessing Cookies

• echo $_COOKIE*’userid’+; // prints 100

• Cookie as array

– setcookie("test_cookie[0]", "foo");

– setcookie("test_cookie[1]", "bar");

– setcookie("test_cookie[2]", "bar");

• var_dump($_COOKIE*‘test_cookie’+);

Page 6: Introduction to php   web programming - sessions and cookies

Destroying Cookies

• There is no special methods to destroy a cookie, We achieve it by

setting the cookie time into a past time so that it destroys it

– Eg : setcookie(‘userid’,100,time()-100);

Page 7: Introduction to php   web programming - sessions and cookies

Sessions

Page 8: Introduction to php   web programming - sessions and cookies

Sessions

• Session serve the same purpose of cookies that is sessions are used

to maintain the state in between requests

• Session can be started in two ways in PHP

1. By changing the session.auto_start configuration setting in php.ini

2. Calling session_start() on the beginning of each pages wherever you

use session(Most common way)

Note: session_start() must be called before any output is sent to the browser

Page 9: Introduction to php   web programming - sessions and cookies

Creating and accessing session

• Once session is started you can create and access session variables

like any other arrays in PHP

– $_SESSION[‘userid’+ = 100;

– echo $_SESSION*‘userid’+; //prints 100

Session variable name variable value

Page 10: Introduction to php   web programming - sessions and cookies

Destroying session

• There are two methods to destroy a session variable

1. Using unset() function

• Eg unsset($_SESSION*‘userid’+)

2. Calling session_destroy() method. This will effectively destroy all the

session variables. So for deleting only one variable you should go for

the previous method

• Session_destroy()

Page 11: Introduction to php   web programming - sessions and cookies

Comparison

cookies are stored in the user's

browser

A cookie can keep information in the

user's browser until deleted by user or set

as per the timer. It will not be destroyed

even if you close the browser.

Cookies can only store string

we can save cookie for future reference

Sessions are stored in server

A session is available as long as the

browser is opened. User cant disable the

session. It will be destroyed if you close the

browser

Can store not only strings but also

objects

session cant be.

Cookies Session

Page 12: Introduction to php   web programming - sessions and cookies

HTTP Headers

Page 13: Introduction to php   web programming - sessions and cookies

Headers - Redirection

• The most common use of headers is to redirect the user to another

page. To do this,we use the Location header:

Eg: header("Location: http://phparch.com");

• header() must be called before any other output, including any

whitespace characters outside of PHP tags, as well as all HTML

data.

• If you fail to abide by this rule, two things will happen: your

header will have no effect, and PHP may output an error.

Page 14: Introduction to php   web programming - sessions and cookies

Questions?

‚A good question deserve a good grade…‛

Page 15: Introduction to php   web programming - sessions and cookies

End of day

Page 16: Introduction to php   web programming - sessions and cookies

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

Page 17: Introduction to php   web programming - sessions and cookies

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]


Recommended