session and cookies,get and post methods

24

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 20-Aug-2015

328 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Session and cookies,get and post methods
Page 2: Session and cookies,get and post methods

sanila.p [email protected]

www.facebook.com/username twitter.com/sanila92 linkedin.com/sanila

session and cookiesget and post methods

Page 3: Session and cookies,get and post methods

Introduction

Web appcation work on HTTPprotocolHTTP protocol is a stateless protocolIt does not maintain the state between

requests

Page 4: Session and cookies,get and post methods

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.

Page 5: Session and cookies,get and post methods

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

Page 6: Session and cookies,get and post methods

Ways of creating cookies

Page 7: Session and cookies,get and post methods

By using HttpCookies class

HttpCookie StudentCookies = new HttpCookie("StudentCookies"); StudentCookies.Value = TextBox1.Text; StudentCookies.Expires = DateTime.Now.AddHours(1);

Response.Cookies.Add(StudentCookies);

Page 8: Session and cookies,get and post methods

By using Response directly

Response.Cookies["StudentCookies"].Value = TextBox1.Text; Response.Cookies["StudentCookies"].ExpiresDateTime.Now.AddDays(1);

Page 9: Session and cookies,get and post methods

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);

Page 10: Session and cookies,get and post methods

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;

Page 11: Session and cookies,get and post methods

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

Page 12: Session and cookies,get and post methods

Deleting cookies

For deleting cookies:

if (Request.Cookies["StudentCookies"] != null) { Response.Cookies["StudentCookies"].Expires =

DateTime.Now.AddDays(-1); }

Page 13: Session and cookies,get and post methods

session

Session serve the same purpose of cookies that is sessions are used to maintain the state in between requests

Page 14: Session and cookies,get and post methods

Creating session variable

Session["FirstName"] = TextBox.Text; Session["LastName"] = TextBox1.Text;

Page 15: Session and cookies,get and post methods

How to receive session variable

if (Session["UserName"] != null) {

label1.Text = "Welcome : " + Session[“username"]; } else { label1.Text= “please check your username” ; }

Page 16: Session and cookies,get and post methods

Destroying the session itself

• Session.clear(); //clears all the session variables

• Session.Abandon(); //destroys the whole session

Page 17: Session and cookies,get and post methods

Example program

Page 18: Session and cookies,get and post methods

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.

Page 19: Session and cookies,get and post methods

HTTP methods

HTTP methods is used for send the data from client side to server side.

GET METHODPOST METHOD

Page 20: Session and cookies,get and post methods

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

Page 21: Session and cookies,get and post methods

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

Page 22: Session and cookies,get and post methods

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.

Page 23: Session and cookies,get and post methods

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

Page 24: Session and cookies,get and post methods

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