Unit 3  -  for php  application Sessions.pptx
To start a session in PHP, you need to call the
session_start() function at the beginning of your script. This
function initializes the session and makes session-related
data accessible via the $_SESSION superglobal.
2
<?php
session_start();
?>
3
To store data in the $_SESSION array to make it
accessible across multiple requests. This data persists
until the session is destroyed or the user logs out.
$_SESSION['username'] = 'john_doe';
$_SESSION['cart'] = ['item1', 'item2', 'item3'];
4
To retrieve data from a session, you can simply access
the $_SESSION array.
$username = $_SESSION['username'];
$cartItems = $_SESSION['cart'];
5
To modify session data just like any other PHP array.
$_SESSION['cart'][] = 'item4'; // Add an item to the cart
$_SESSION['username'] = 'jane_doe'; // Update the
username
6
To remove a specific item from the session, you can use
the unset() function.
unset($_SESSION['cart'][1]); // Remove the second item
from the cart
7
To end a session, call the session_destroy() function. This
clears all session data and cookies associated with the
session. However, this doesn't unset the $_SESSION
superglobal, so any session data set during the script
execution will still be accessible until the script finishes
running.
session_destroy();
8
Sessions have a timeout period defined in PHP
configuration. By default, this is set to 24 minutes. If a
user remains inactive for this duration, their session data
will be automatically destroyed.
9
Sessions should be used carefully to avoid security risks
like session fixation, session hijacking, and session data
manipulation. To enhance security, use techniques like
session ID regeneration (session_regenerate_id()) and
use secure cookies.
10
// During login
if ($validCredentials) {
$_SESSION['user_id'] = $userId;
}
// On protected pages
session_start();
if (!isset($_SESSION['user_id'])) {
// Redirect to login page
}
11
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$_SESSION['user_input'] = $_POST['input_data'];
header('Location: display.php');
exit();
}
?>
<form method="post" action="">
<input type="text" name="input_data" placeholder="Enter data">
<button type="submit">Submit</button>
</form>
12
<?php
session_start();
if (isset($_SESSION['user_input'])) {
echo "User input: " . $_SESSION['user_input'];
} else {
echo "No user input found.";
}
?>
13
<?php
session_start();
$products = [
['id' => 1, 'name' => 'Product A', 'price' => 10.99],
['id' => 2, 'name' => 'Product B', 'price' => 15.99],
['id' => 3, 'name' => 'Product C', 'price' => 7.49],
];
if (isset($_POST['add_to_cart'])) {
$productId = $_POST['product_id'];
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [];
}
$_SESSION['cart'][] = $productId;
}
?>
14
<!DOCTYPE html>
<html>
<head>
<title>Simple Shopping Cart Example</title>
</head>
<body>
<h1>Products</h1>
<ul>
<?php foreach ($products as $product): ?>
<li>
<?php echo $product['name']; ?> - $<?php echo $product['price']; ?>
<form method="post">
<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
<input type="submit" name="add_to_cart" value="Add to Cart">
</form>
</li>
<?php endforeach; ?>
</ul>
<h2><a href="cart.php">View Cart</a></h2>
</body>
</html>
15
<?php
session_start();
$products = [
['id' => 1, 'name' => 'Product A', 'price' => 10.99],
['id' => 2, 'name' => 'Product B', 'price' => 15.99],
['id' => 3, 'name' => 'Product C', 'price' => 7.49],
];
function getProductById($id) {
global $products;
foreach ($products as $product) {
if ($product['id'] == $id) {
return $product;
}
}
return null;
}
$cartItems = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
$totalPrice = 0;
?>
16
<html>
<head>
<title>Shopping Cart</title>
</head>
<body>
<h1>Shopping Cart</h1>
<table>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
<?php foreach ($cartItems as $itemId): ?>
<?php $product = getProductById($itemId); ?>
<?php if ($product): ?>
<tr>
<td><?php echo $product['name']; ?></td>
<td>$<?php echo $product['price']; ?></td>
</tr>
<?php $totalPrice += $product['price']; ?>
<?php endif; ?>
<?php endforeach; ?>
<tr>
<th>Total</th>
<th>$<?php echo $totalPrice; ?></th>
</tr>
</table>
</body>
</html>
17
18

More Related Content

PPT
Php Sessoins N Cookies
PPT
php $_GET / $_POST / $_SESSION
PPT
Lecture8 php page control by okello erick
PPTX
PHP Cookies and Sessions
PDF
4.4 PHP Session
PPTX
FP512 Cookies sessions
PPTX
Cookie and session
Php Sessoins N Cookies
php $_GET / $_POST / $_SESSION
Lecture8 php page control by okello erick
PHP Cookies and Sessions
4.4 PHP Session
FP512 Cookies sessions
Cookie and session

Similar to Unit 3 - for php application Sessions.pptx (20)

PDF
Php tutorial handout
PPT
Session,cookies
PPTX
PHP SESSIONS & COOKIE.pptx
PPTX
Session handling in codeigniter
PPT
Cookies and sessions
PPTX
murach12.pptx
DOC
Creating a Simple PHP and MySQL-Based Login System
PPT
Php ssession - cookies -introduction
PPT
Php mysql
PPTX
StateManagementintPHPStateManagementinPHP.pptx
PPT
Parameter Passing & Session Tracking in PHP
PDF
Introduction to php web programming - sessions and cookies
PPT
PHP-07-Cookies-Sessions indepth powerpoint
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPT
PHP - Introduction to PHP Cookies and Sessions
DOC
Php sessions
PDF
PHP-Cookies-Sessions.pdf
PPT
Php 07-cookies-sessions
PPT
17 sessions
Php tutorial handout
Session,cookies
PHP SESSIONS & COOKIE.pptx
Session handling in codeigniter
Cookies and sessions
murach12.pptx
Creating a Simple PHP and MySQL-Based Login System
Php ssession - cookies -introduction
Php mysql
StateManagementintPHPStateManagementinPHP.pptx
Parameter Passing & Session Tracking in PHP
Introduction to php web programming - sessions and cookies
PHP-07-Cookies-Sessions indepth powerpoint
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PHP - Introduction to PHP Cookies and Sessions
Php sessions
PHP-Cookies-Sessions.pdf
Php 07-cookies-sessions
17 sessions
Ad

