Title: Lec08 : PHP
1Lec08 PHP Session
- ??? ?. ??????? ????????
- http//www.siam2dev.com
- ???????????????????????? ?????????????????????????
????????? - ???. ??????
2????????? session
- Session ??????????????? ??????
- ???????????????????????? session
??????????????????? session ??????????????????????
??? session ??????????????????????????????? - ?????????????? Cookie ??? Session
????????????????
??????? Cookie ????????????????
??????? Session ????????????????????????????????
3????????? session ???
- ?????????? session ???????????????????????????????
?????????? ???????????????????????????????????????
????????????? session PHP ??????????? session
????????????????????? session ???????? session ID
????????????????????????? ???? session ID
?????????????????? ??????????????????? ???
session ID ?????????????????????????? session ????
4????????? session ???
- ?????????????? Cookie ??? Session
????????????????
??????? Cookie ????????????????
??????? Session ????????????????????????????????
5???????????? Session
- Session ???????????????????????????
??????????????????????????????????????????????????
????? - ????????????????? session ????????????????????????
????? ????????????????????????????????
(?????????????????????????????????)
6Session ???????
- ??????????????????????????????????????????????????
?? ??? - ???????????????????????????? cookie
?????????????????????? ??????????????????????
??????????????????????????????????????????????????
?????????? - ????????????????????????????? ???????????????????
??????????????????????? ??????????????????????
?????????????????????????????????????????????
????? ????????? session ??????????????????????????
7What are sessions?
HTTP a stateless protocol
Client 1
Web server
Client 2
Joe
Mary
1
2
3
4
5
6
8Why do we need sessions?
A SESSION associates DATA with a USER for
duration of their entire visit
- CUSTOM web pages for different users(users can
log in to web database)
9How to store session info
1. Cookies Cookies store client-specific data on
the client
N.B. client may reject cookie!Security
issues?Max cookie size 4k
2. Session files
- Sessions store client-specific data on the server
- Sessions are tagged with a unique session id
10Cookies
- A cookie is a small piece of text containing
identifying information - Sent by server to browser on first interaction
- Sent by browser to the server that created the
cookie on further interactions - part of the HTTP protocol
- Server saves information about cookies it issued,
and can use it when serving a request - E.g., authentication information, and user
preferences
Cookies can be stored permanently or for a
limited time
11Session ids
Web server
Joe
Mary
Session ID
Session ID
12Session management
- PHP4 includes functions to
- manage session data on the server
- generate random session ID to identify user
- saves session ID
- either with a cookie (N.B. session ID only)or
in the query string
13???????????? session
- ??????????????? session ?????????????
-
- ????????
session_register(name)
name ?????????????? session ???????????????? ?????
???????????????????????? ???? boolean
???????????????????????? ????????????????????????
???????? session ?????????????????????
session_register(Mysession)
14??????????????? session
- ??????????????????? session ??????????????????????
?? session ???????????????????????????????????????
? session - ?????????????????????????????? session
?????????????? ???????????????????????
session_start() ????????????????????? session
????????????????? PHP ????????????????????????????
??? session ????
15??????????????? session
- ?????????????? PHP (PHP variable)
- ????????
HTTP_SESSION_VARSname
lt? session_register(myweb) HTTP_SESSION_VARS
myweb http//www.siam2dev.com print
HTTP_SESSION_VARSmyweb ?gt
16???????? session
Testsession1.php
lt? session_register(myweb) HTTP_SESSION_VARS
myweb http//www.siam2dev.com print
HTTP_SESSION_VARSmyweb ?gt
Testsession2.php
lt? session_start() Print Testsession2.phpltbrgt
print HTTP_SESSION_VARSmyweb ?gt
17??????????? session ??????
- ??????????????? session ??????????
??????????????????? session ??????
???????????????????????? ?????????????? - ????????
session_unregister(name)
name ?????????????? session ???????????????? ?????
???????????????????????? ???? boolean
???????????????????????? ????????????????????????
??????? session ?????????????????????
session_unregister(Mysession)
18??????????? session ??????
- ?????????????????? session ??????????????????
?????????????? - ?????????????????????????????? session
????????????????????? ?????????????????????????
???????????????????????????? session ??????
??????????????????????????
session_destroy( )
19???? session ??????????
- ??????????????? php.ini ??????????????????????????
???????? session ?????? - ???? php ????????????????????????????
session.save_path .
20????? ?????? php.ini
- ??????????????????????????????????? session
?????? ??????????????????? session_start()
?????????????????????? - ?????????????? session ?????????????????? cookie
???????????????? session ??????????????????????
????????
session.auto_start 1 ??????????? 0
session.cookie_lifetime ??????????
???????????? 0