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

Unit 3 - for php application Sessions.pptx

  • 2.
    To start asession 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
  • 3.
  • 4.
    To store datain 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 datafrom a session, you can simply access the $_SESSION array. $username = $_SESSION['username']; $cartItems = $_SESSION['cart']; 5
  • 6.
    To modify sessiondata 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 aspecific 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 asession, 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 atimeout 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 beused 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 ShoppingCart 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> <?phpforeach ($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.