Session and cookies,get and post methods

640 views
434 views

Published on

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

No Downloads
Views
Total views
640
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Session and cookies,get and post methods

  1. 1. sanila.p sanila222@gmail.com www.facebook.com/username twitter.com/sanila92 linkedin.com/sanila session and cookies get and post methods
  2. 2. Introduction  Web appcation work on HTTPprotocol HTTP protocol is a stateless protocol It does not maintain the state between requests
  3. 3. cookies • A cookie is often used to identify a user. • A cookie is a small file that the server embeds on the user's computer. • Common use of cookies is to remember users between visits.
  4. 4. Uses of cookies • Identification of a user session, user's preferences. • Cookies can also be used for travelling of data from one page to another. • Shopping cart contents • Authentication
  5. 5. Ways of creating cookies
  6. 6. By using HttpCookies class HttpCookie StudentCookies = new HttpCookie("StudentCookies"); StudentCookies.Value = TextBox1.Text; StudentCookies.Expires = DateTime.Now.AddHours(1); Response.Cookies.Add(StudentCookies);
  7. 7. By using Response directly Response.Cookies["StudentCookies"].Value = TextBox1.Text; Response.Cookies["StudentCookies"].ExpiresDateTime.Now.AddDays(1) ;
  8. 8. multiple values in same cookie Response.Cookies["StudentCookies"]["RollNumber"] = TextBox1.Text; Response.Cookies["StudentCookies"]["FirstName"] = "Anu"; Response.Cookies["StudentCookies"]["LastName"] = "Menon"; Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);
  9. 9. How to Retrieve a Cookie Value? For httpcookie class and response directly method: string val = Request.Cookies["StudentCookies"].Value; For multiple cookies: string val; val = Request.Cookies["StudentCookies"]["RollNumber"]; val= val+ " " + Request.Cookies["StudentCookies"]["FirstName"]; val = val+ " " +Request.Cookies["StudentCookies"]["LastName"]; Label1.Text = val;
  10. 10. Example program: protected void Page_Load(object sender, EventArgs e) { HttpCookie cookie = Request.Cookies["cookie"]; if (cookie == null) { HttpCookie userCookie = new HttpCookie("cookie"); userCookie["Name"] = "sanila"; userCookie.Expires = DateTime.Now.AddSeconds(8); Response.Cookies.Add(userCookie); Label1.Text = "Cookie created at: " + DateTime.Now.ToString() + "<br /><br />"; } else { string name = cookie["Name"]; Label1.Text += "Welcome " + name; } } demo
  11. 11. Deleting cookies For deleting cookies: if (Request.Cookies["StudentCookies"] != null) { Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(-1); }
  12. 12. session Session serve the same purpose of cookies that is sessions are used to maintain the state in between requests
  13. 13. Creating session variable Session["FirstName"] = TextBox.Text; Session["LastName"] = TextBox1.Text;
  14. 14. How to receive session variable if (Session["UserName"] != null) { label1.Text = "Welcome : " + Session[“username"]; } else { label1.Text= “please check your username” ; }
  15. 15. Destroying the session itself • Session.clear(); //clears all the session variables • Session.Abandon(); //destroys the whole session
  16. 16. Example program
  17. 17. Difference between session and cookies cookies session cookies are stored in the user's browser Sessions are stored in server 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. A session is available as long as the browser is opened. Usercant disable the session. It will be destroyed if you close the browser Cookies can only store string Can store any object we can save cookie for future reference Session can’t be.
  18. 18. HTTP methods HTTP methods is used for send the data from client side to server side. GET METHOD POST METHOD
  19. 19. comparison GET METHOD POST METHOD Data will be arranged in HTTP header by appending to the URL as query string Data will be arranged in HTTP message body. Data is in query string so user can view the data Data is not shown to user Less secured compared to POST method because data is in query string so it will be saved in browser history and web server logs safer than GET method because data is not saved in history or web server logs. As data is saved in URL so its saves only 2048 bytes data We can used any amount of data and any format Hacking will be easy Hacking is difficult Only ASCII character data type allowed No restrictions. Allows binary data also
  20. 20. 21 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
  21. 21. Want to learn more about programming or Looking to become a good programmer? Are you wasting time on searching so many contents online? Do you want to learn things quickly? Tired of spending huge amount of money to become a Software professional? Do an online course @ baabtra.com We put industry standards to practice. Our structured, activity based courses are so designed to make a quick, good software professional out of anybody who holds a passion for coding.
  22. 22. Follow us @ twitter.com/baabtra Like us @ facebook.com/baabtra Subscribe to us @ youtube.com/baabtra Become a follower @ slideshare.net/BaabtraMentoringPartner Connect to us @ in.linkedin.com/in/baabtra Give a feedback @ massbaab.com/baabtra Thanks in advance www.baabtra.com | www.massbaab.com |www.baabte.com
  23. 23. 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 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com Contact Us

×