php and sessions. session – a general definition the general definition of a session in the...

23
PHP and Sessions

Upload: elinor-adams

Post on 17-Jan-2018

225 views

Category:

Documents


0 download

DESCRIPTION

Session – a web definition When we think about Sessions in terms of Web Systems we add to the general definition The interactions (requests and responses) that take place between 2 computers during a set period of time. PLUS, we typically store data (persistence) between these requests and response. This data will go away once the session is ended. Languages built for the web (or that are useful for the web) will have built-in code to handle Web Sessions

TRANSCRIPT

Page 1: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

PHP and Sessions

Page 2: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Session – a general definition

The GENERAL definition of a session in the “COMPUTER WORLD” is:

The interactions (requests and responses) that take place between 2 computers during a set period of time.

There are many kinds of sessions in the “Computer World” that even you have experienced. ssh / telnet session sftp session Session between your app and a server –like a Bank of

America App that communicates with the Bank server

Page 3: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Session – a web definition

When we think about Sessions in terms of Web Systems we add to the general definition

The interactions (requests and responses) that take place between 2 computers during a set period of time.

PLUS, we typically store data (persistence) between these requests and response. This data will go away once the session is ended.

Languages built for the web (or that are useful for the web) will have built-in code to handle Web Sessions

Page 4: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

PHP Sessions

In PHP, we have the ability to: Start a session Grab existing session Add data ‘to” a session Remove data “from” a session Set the lifetime of a session Destroy (kill/end) a session

A PHP session variable is used to store information about, or change settings for a user session.

Session variables hold information about one single user, and are available to all pages in one application.

Page 5: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

PHP Sessions

In PHP, we have a special pre-defined array we can use to store session data in: $_SESSION[]

This is an associative array (key to values) where $_SESSION[‘the_name’] is the data value

associated with the key ‘the_name’…..

Page 6: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

PHP Sessions

Remember our Session data holds information about one single user (client) during its session with another computer (server)

By default (though you can alter this), any php program served from the same Server and base URL has access to the same $_SESSION[] data.

So if you have a cart.php and a processorder.php both coming from you account in puzzle –they have access to the same $_SESSION[] data with the client invoking those php programs.

Page 7: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

PHP Sessions session_start() Before you can store user information in your

PHP session, you must first start up the session. NOTE: this function will create a new session if none

exists between the client and server OR if one exists will “grab” the session and populate $_SESSION[] array --- THIS IS DONE FOR YOU by the PHP interpreter/Apache server.

The session_start() function must appear BEFORE the <html> tag

Page 8: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

PHP Sessions

$_SESSION['views']=1;

if(isset($_SESSION['views']))$_SESSION['views']=$_SESSION['views']+1;else$_SESSION['views']=1;echo "Views=". $_SESSION['views'];

Page 9: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Example --- See our website

<?php

session_start();

//if session variable already exists then increment it by 1//else set to 1if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views']; ?>

Page 10: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Example --- See our website<?php

session_start();

// store session data $_SESSION['views'] = 1;

//retrieve data echo "Pageviews = ". $_SESSION['views'];

?>

Page 11: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Example 2 --- See our website – VIEWS Counter<?php

session_start();

//if session variable already exists then increment it by 1//else set to 1if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views']; ?>

Hit it 2 times

Hit it 3 times

Page 12: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

PHP Sessions – removing data unset($_SESSION['views']); The unset() function is used to free the

specified session variable

<?php session_start();

//removes session variable cart if it existsif(isset($_SESSION['cart'])) unset($_SESSION['cart']);

?>

Page 13: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

PHP Sessions --destroying (killing) session_destroy(); will reset your session and you will lose all your

stored session data.

<?php session_start();

//intermediate code//.....

//ready to destory sessionsession_destroy(); ?>

Page 14: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Another page visit example

Page 15: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

visit.php<?php

session_start();$current=time(); // look at the current timeif($_SESSION[last_click]) { $passed=$current-$_SESSION[‘last_click’]; $to_print.="$passed seconds have passed since your last visit.\n"; $_SESSION[‘last_click’]=$current;} else { $to_print="This is your first visit.\n"; $_SESSION[‘last_click’]=$current; }print "$top\n$to_print\n$bottom";?>

FIRST TIME:Your Visit StatusThis is your first visit.Thank you and please return

SECOND TIMEYour Visit Status 43 seconds have passed since your last visit. Thank you and please return

Page 16: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

YOU CAN ALSO STORE INFORMATION ON THE CLIENT CALLED COOKIES THAT IS AUTOMATICALLY SENT TO SERVER WHEN CLIENT RE-REQUESTS THAT SERVER

Did you know….

Page 17: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

This is how Amazon knows your name See it knows about “Behzad’s Amazon”

Page 18: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Cookies

Cookies are (name, value) pairs that are stored in the Client machine (in our case the client SW is a browser and it does this storing for you in a file) that is persistent –and it is returned to the Server everytime you go back to same URL/Server.

Page 19: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

cookies

A cookie is a piece of attribute/value data. A server can send cookies as value of a HTTP header Set-Cookie:. Multiple headers may be sent.

When the client visits the web site again, it will send the cookie back to the server with a HTTP header Cookie:

Page 20: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Set-Cookie Set-Cookie: name=value; [expires= date;]

[path=path;] [domain= domain] [secure] where

name= is the variable name set in the cookie value= is the variable's value date= is a date when the cookie expires path= restricts the cookie to be sent only when

requests to a path starting with path are made domain= restricts the sending of the cookie to a

certain domain secure restricts transmission to https

Page 21: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Cookies:

The browser compares the request it wants to make with the URL and the domain that sent the cookie.

If the path is not set the cookie will only be sent to a request with the originating URL.

If the cookie matches the request a request header of the form

Cookie: name1=value1 ; name2=value2 is sent.

Page 22: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

22

PHP and Cookies Cookies in PHP are fairly easy to use:

setcookie() function is called to create a cookie that will be sent to the client See http://php.net/manual/en/function.setcookie.php As always with cookies, they must be sent with the http

header Thus, you should determine and set any cookies in PHP

mode prior to using any html (or even simple text) $_COOKIE array contains the cookies received

back from the client machine Cookies sent to client by server previously Associative array allows access of cookies by name

Page 23: PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Confused --- Sessions and Cookies