Skip to main content

Social Networking Site Project Setup


In this tutorial, I will show you how to set up our project before we start developing a several web pages. First lets create a folder name “philsocial” inside our web server. Since we will be using a Twitter Bootstrap framework, and if you don’t have a copy of this, you can download it here. After downloading, you need to extract it and copy the following folder such as assets, css, fonts and js. Then paste it inside “philsocial” folder, and add three new folder such as “img” where we’re going to store our images to be used later on, the another folder and name it as “uploads”. This folder will be used as a storage area for our upload files and documents. And finally the “includes” folder that will hold our different classes and libraries.
Here's the folder structure:

philsocial/
├── css/
│ ├── bootstrap.css
│ ├── bootstrap.min.css
│ ├── bootstrap-theme.css
│ ├── bootstrap-theme.min.css
├── js/
│ ├── bootstrap.js
│ ├── bootstrap.min.js
└── fonts/
├── glyphicons-halflings-regular.eot
├── glyphicons-halflings-regular.svg
├── glyphicons-halflings-regular.ttf
└── glyphicons-halflings-regular.woff
|--- assets/
| |--css
| | |--pygments-manni.css
| | |--docs.css
| |--js
| | |--application.js
| | |--customizer.js
| | |--filesaver.js
| | |--holder.js
| | |--html5shiv.js
| | |--jquery.js
| | |--jszip.js
| | |--less.js
| | |--raw-files.js
| | |--respond.min.js
| | |--uglify.js
| |--ico
|--- uploads
|--- img
|--- includes

This time, we're going to create a new PHP file and name it as “config.php”, then save it inside includes folder. And add the following code:

  1. <?php
  2. /**
  3. * Description: The main class for Database.
  4. * Author: Joken Villanueva
  5. * Date Created: October 27, 2013
  6. * Revised By:
  7. */
  8.  
  9. //Database Constants
  10. defined('DB_SERVER') ? null : define("DB_SERVER","localhost");//define our database server
  11. defined('DB_USER') ? null : define("DB_USER","root"); //define our database user
  12. defined('DB_PASS') ? null : define("DB_PASS",""); //define our database Password
  13. defined('DB_NAME') ? null : define("DB_NAME","philsocialdb"); //define our database Name
  14. ?>

Next,together with our “config.php file”, we’re going to create a PHP file that will hold and manage our Database objects, and this lets save this file as “database.php”, then add the following code:

  1. <?php
  2. /**
  3. * Description: The main class for Database.
  4. * Author: Joken Villanueva
  5. * Date Created: october27, 2013
  6. * Revised By:
  7. */
  8.  
  9. require_once(LIB_PATH.DS."config.php");
  10. class Database {
  11. var $sql_string = '';
  12. var $error_no = 0;
  13. var $error_msg = '';
  14. private $conn;
  15. public $last_query;
  16. private $magic_quotes_active;
  17. private $real_escape_string_exists;
  18.  
  19. function __construct() {
  20. $this->open_connection();
  21. $this->magic_quotes_active = get_magic_quotes_gpc();
  22. $this->real_escape_string_exists = function_exists("mysql_real_escape_string");
  23. }
  24.  
  25. public function open_connection() {
  26. $this->conn = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
  27. if(!$this->conn){
  28. echo "Problem in database connection! Contact administrator!";
  29. exit();
  30. }else{
  31. $db_select = mysql_select_db(DB_NAME,$this->conn);
  32. if (!$db_select) {
  33. echo "Problem in selecting database! Contact administrator!";
  34. exit();
  35. }
  36. }
  37.  
  38. }
  39.  
  40. function setQuery($sql='') {
  41. $this->sql_string=$sql;
  42. }
  43.  
  44. function executeQuery() {
  45. $result = mysql_query($this->sql_string, $this->conn);
  46. $this->confirm_query($result);
  47. return $result;
  48. }
  49.  
  50. private function confirm_query($result) {
  51. if(!$result){
  52. $this->error_no = mysql_errno( $this->conn );
  53. $this->error_msg = mysql_error( $this->conn );
  54. return false;
  55. }
  56. return $result;
  57. }
  58.  
  59. function loadResultList( $key='' ) {
  60. $cur = $this->executeQuery();
  61.  
  62. $array = array();
  63. while ($row = mysql_fetch_object( $cur )) {
  64. if ($key) {
  65. $array[$row->$key] = $row;
  66. } else {
  67. $array[] = $row;
  68. }
  69. }
  70. return $array;
  71. }
  72.  
  73. function loadSingleResult() {
  74. $cur = $this->executeQuery();
  75.  
  76. while ($row = mysql_fetch_object( $cur )) {
  77. $data = $row;
  78. }
  79. return $data;
  80. }
  81.  
  82. function getFieldsOnOneTable( $tbl_name ) {
  83.  
  84. $this->setQuery("DESC ".$tbl_name);
  85. $rows = $this->loadResultList();
  86.  
  87. $f = array();
  88. for ( $x=0; $x<count( $rows ); $x++ ) {
  89. $f[] = $rows[$x]->Field;
  90. }
  91.  
  92. return $f;
  93. }
  94.  
  95. public function fetch_array($result) {
  96. return mysql_fetch_array($result);
  97. }
  98. //gets the number or rows
  99. public function num_rows($result_set) {
  100. return mysql_num_rows($result_set);
  101. }
  102.  
  103. public function insert_id() {
  104. // get the last id inserted over the current db connection
  105. return mysql_insert_id($this->conn);
  106. }
  107.  
  108. public function affected_rows() {
  109. return mysql_affected_rows($this->conn);
  110. }
  111.  
  112. public function escape_value( $value ) {
  113. if( $this->real_escape_string_exists ) { // PHP v4.3.0 or higher
  114. // undo any magic quote effects so mysql_real_escape_string can do the work
  115. if( $this->magic_quotes_active ) { $value = stripslashes( $value ); }
  116. $value = mysql_real_escape_string( $value );
  117. } else { // before PHP v4.3.0
  118. // if magic quotes aren't already on then add slashes manually
  119. if( !$this->magic_quotes_active ) { $value = addslashes( $value ); }
  120. // if magic quotes are active, then the slashes already exist
  121. }
  122. return $value;
  123. }
  124.  
  125. public function close_connection() {
  126. if(isset($this->conn)) {
  127. mysql_close($this->conn);
  128. unset($this->conn);
  129. }
  130. }
  131.  
  132. }
  133. $mydb = new Database();
  134.  
  135.  
  136. ?>

