0% found this document useful (0 votes)
34 views20 pages

PW-06 State Handling

The document discusses state handling in web programming using stateless protocols like HTTP. It explains that stateless protocols do not require the server to retain session information across multiple requests. It then discusses how state can be maintained across requests using cookies and sessions. Cookies are small files stored on the user's computer that can identify users or store variables. Sessions use a unique ID to identify users and store variables on the server. The document provides examples of using PHP functions like setcookie() and session_start() to work with cookies and sessions.

Uploaded by

Andika Nugraha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views20 pages

PW-06 State Handling

The document discusses state handling in web programming using stateless protocols like HTTP. It explains that stateless protocols do not require the server to retain session information across multiple requests. It then discusses how state can be maintained across requests using cookies and sessions. Cookies are small files stored on the user's computer that can identify users or store variables. Sessions use a unique ID to identify users and store variables on the server. The document provides examples of using PHP functions like setcookie() and session_start() to work with cookies and sessions.

Uploaded by

Andika Nugraha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Web Programming

State Handling

§ Aryo Pinandito, ST, M.MT


HTTP as Stateless
Protocol
Stateless Protocol
§ Stateless protocol treats each request as an
independent transaction that is unrelated to any
previous request
§ that the communication consists of independent pairs
of request and response.
§ Stateless protocol does not require the server to
retain session information or status about each
communications partner for the duration of
multiple requests.
Stateless Protocol Examples
§ Internet Protocol (IP)
§ foundation for the Internet

§ Hypertext Transfer Protocol (HTTP)


§ foundation of data communication for the World Wide
Web (WWW)
State and Session
§ Questions about user state:
§ How to keep facebook users keep logged in while
browsing friends profiles or other pages?
§ How to keep your shopping cart entries while you are
browsing another goods to add?
§ How to keep students previous question answers on an
online student examination system?
§ How do we keep user state?
§ Cookies
§ Session
Cookies
§ Cookie is a small file that the
server embeds on the user's
computer.
§ Cookie is often used to identify a
user (or user’s session).
§ Variables stored on a cookie is
read when users access a
website who own those cookie.
§ Web sites can usually only
modify their own cookies.
Cookies
§ Sets cookie using PHP setcookie() function
setcookie(name, [value], [expire], [path], [domain]);

§ Sets cookie example


variable
value
<?php
setcookie("user", "Aryo", time()+3600);
?>
variable expiry time in
name seconds
Retrieving Cookies
§ Retrieves cookies using $_COOKIE super global
array

$_COOKIE["cookie-name"];

<?php
$user = $_COOKIE["user"];
?>
Questions About Cookie

§ Is it secure?
§ Is it OK to put sensitive data into cookie?
§ How to secure it?
Session
Session
§ With session, users are allowed to store
information on the server for later use (i.e.
username, shopping item, question answer, etc)
§ Session information is stored temporarily on the
server and will be deleted if it is destroyed or after
the user has left the website for a specified time.
Session (2)
§ Sessions work by creating a unique id
(PHPSESSID) for each visitor and store variables
based on this PHPSESSID.
§ While variables contained in a session stored
securely on the server, this PHPSESSID value is
stored on the client computer as a cookie in order
to be able to keep track with the client, if cookies
are disabled, PHPSESSID value is stored in the
URL as a query string.
Using Session
§ Starting session
<?php session_start(); ?>
§ Storing session
<?php
session_start(); // don't forget!
$_SESSION['status']=1;
?>
§ Retrieving a session variable (in another PHP file)
<?php
session_start();
echo "User status " . $_SESSION['status'];
?>
Using Sessions
§ Removing one session variable
<?php
session_start(); // don't forget!
if(isset($_SESSION['status']))
unset($_SESSION['status']);
?>
§ Destroying the whole user’s session
<?php
session_destroy();
?>
Questions?

Questions?
Tugas
§ Kembangkan file form yang dibuat dengan
menambahkan input untuk upload file gambar.
§ Tampilkan isian yang dimasukkan ke dalam form
melalui sebuah file tampil.php dengan format
tampilan seperti yang disepakati di kelas.
§ Silakan "percantik" tampilan dengan
menggunakan CSS
Tugas
§ Simpan file form.html, tampil.php, dan *.css yang
digunakan ke dalam 1 file:
tugas-post-upload.zip
§ Kirim sebagai attachment email dengan subject:
PW: Tugas 4 - Tugas Post Upload
§ Kirim sebelum
Senin, 27 Maret 2017 pukul 20:00 WIB
§ Tugas ini adalah tugas perorangan.
Tugas A
§ Buatlah sebuah program berbasis web dengan
menggunakan PHP yang mencatat berapa kali
sebuah halaman web tertentu dibuka.
§ Buat sebuah program lain yang menampilkan
kapan saja halaman web tersebut dibuka.
§ Gunakan SESSION atau COOKIE.
Tugas B
§ Buatlah sebuah program berbasis web dengan
menggunakan PHP yang mencatat web browser
apa yang digunakan pengguna.
§ Tampilkan pesan ucapan selamat
pagi/siang/sore/malam sesuai kapan halaman
tersebut diakses beserta web browser yang
digunakan.
§ Gunakan SESSION atau COOKIE
Tugas A atau B
§ Subject Email: PW: Tugas 5 - State
§ Sebelum: 4 April 2017 02:00 WIB
§ Tugas ini adalah tugas maksimal per-2-orangan.
§ Kirimkan file:
§ PHP, HTML, dan CSS
§ Beserta screenshot-nya
§ Dalam 1 file ZIP: tugas-state.zip

You might also like