Saturday, April 9, 2016

Building a social network: Header(P3).

Introduction:

In this section, I will introduce the header.php file that each page of the project will need to have access the same set of features. This file is actually included by the other files and it includes functions.php file. This means that only a single require_once is needed in each file.

header.php

<?php
session_start();
  echo "<!DOCTYPE html>\n<html><head><link rel='icon' href='robin.gif' 
type='image/gif' sizes='16x16'>";
  require_once 'functions.php';
  $userstr = ' (Guest)';
  if (isset($_SESSION['user']))
  {
    $user     = $_SESSION['user'];
    $loggedin = TRUE;
    $userstr  = " ($user)";
  }
  else $loggedin = FALSE;
  echo "<title>$appname$userstr</title><link rel='stylesheet' " .
       "href='styles.css' type='text/css'>"                     .
       "</head><body><center><canvas id='logo' width='624' "    .
       "height='96'>$appname</canvas></center>"             .
       "<div class='appname'>$appname$userstr</div>"            .
       "<script src='javascript.js'></script>";
  if ($loggedin)
  {
    echo "<br ><ul class='menu'>" .
         "<li><a href='members.php?view=$user'>Home</a></li>" .
         "<li><a href='members.php'>Members</a></li>"         .
         "<li><a href='friends.php'>Friends</a></li>"         .
         "<li><a href='messages.php'>Messages</a></li>"       .
         "<li><a href='profile.php'>Edit Profile</a></li>"    .
         "<li><a href='logout.php'>Log out</a></li></ul><br>";
  }
  else
  {
    echo ("<br><ul class='menu'>" .
          "<li><a href='index.php'>Home</a></li>"                .
          "<li><a href='signup.php'>Sign up</a></li>"            .
          "<li><a href='login.php'>Log in</a></li></ul><br>"     .
          "<span class='info'>&#8658; You must be logged in to " .
          "view this page.</span><br><br>");
  }
?>
 As we see, this file starts by calling the function session_start. This sets up a session that will remember certain values we want to store across different PHP files. With the session started, the program then checks whether the session variable user is currently assigned a value. If so, a user has logged in and the variable $loggedin is set to TRUE.
 After the main setup code in which a style sheet is loaded, a canvas element is created for the logo, and a div is also created. The file  javascript.js is loaded to pull in the  O,  S, and  C functions which are short forms are used to select HTML elements by Id or class name. 
 Using the value of  $loggedin, an  if block displays one of two sets of menus. The non-logged-in set simply offers options of Home, Sign up, and Log in, whereas the logged-in version offers full access to the project’s features. Additionally, if a user is logged in, his or her username is appended in brackets to the page title and placed after the main heading. We can freely refer to $user wherever we want to put in the name, because if the user is not logged in, that variable is empty and will have no effect on the output.
  The styling applied to this file is in the file  styles.css and includes creating a wide heading with a colored background, and turning the links in the lists to rounded buttons.

Conclusion:

  In this section, I helped you build the header.php file included in almost other php files in our project. This file actually defines menus, logo for the site regardless user already logins the site or not.
* Ref:

No comments:

Post a Comment