session and cookie

18
Mr.Warawut Mr.Warawut Khangkhan Khangkhan Mr.Warawut Mr.Warawut Khangkhan Khangkhan Facebook Facebook: : http://www.facebook.com/AjWarawut http://www.facebook.com/AjWarawut Twitter: Twitter: http://twitter.com/awarawut http://twitter.com/awarawut E-Mail: Mail: [email protected] [email protected] Mobile: 089 Mobile: 089-461 461-9591 9591

Upload: warawut

Post on 18-Nov-2014

680 views

Category:

Education


2 download

DESCRIPTION

ตรวจสอบข้อมูลด้วย Session และ Cookie

TRANSCRIPT

Page 1: Session and Cookie

Mr.WarawutMr.Warawut KhangkhanKhangkhanMr.WarawutMr.Warawut KhangkhanKhangkhanFacebookFacebook: : http://www.facebook.com/AjWarawuthttp://www.facebook.com/AjWarawut

Twitter: Twitter: http://twitter.com/awarawuthttp://twitter.com/awarawutEE--Mail: Mail: [email protected]@hotmail.com

Mobile: 089Mobile: 089--461461--95919591

Page 2: Session and Cookie

Mr.Warawut Khangkhan 2Chapter 16 Session & Cookie

Page 3: Session and Cookie

SessionSession� ���������� ��� PHP ������� version 4.0 ������

� ���������� ����� ���!���"�� ����# ���$%&��&

� Session ���������'�( PHP )*+��&��������

Mr.Warawut Khangkhan 3Chapter 16 Session & Cookie

Page 4: Session and Cookie

������������ ��������������� ��� sessionsession� session_start( )

����',��(��+�)*+��+��-&�� +�&���&��� (initialize) session������: session_start( )

� session_register( )� session_register( )����',��(��+�)*+��&��) �*����������&�� session �,!!��� (����& > 1 ������) ������: session_register(�0+������� [, �0+������� …])

������: �0+�������)*+� ��-&��',��(��+� session_register( ) ���&��* $ �1�-�&� �� ! �&���2���0+������� session ��&3�������0+��-��

single quote -�0� double quote

Mr.Warawut Khangkhan 4Chapter 16 Session & Cookie

Page 5: Session and Cookie

New document: New document: PHP

Save: Save: session_register1.php

Mr.Warawut Khangkhan 5Chapter 16 Session & Cookie

Page 6: Session and Cookie

New document: New document: PHP

Save: Save: session_register2.php

Mr.Warawut Khangkhan 6Chapter 16 Session & Cookie

Page 7: Session and Cookie

������������ ��������������� ��� sessionsession� session_unregister( )

����',��(��+�)*+��&���� ������) �*�������� session������: session_unregister( )

� session_destroy( )� session_destroy( )����',��(��+�)*+��&)1�����&�%�)���-���� session �,!!��� ������: session_destroy( )

Mr.Warawut Khangkhan 7Chapter 16 Session & Cookie

Page 8: Session and Cookie

�� ��������������� ����� ��������������� ��� SessionSession� ��� session_file1.php �����'��(�-&�����0+�$%&��& 4��*������

�&�����0+� username ������������� ��� session_file2.php )1�-�&�)*+��) �*�������� $username ��&

�� session �,!!��� 5�&�)����������������������� ������ $username (Global Variable) PHP

�� session �,!!��� 5�&�)����������������������� ������ $username �*������������4���� (Global Variable) )*+ PHP ��&��������-& 4��PHP ! �1����)*+���!���������&�����0+� username ���'�(session_file1.php ��1�-���-&���������*� (�������)1����������� -����PHP ���)6� <form> ��� HTML ����*+���� session)

� ��� session_file3.php ! ���������������� $username ���� �50+���������0������������� $username �����*�������%��� ��6���7������ ��&��&

Mr.Warawut Khangkhan Chapter 16 Session & Cookie 8

Page 9: Session and Cookie

New document: New document: PHP

Save: Save: session_file1.php

Mr.Warawut Khangkhan 9Chapter 16 Session & Cookie

Page 10: Session and Cookie

New document: New document: PHP

Save: Save: session_file2.php

Mr.Warawut Khangkhan 10Chapter 16 Session & Cookie

Page 11: Session and Cookie

New document: New document: PHP

Save: Save: session_file3.php

Mr.Warawut Khangkhan 11Chapter 16 Session & Cookie

Page 12: Session and Cookie

Mr.Warawut Khangkhan Chapter 16 Session & Cookie 12

Page 13: Session and Cookie

CookieCookie� -��"�� �&�%�)*+����������6��&�����0+�����$%&��& �50+��� 4���(��������!����

� ���$%&��&���� �� 4��-���!��)*+������ cookie ��������0+�����$%&��&��&�"&�-��cookie �����-����� (expire) �0+�$%&��&��*���%�'�( PHP �0+�# ������6�)��*��*�����'�( PHP )*+��&�� cookie ����� ����8��(�6! ��� cookie ����������*�����'�( PHP )*+��&�� cookie ����� ����8��(�6! ��� cookie ���������8 �('����( 8�+� PHP ! �1� PHP ��1�-�������������-&�������"���!�������&�����

Mr.Warawut Khangkhan Chapter 16 Session & Cookie 13

Page 14: Session and Cookie

������������� ���������������� ��� Session & CookieSession & Cookie������������� Cookie Session

���-����� ������%�������)*+����1�-����&�����&��-�0������+��-&-��������

������%���� �4��(�������8��()*+�1������&��� "&�� �4���("%��9������6�����+��-&-�������� "&�� �4���("%��9������6�������� ����� session

�����������&��������� �'�()*+��*����&! �&����%���4'�(����(��*��������'�()*+��&�����������������

��!1���� �5�� ������� �4��()*+��&���8(��&��

Mr.Warawut Khangkhan Chapter 16 Session & Cookie 14

Page 15: Session and Cookie

���� ��� ��������������� ��� ����������� CookieCookie� cookie )*+ web server �� web browser ������������ �����&�%�������

header ��� HTTP Protocol

� "&���*����&',��(��+� setcookie( ) ��-���',��(��+��0+�# ��� PHP )*+��&����&�%������ browser �6! �� ����$ �5������������ browser �6! �� ����$ �5�������

� � :*��&�� :�&�����*�',��(��+�)*+��&)1� output buffering ��& �50+��-&����1��&�%���5����&��-�������!1���+����� -�0��'�'��( (buffer) ���� ��&���������&�%��-������������ browser )*��*�������)&��

Mr.Warawut Khangkhan Chapter 16 Session & Cookie 15

Page 16: Session and Cookie

New document: New document: PHP

Save: Save: cookie_create.php

Mr.Warawut Khangkhan 16Chapter 16 Session & Cookie

Page 17: Session and Cookie

New document: New document: PHP

Save: Save: cookie_test.php

Mr.Warawut Khangkhan 17Chapter 16 Session & Cookie

Page 18: Session and Cookie

������������ ��������������� ��� Output BufferingOutput Buffering� ob_start( )

����',��(��+�)*+��9������&��� output buffering �� ��+��-&*���!���'�'��( ��& 4����� ���! ��*��',��(��+��*�)*+��)���������'�(

� ob_end_flush( )� ob_end_flush( )����',��(��+�)*+��&����&�%�!���'�'��(���������8��( �� �9������&��� output buffering 4����� �����*��',��(��+��*�)*+��)�����)&������'�(

Mr.Warawut Khangkhan Chapter 16 Session & Cookie 18