0
Introduction to PHP
Web programming
Week 11, day2
Cookies
Cookies
• HTTP is a stateless protocol; this means that the web server does not know
(or care) whether two requests comes ...
Creating cookie
• setcookie(‚userid", "100", time() + 86400);
• This simply sets a cookie variable named ‚userid‛ with val...
Accessing Cookies
• echo $_COOKIE*’userid’+; // prints 100
• Cookie as array
– setcookie("test_cookie[0]", "foo");
– setco...
Destroying Cookies
• There is no special methods to destroy a cookie, We achieve it by
setting the cookie time into a past...
Sessions
Sessions
• Session serve the same purpose of cookies that is sessions are used
to maintain the state in between requests
•...
Creating and accessing session
• Once session is started you can create and access session variables
like any other arrays...
Destroying session
• There are two methods to destroy a session variable
1. Using unset() function
• Eg unsset($_SESSION*‘...
Comparison
 cookies are stored in the user's
browser
 A cookie can keep information in the
user's browser until deleted ...
HTTP Headers
Headers - Redirection
• The most common use of headers is to redirect the user to another
page. To do this,we use the Loca...
Questions?
‚A good question deserve a good grade…‛
End of day
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.co...
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, N...
Upcoming SlideShare
Loading in...5
×

Introduction to php web programming - sessions and cookies

825

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
825
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
74
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Introduction to php web programming - sessions and cookies"

  1. 1. Introduction to PHP Web programming Week 11, day2
  2. 2. Cookies
  3. 3. 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
  4. 4. 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.
  5. 5. 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’+);
  6. 6. 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);
  7. 7. Sessions
  8. 8. 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
  9. 9. 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
  10. 10. 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()
  11. 11. 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
  12. 12. HTTP Headers
  13. 13. 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.
  14. 14. Questions? ‚A good question deserve a good grade…‛
  15. 15. End of day
  16. 16. 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
  17. 17. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×