Some exciting things about PHP

PHP has mixed reviews from developers. Some really hate it, and few love it, however, I recently had to build an excursion booking website using PHP and MySQL. It was my first time doing anything with PHP but I found some really good things I'll like to share about PHP and its functions so walk with me.

  • SESSIONS.

    PHP makes use of sessions as a way to retrieve data that should only be accessible to specific users. For example, only someone who is logged in to Netflix should be able to add movies to 'My Lists'. If you think about it this happens in everyday situations where user authentication is required. It is created using the code

       ini_set("session.save_path", "your_path_here");
      if (session_status() == PHP_SESSION_NONE) {
          session_start();
      }
    

    It would be wise to use the above code in the navbar because it needs to check for sessions across every page and it is assumed we have a navbar in every page. This code creates a user session whenever a condition is met, for example, a user has logged in successfully. Here is how such a condition would look.

        $Username = mysqli_real_escape_string($conn, $_POST['username']);
              $pwd = mysqli_real_escape_string($conn, $_POST['Password']);
              $_SESSION['logged-in'] = true;
              if ($_SESSION['logged-in']) {
                  # code...
                  $_SESSION['username'] = $Username;
                  $_SESSION['userID'] = $userID;
                  header("location: index.php");
              }
    

    after verifying the user and redirecting him, some restricted items can now be displayed on the navbar like this:

  •   <?php
      ini_set("session.save_path", "/home/unn_w22016721/sessionData");
      if (session_status() == PHP_SESSION_NONE) {
          session_start();
    
      }
      ?>
      <!DOCTYPE html>
      <html lang="en">
      <head>
      //insert head tag contents   
          </head>
      <body>
          <header>
    
             <nav class="nav-bar">
                  <div class="logo">
                      <h1 ><a class="Company" href="index.php">East Experience</a></h1>
                      <!-- closing tag for Company -->
                  </div>
                      <ul class="nav-items">
                          <li><a href="index.php">Home</a></li>
                          <li><a href="about.php">About</a></li>
                          <?php 
    
                             if (isset($_SESSION['logged-in'])) {
                                  # code...
                                  echo   "<li><a href='destinations.php'>Destination</a></li>";
                                  echo "<li><a href='Manage.php'>Manage Bookings</a></li>";
    
                               echo "<li><a href='logout.php'>Log out</a></li>";
    
                              } else{
                                  echo "<li><a href='signup.php'>Sign up</a></li>";
                                  echo " <li><a href='login.php'>Log in</a></li>";
                              }
                          ?>  
                      </ul>
              <!-- closing tag for nav section -->
              </nav>
                  </header>
    

    In the code above we've added the destination, manage booking and log-out pages to the navbar which are only accessible after a user logs in. This is one powerful use of sessions.

    Now how do we make sessions go away, say when a user has logged out? Well, it's very easy too! All you have to do is use SESSION_DESTROY(). I'll show you how.

<?php 

ini_set("session.save_path", "your_path_here");
if (session_status() == PHP_SESSION_NONE) {
    session_start();
    session_unset();
    session_destroy();
}

header('location: ./index.php');

The above code would delete the session data after logging the user out and reverse the webpage to your defaults.

And that's all I can cover today. I'll share more in the coming weeks so stay tuned.