php

Notes and Practical Projects for learning php while my learning journey

View on GitHub

Session


Table Of Contents


What is a PHP Session?

Tip: If you need a permanent storage, you may want to store the data in a database.


Start session

  1. start it manually
    session_start();  
    
  2. edit php.ini file
    sed -i 's/session.auto_start = 0/session.auto_start = 1/'  php.ini
    

Note The session_start() function must be the very first thing in your document. Before any HTML tags.


Set session variables

$_SESSION["favcolor"] = "yellow";

Get PHP session values

print_r($_SESSION); // print all the values 
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";

Most sessions set a user-key on the user’s computer that looks something like this: 765487cf34ert8dede5a562e4f3a7e12. Then, when a session is opened on another page, it scans the computer for a user-key. If there is a match, it accesses that session, if not, it starts a new session.


Remove all session variables

session_unset();

Destroy the session

session_destroy();

Click me if you don’t know what is the cookies