And we need also to create another PHP file, then save it as “function.php” and add the following code:
This code is useful when we are cleaning some string or variable, redirecting to another page and automatic loading of different classes.

  1. <?php
  2. function strip_zeros_from_date($marked_string="") {
  3. //first remove the marked zeros
  4. $no_zeros = str_replace('*0','',$marked_string);
  5. $cleaned_string = str_replace('*0','',$no_zeros);
  6. return $cleaned_string;
  7. }
  8. function redirect_to($location = NULL) {
  9. if($location != NULL){
  10. header("Location: {$location}");
  11. }
  12. }
  13. function output_message($message="") {
  14.  
  15. if(!empty($message)){
  16. return "<p class=\"message\">{$message}</p>";
  17. }else{
  18. return "";
  19. }
  20. }
  21. function __autoload($class_name) {
  22. $class_name = strtolower($class_name);
  23. $path = LIB_PATH.DS."{$class_name}.php";
  24. if(file_exists($path)){
  25. require_once($path);
  26. }else{
  27. die("The file {$class_name}.php could not be found.");
  28. }
  29.  
  30. }
  31.  
  32.  
  33. ?>

And finally, we will create another PHP file inside includes folder then, save it as “initialize.php” and add the following code:
This code will simply do the initialization of our different classes.

  1. <?php
  2. /**
  3. * Description: This includes for basic and core configurations.
  4. * Author: Joken Villanueva
  5. * Date Created: october 27, 2013
  6. * Revised By:
  7. */
  8.  
  9. //define the core paths
  10. //Define them as absolute peths to make sure that require_once works as expected
  11.  
  12. //DIRECTORY_SEPARATOR is a PHP Pre-defined constants:
  13. //(\ for windows, / for Unix)
  14. defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
  15.  
  16. defined('SITE_ROOT') ? null : define ('SITE_ROOT', $_SERVER['DOCUMENT_ROOT'].DS.'philsocial');
  17.  
  18. defined('LIB_PATH') ? null : define ('LIB_PATH',SITE_ROOT.DS.'includes');
  19.  
  20. // load config file first
  21. require_once(LIB_PATH.DS."config.php");
  22. //load basic functions next so that everything after can use them
  23. require_once(LIB_PATH.DS."functions.php");
  24. //later here where we are going to put our class session
  25.  
  26. //Load Core objects
  27. require_once(LIB_PATH.DS."database.php");
  28.  
  29. //load database-related classes
  30.  
  31.  
  32. ?>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted byaacac (not verified)on Sat, 07/07/2018 - 11:31

nice one keep up

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.