Recently uploaded (20)

PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
B.Sc. DS Unit 2 Software Engineering.pptx
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
TNA_Presentation-1-Final(SAVE)) (1).pptx
My India Quiz Book_20210205121199924.pdf
Unit 4 Computer Architecture Multicore Processor.pptx
Environmental Education MCQ BD2EE - Share Source.pdf
Weekly quiz Compilation Jan -July 25.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
Share_Module_2_Power_conflict_and_negotiation.pptx
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
IGGE1 Understanding the Self1234567891011
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Chinmaya Tiranga quiz Grand Finale.pdf
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Ad

Unit 3 - for php application Sessions.pptx

  • 2. To start a session in PHP, you need to call the session_start() function at the beginning of your script. This function initializes the session and makes session-related data accessible via the $_SESSION superglobal. 2
  • 4. To store data in the $_SESSION array to make it accessible across multiple requests. This data persists until the session is destroyed or the user logs out. $_SESSION['username'] = 'john_doe'; $_SESSION['cart'] = ['item1', 'item2', 'item3']; 4
  • 5. To retrieve data from a session, you can simply access the $_SESSION array. $username = $_SESSION['username']; $cartItems = $_SESSION['cart']; 5
  • 6. To modify session data just like any other PHP array. $_SESSION['cart'][] = 'item4'; // Add an item to the cart $_SESSION['username'] = 'jane_doe'; // Update the username 6
  • 7. To remove a specific item from the session, you can use the unset() function. unset($_SESSION['cart'][1]); // Remove the second item from the cart 7
  • 8. To end a session, call the session_destroy() function. This clears all session data and cookies associated with the session. However, this doesn't unset the $_SESSION superglobal, so any session data set during the script execution will still be accessible until the script finishes running. session_destroy(); 8
  • 9. Sessions have a timeout period defined in PHP configuration. By default, this is set to 24 minutes. If a user remains inactive for this duration, their session data will be automatically destroyed. 9
  • 10. Sessions should be used carefully to avoid security risks like session fixation, session hijacking, and session data manipulation. To enhance security, use techniques like session ID regeneration (session_regenerate_id()) and use secure cookies. 10
  • 11. // During login if ($validCredentials) { $_SESSION['user_id'] = $userId; } // On protected pages session_start(); if (!isset($_SESSION['user_id'])) { // Redirect to login page } 11
  • 12. <?php session_start(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_SESSION['user_input'] = $_POST['input_data']; header('Location: display.php'); exit(); } ?> <form method="post" action=""> <input type="text" name="input_data" placeholder="Enter data"> <button type="submit">Submit</button> </form> 12
  • 13. <?php session_start(); if (isset($_SESSION['user_input'])) { echo "User input: " . $_SESSION['user_input']; } else { echo "No user input found."; } ?> 13
  • 14. <?php session_start(); $products = [ ['id' => 1, 'name' => 'Product A', 'price' => 10.99], ['id' => 2, 'name' => 'Product B', 'price' => 15.99], ['id' => 3, 'name' => 'Product C', 'price' => 7.49], ]; if (isset($_POST['add_to_cart'])) { $productId = $_POST['product_id']; if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = []; } $_SESSION['cart'][] = $productId; } ?> 14
  • 15. <!DOCTYPE html> <html> <head> <title>Simple Shopping Cart Example</title> </head> <body> <h1>Products</h1> <ul> <?php foreach ($products as $product): ?> <li> <?php echo $product['name']; ?> - $<?php echo $product['price']; ?> <form method="post"> <input type="hidden" name="product_id" value="<?php echo $product['id']; ?>"> <input type="submit" name="add_to_cart" value="Add to Cart"> </form> </li> <?php endforeach; ?> </ul> <h2><a href="cart.php">View Cart</a></h2> </body> </html> 15
  • 16. <?php session_start(); $products = [ ['id' => 1, 'name' => 'Product A', 'price' => 10.99], ['id' => 2, 'name' => 'Product B', 'price' => 15.99], ['id' => 3, 'name' => 'Product C', 'price' => 7.49], ]; function getProductById($id) { global $products; foreach ($products as $product) { if ($product['id'] == $id) { return $product; } } return null; } $cartItems = isset($_SESSION['cart']) ? $_SESSION['cart'] : []; $totalPrice = 0; ?> 16
  • 17. <html> <head> <title>Shopping Cart</title> </head> <body> <h1>Shopping Cart</h1> <table> <tr> <th>Product</th> <th>Price</th> </tr> <?php foreach ($cartItems as $itemId): ?> <?php $product = getProductById($itemId); ?> <?php if ($product): ?> <tr> <td><?php echo $product['name']; ?></td> <td>$<?php echo $product['price']; ?></td> </tr> <?php $totalPrice += $product['price']; ?> <?php endif; ?> <?php endforeach; ?> <tr> <th>Total</th> <th>$<?php echo $totalPrice; ?></th> </tr> </table> </body> </html> 17
  • 18. 18