Social Networking Site: Reusing of Codes using PHP Include

In this tutorial I'm going to show you how to reuse of codes using PHP includes, this including of files saves a lot of work. Since we are using a standard Header and Footer this is the best time to apply this concept. So that later on, if you need to update the header you can simply update the header include file not all the pages that has same header. To start with tutorial, open first our project called “philsocial”. Then open the “home.php” file and cut all the codes found in the header section and it looks like as shown below.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="description" content="">
  7. <meta name="author" content="">
  8. <link rel="shortcut icon" href="">
  9.  
  10. <title>Philsocial</title>
  11.  
  12. <!-- Bootstrap core CSS -->
  13. <link href="css/bootstrap.css" rel="stylesheet">
  14.  
  15. <!-- Custom styles for this template -->
  16. <link href="jumbotron.css" rel="stylesheet">
  17.  
  18. <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  19. <!--[if lt IE 9]>
  20. <script src="../../assets/js/html5shiv.js"></script>
  21. <script src="../../assets/js/respond.min.js"></script>
  22. <![endif]-->
  23. <?php
  24. //login confirmation
  25. confirm_logged_in();
  26. ?>
  27. .table th, .table td {
  28. border-top: none;
  29. }
  30. </head>
  31.  
  32. <body>
  33.  
  34. <div class="navbar navbar-inverse navbar-fixed-top">
  35. <div class="container">
  36. <div class="navbar-header">
  37. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
  38. <span class="icon-bar"></span>
  39. <span class="icon-bar"></span>
  40. <span class="icon-bar"></span>
  41. </button>
  42. <a class="navbar-brand" href="home.php"><B>Philsocial</B></a>
  43. </div>
  44. <form class="navbar-form navbar-left">
  45. <div class="form-group">
  46. <div class="rows">
  47. <input type="search" placeholder="search" class="form-control" size="40">
  48. </div>
  49. </div>
  50. </form>
  51. <div class="navbar-collapse collapse">
  52.  
  53. <form class="navbar-form navbar-right">
  54. <ul class="nav navbar-nav">
  55. <li class="active"><a href="home.php">Home</a></li>
  56.  
  57. <li class="dropdown">
  58.  
  59. <a href="#" class="dropdown-toggle" data-toggle="dropdown">
  60. <?php
  61. //retrieve session variable
  62. echo $_SESSION['fName'];?>
  63. <b class="caret"></b>
  64. </a>
  65.  
  66. <ul class="dropdown-menu">
  67. <li><a href="#">My Profile</a></li>
  68. <li><a href="#">Edit profile</a></li>
  69. <li><a href="#">Edit profile Picture</a></li>
  70. <li><a href="#">Customize profile</a></li>
  71. <li><a href="#">Edit Work and Education</a></li>
  72.  
  73. </ul>
  74. </li>
  75. <li class="dropdown">
  76.  
  77. <a href="#" class="dropdown-toggle" data-toggle="dropdown">Account<b class="caret"></b></a>
  78.  
  79. <ul class="dropdown-menu">
  80. <li><a href="#">Account Settings</a></li>
  81. <li><a href="#">Privacy Settings</a></li>
  82. <li><a href="#">Manage Social Accounts</a></li>
  83. <li><a href="#">Manage Credits</a></li>
  84. <li><a href="logout.php">Logout</a></li>
  85.  
  86. </ul>
  87. </li>
  88. </ul>
  89. </form>
  90. </div><!--/.navbar-collapse -->
  91. </div>
  92. </div>
Then we’re going to create a new PHP file called “header.php” and we will paste the code we cut from “home.php”. Then we will add a single line of code in the home.php under this line require_once("includes/initialize.php");. And heres the following code: This code below simply calls the header.php file.
  1. <?php
  2. require_once("includes/initialize.php");
  3. include 'header.php';
  4. ?>
Next we can do all the process above to apply this in all our web pages that has the same header. If you want to see more of my works, new Source Code or Application and Tutorials Just click here.

Add new comment