the truth about sessions -...

25
The Truth about Sessions 1 Chris Shiflett Principal, OmniTI [email protected]

Upload: others

Post on 04-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

The Truth about Sessions

1

Chris ShiflettPrincipal, OmniTI

[email protected]

Page 2: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Talk Outline

‣ HTTP and Statelessness

‣ GET, POST, and Cookies

‣ PHP Sessions

‣ Debugging Sessions

‣ Session Security

‣ Questions and Answers

2

Page 3: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

HTTP

3

Server

HTTP Request

HTTP ResponseClient

Hypertext Transfer Protocol

Page 4: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

HTTP Request

4

GET / HTTP/1.1Host: shiflett.org

POST /post.php HTTP/1.1Host: shiflett.orgContent-Type: application/x-www-form-urlencodedContent-Length: 7

foo=bar

Page 5: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

HTTP Response

5

HTTP/1.1 200 OKDate: Thu, 15 Mar 2007 14:30:00 GMTServer: ApacheContent-Type: text/html; charset=UTF-8Content-Length: 35

<html><p>Hello, World!</p></html>

Page 6: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Statelessness

6

"HTTP is a stateless protocol."

Remember Me?

Nope.Client Server

Page 7: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

GET Data

7

GET /next.php?id=1234 HTTP/1.1Host: shiflett.org

<?php $_GET['id'] = '1234'; ?>

Page 8: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

GET Data

8

<?php output_add_rewrite_var('id', '1234'); ?>

<a href="next.php">Next Page</a>

<a href="next.php?id=1234">Next Page</a>

Page 9: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

POST Data

9

POST /next.php HTTP/1.1Host: shiflett.orgContent-Type: application/x-www-form-urlencodedContent-Length: 7

id=1234

<?php $_POST['id'] = '1234'; ?>

Page 10: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Cookies

10

"HTTP State Management Mechanism"

‣ RFC 2965

‣ RFC 2109

‣ "Persistent Client State HTTP Cookies"http://wp.netscape.com/newsref/std/cookie_spec.html

Page 11: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Cookies

11

bool setcookie ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly]]]]]] )

<?php setcookie('id', '1234'); ?>

Page 12: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Cookies

12

void header ( string $string [, bool $replace [, int $http_response_code]] )

<?php header('Set-Cookie: id=1234'); ?>

Page 13: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Cookies

13

Server

HTTP Request

HTTP Response + Set-Cookie

HTTP Request + CookieClient

HTTP Response

Page 14: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

14

session_start();

Page 15: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

PHP Sessions

15

$_SESSION['name'] = 'Chris';

echo $_SESSION['name'];/* Chris */

"That was easy. Almost too easy."

Page 16: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Debugging PHP Sessions

16

<?php session_set_save_handler('_open', '_close', '_read', '_write', '_destroy', '_clean'); ?>

Page 17: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Debugging PHP Sessions

17

<?php function _open(){ echo '<p>_open()</p>'; /* ... */} ?>

Page 18: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Debugging PHP Sessions

18

The "write" handler is not executed until after the output stream is closed.

– http://php.net/session_set_save_handler

Page 19: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Debugging PHP Sessions

19

<?php function _open(){ error_log("_open()\n", 3, '/tmp/debug'); /* ... */} ?>

Page 20: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Debugging PHP Sessions

20

_open()_read(058c616b8ed35c16ccaf97fd1e1ba395)_write(058c616b8ed35c16ccaf97fd1e1ba395, name|s:5:"Chris";)_close()

<?php include '../inc/session.inc'; $_SESSION['name'] = 'Chris'; ?>

Page 21: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Debugging PHP Sessions

21

<?php header('Location: http://example.org/');session_write_close(); ?>

Page 22: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Session Security

22

http://example.org/index.php?PHPSESSID=1234

Page 23: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Session Security

23

<?php /* $_SESSION['auth'] = FALSE; */ if (auth($_POST['username'], $_POST['password'])) { $_SESSION['auth'] = TRUE; session_regenerate_id(TRUE);} ?>

Page 24: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Session Security

24

Server

HTTP Request

HTTP Response +Set-Cookie: PHPSESSID=1234

HTTP Request +Cookie: PHPSESSID=1234

GoodGuy

HTTP ResponseBadGuy

Page 25: The Truth about Sessions - conf.phpquebec.comconf.phpquebec.com/slides/2007/the-truth-about-sessions.pdf · Talk Outline ‣ HTTP and Statelessness ‣ GET, POST, and Cookies ‣

Thanks for Listening!

25

‣ http://shiflett.org/

‣ http://omniti.com/