PHP Sessions

Start Session

This will start a brand-new session.

<?php
//use this to start/restart the session
session_start();

// insert something into the session
$_SESSION["cartId"] = 5;

echo "Your cart id is " . $_SESSION["cartId"] . "<br />";
echo "Your session id is " . session_id();

Show Session

This will show something currently stored in an existing session.

<?php
//use this to start/restart the session
session_start();

echo "Your cart id is " . $_SESSION["cartId"];
echo "<br>Your session id is " . session_id();

Destroy Session

This will destroy a session completely and delete all data from the server.

<?php
//use this to start/restart the session
session_start();

// set session to an empty array
$_SESSION = array();

// destroy the cookie
$params = session_get_cookie_params();
setcookie(session_name(), "", 1, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);

// destroy the session
session_destroy();