Date post: | 20-Aug-2015 |
Category: |
Technology |
Upload: | baabtracom-no-1-supplier-of-quality-freshers |
View: | 325 times |
Download: | 4 times |
sanila.p [email protected]
www.facebook.com/username twitter.com/sanila92 linkedin.com/sanila
session and cookiesget and post methods
Introduction
Web appcation work on HTTPprotocolHTTP protocol is a stateless protocolIt does not maintain the state between
requests
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.
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
Ways of creating cookies
By using HttpCookies class
HttpCookie StudentCookies = new HttpCookie("StudentCookies"); StudentCookies.Value = TextBox1.Text; StudentCookies.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(StudentCookies);
By using Response directly
Response.Cookies["StudentCookies"].Value = TextBox1.Text; Response.Cookies["StudentCookies"].ExpiresDateTime.Now.AddDays(1);
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);
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;
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
Deleting cookies
For deleting cookies:
if (Request.Cookies["StudentCookies"] != null) { Response.Cookies["StudentCookies"].Expires =
DateTime.Now.AddDays(-1); }
session
Session serve the same purpose of cookies that is sessions are used to maintain the state in between requests
Creating session variable
Session["FirstName"] = TextBox.Text; Session["LastName"] = TextBox1.Text;
How to receive session variable
if (Session["UserName"] != null) {
label1.Text = "Welcome : " + Session[“username"]; } else { label1.Text= “please check your username” ; }
Destroying the session itself
• Session.clear(); //clears all the session variables
• Session.Abandon(); //destroys the whole session
Example program
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.
HTTP methods
HTTP methods is used for send the data from client side to server side.
GET METHODPOST METHOD
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
21
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
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.
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
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
Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode
Start up VillageEranakulam,Kerala, India.
Email: [email protected]
Contact Us