Sessions n cookies

404 views
302 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
404
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sessions n cookies

  1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  2. 2. Sessions, Cookies, Get and Post Jaseena A P jsnp65@gmail.com www.facebook.com/Jaseena Muhammed A P twitter.com/username in.linkedin.com/in/profilena me 9539443588
  3. 3. 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.
  4. 4. 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],[se cure]); This sets a cookie named "uname" - that expires after ten hours. <?php setcookie("uname", $name, time()+36000); ?> <html> <body> …
  5. 5. How to retrieve a cookie value? To access a cookie you just refer to the cookie name as a variable or use $_COOKIE array Tip: 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>
  6. 6. How to delete a cookie? It will expire or 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.
  7. 7. 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.
  8. 8. Using cookies Cookies 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.
  9. 9. SESSIONS Session 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.
  10. 10. How to create a session The session_start() function is used to create sessions. <?php session_start(); ?>
  11. 11. How to Retrieve a Session Value  Register 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']++; ?>
  12. 12. How to Delete a Session Value  session_unregister(´varname´);  How to destroy a session: session_destroy()
  13. 13. 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.
  14. 14. some notes regarding session variables A 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.
  15. 15. 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.
  16. 16. GET method GET 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.
  17. 17. 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>
  18. 18. EXAMPLE Example1.php <?php echo "welcome".$_GET['user']; ?>
  19. 19. EXAMPLE
  20. 20. EXAMPLE
  21. 21. POST method POST 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
  22. 22. 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>
  23. 23. EXAMPLE Example2.php <?php echo "welcome".$_POST['user']; ?>
  24. 24. EXAMPLE
  25. 25. EXAMPLE
  26. 26. THANK YOU
  27. 27. 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
  28. 28. 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

×