Introduction:

 In this section, I will help you to continue building the social network. Especially I will focus on the functions file that is a skeleton of the project.

Functions.php

<?php 
  $dbhost  = 'localhost';    // Unlikely to require changing
  $dbname  = 'your_database_name';   // Modify these...
  $dbuser  = 'your_username';   // ...variables according
  $dbpass  = 'your_password';   // ...to your installation
  $appname = "Qui's Nest"; // ...and preference

  $connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  if ($connection->connect_error) die($connection->connect_error);

  function createTable($name, $query)
  {
    queryMysql("CREATE TABLE IF NOT EXISTS $name($query)");
    echo "Table '$name' created or already exists.<br>";
  }

  function queryMysql($query)
  {
    global $connection;
    $result = $connection->query($query);
    if (!$result) die($connection->error);
    return $result;
  }

  function destroySession()
  {
    $_SESSION=array();

    if (session_id() != "" || isset($_COOKIE[session_name()]))
      setcookie(session_name(), '', time()-2592000, '/');

    session_destroy();
  }

  function sanitizeString($var)
  {
    global $connection;
    $var = strip_tags($var);
    $var = htmlentities($var);
    $var = stripslashes($var);
    return $connection->real_escape_string($var);
  }

  function showProfile($user)
  {
    if (file_exists("$user.jpg"))
      echo "<img src='$user.jpg' style='float:left;'>";

    $result = queryMysql("SELECT * FROM profiles WHERE user='$user'");

    if ($result->num_rows)
    {
      $row = $result->fetch_array(MYSQLI_ASSOC);
      echo stripslashes($row['text']). "<br style='clear:left;'><br>";
    }
  }
?>

 As I mentioned in the introduction section, this file contains a little bit more than just the function, because I have added the database login details here instead of using another separate file. As you can see, the first half-dozen lines of code define the host, database name, username, and password of the database to use.

 It doesn’t matter what you call the database. Also make sure to correctly assign a MySQL username and password to $dbuser and $dbpass. With correct values, the subsequent two lines will open a connection to MySQL and select the database. The last of the initial instructions sets the name of the social networking site by assigning the value Qui’s Nest to the variable $appname. If you want to change the name, this is the place to do so.

Function Analysis:

   The project uses five main functions:

createTable – Checks whether a table already exists and, if not, creates it.

queryMysql – Issues a query to MySQL, outputting an error message if it fails.

destroySession – Destroys a PHP session and clears its data to log users out.

sanitizeString – Removes potentially malicious code or tags from user input.

showProfile – Displays a user’s image and “about me” message if he has one.

 All of these should be obvious in their action to you, with the possible exception of showProfile, which looks for an image of the name user.jpg (where user is the username of the current user), and if it finds it, displays it. It also displays any “about me” text the user may have saved.

  I have ensured that error handling is in place for all the functions that need it, so that they can catch any typographical or other errors you may introduce, and generate error messages. However, if you use any of this code on a production server, you will probably want to provide your own error-handling routines to make the code more user-friendly.

Conclusion:

In this section, I helped you define the functions.php file that we will use in the project. Although this file won’t display any content in the site, it is really important to do almost main functions such as database connection and creation, session creation, error prevention, and showProfile definition.

*

Ref:

strip_tags
htmlentities
stripslashes
real_escape_string
fetch_array

GET A QUOTE
close slider

    BusinessE-commerce/ Online storeNon-profitPersonal websiteI'm not sureOther

    Create a new websiteOverhaul of my websiteMinor changes to my websiteOther

    Help customer find my business onlineSell my product or service and take payments onlineSell my product or service, taking payments offlineProvide information to the publicOther

    ASAPIn the next few weeksIn the next monthIn the next 2-3 monthsI'm not sureOther

    I'm not sureLess than 1 monthUp to 3 monthsUp to 6 monthsOther

    Pin It on Pinterest