0% found this document useful (0 votes)
2 views6 pages

PHP Cookies Sessions Serialization

Webt tech and python

Uploaded by

maneetrathee98
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)
2 views6 pages

PHP Cookies Sessions Serialization

Webt tech and python

Uploaded by

maneetrathee98
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/ 6

PHP Cookies Sessions Serialization

Sure! Here's a detailed but easy-to-understand set of notes on Cookies, Serialization, and
Session State in PHP and MySQL for your Web Technology exam:

Web Technology: Cookies, Serialization,


and Session State in PHP & MySQL

1. Cookies

What are Cookies?


Cookies are small text files stored on the user's browser by the web server.

They help websites remember information about the user between different pages or
visits.

Example: Remembering login info, user preferences, shopping cart items.

How Cookies Work in PHP?


PHP can create, read, and delete cookies using the setcookie() function.

Cookies are sent in HTTP headers before any HTML output.

Creating a Cookie in PHP

php

setcookie("username", "JohnDoe", time() + 3600); // Cookie valid for 1 hour

"username" is the cookie name.

"JohnDoe" is the value stored.

time() + 3600 sets the cookie expiry time (current time + 3600 seconds).

1/6
Reading a Cookie in PHP
Use the global $_COOKIE array.

php

if(isset($_COOKIE["username"])) {
echo "Welcome " . $_COOKIE["username"];
}

Deleting a Cookie
Set the cookie with a past expiry date:

php

setcookie("username", "", time() - 3600);

Important Points About Cookies


Cookies are stored on the client-side (user's computer).

Max size ~4KB per cookie.

Users can disable cookies.

Cookies can be a security risk if sensitive info is stored without encryption.

2. Serialization

What is Serialization?
Serialization means converting a PHP variable (like arrays or objects) into a string format
that can be stored or transferred.

Useful when you want to save complex data (arrays, objects) in cookies, sessions, or
databases.

Why Serialize?
Cookies and databases only store strings.

2/6
Serialization converts data structures to strings and unserialization converts it back.

PHP Serialization Functions


serialize($data) — Converts PHP data into a serialized string.

unserialize($string) — Converts serialized string back to PHP data.

Example
php

$data = array("apple", "banana", "cherry");


$serializedData = serialize($data);

echo $serializedData; // Outputs something like: a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"cherry";}

$originalData = unserialize($serializedData);
print_r($originalData); // Converts back to the original array

Use Case of Serialization with Cookies


Since cookies only store strings, to save an array:

php

$array = array("red", "green", "blue");


setcookie("colors", serialize($array), time() + 3600);

When reading:

php

if(isset($_COOKIE["colors"])) {
$colors = unserialize($_COOKIE["colors"]);
print_r($colors);
}

3. Session State

3/6
What is a Session?
A session stores user data on the server to maintain state across multiple page requests.

Sessions allow persistent user-specific data without storing it on the client side.

How Does PHP Session Work?


PHP creates a session ID (a unique string).

This session ID is usually stored as a cookie on the client.

The server uses this ID to access the stored session data for that user.

Starting a Session in PHP

php

session_start(); // Must be called before any output

Storing Data in Session


php

$_SESSION['username'] = "JohnDoe";
$_SESSION['cart'] = array("apple", "banana");

Accessing Session Data


php

echo $_SESSION['username']; // Outputs: JohnDoe

Destroying a Session

php

session_start();
session_unset(); // Clear all session variables
session_destroy(); // Destroy the session

Session vs Cookies: Key Differences

4/6
Feature Cookies Sessions

Storage Client-side (browser) Server-side

Size limit About 4KB Limited by server storage

Security Less secure (can be manipulated) More secure (data on server)

Lifetime Controlled by expiry time Controlled by session timeout

Using Sessions with MySQL


Session data by default is stored in files on the server.

You can configure PHP to store session data in MySQL database for persistence and
scalability.

This helps if you have multiple web servers and want centralized session storage.

Summary
Concept Purpose Storage Location Usage Example

Cookies Store small data on client Client (browser) User preferences, login
browser info

Serialization Convert complex data to Used inside cookies, Storing arrays/objects in


string & back sessions, DB cookies

Sessions Maintain user state across Server Logged-in user info,


pages on server shopping carts

If you want, I can also provide simple code examples for each part. Just ask! Good luck for
your exam! 🍀

5/6
Would you like me to prepare notes on related topics like PHP-MySQL connection or
managing state using sessions in detail?

6/6

You might also like