PHP Login System with Admin Features

This forum was created to talk about the PHP Login System with admin features created by jpmaster77 on evolt's website


    Friends feature (Complete)

    Share

    Poll

    Did you find this useful?

    [ 3 ]
    100% [100%] 
    [ 0 ]
    0% [0%] 

    Total Votes: 3
    avatar
    jbonnett

    Number of posts : 2
    Age : 25
    Registration date : 2012-12-10

    Friends feature (Complete)

    Post  jbonnett on Tue Jan 15, 2013 6:51 pm

    Hi I could not find anywhere a friends add on, so I created one myself...

    first add the friends table to the database
    Code:
    CREATE TABLE IF NOT EXISTS `friends` (
      `person_id` varchar(50) NOT NULL,
      `friend_id` varchar(50) DEFAULT NULL,
      `status` varchar(10) DEFAULT NULL
    );

    add table to constants
    Code:
    define("TBL_FRIENDS",  "friends");

    create "friends.php"
    Code:
    <?php include("include/session.php");

    if($session->logged_in){

     $action = $_REQUEST['action'];
     $pid = $_REQUEST['person'];
     $fid = $_REQUEST['friend'];

    // send request

    if ($action == 'add') {
       $add = "INSERT INTO `".TBL_FRIENDS."` (person_id, friend_id, status) VALUES ('$_REQUEST[person]', '$_REQUEST[friend]', 'inActive')";
       if (!$database->query($add, $con)) {
           die('Error: ' . mysql_error());
       }
       header("location:" .$_SERVER['HTTP_REFERER']);
    }


    // accept request

    if ($action == 'accept') {
       $q = "UPDATE `".TBL_FRIENDS."` SET  `status` =  'Active' WHERE `person_id` =  '$pid' AND `friend_id` =  '$fid' AND `status` =  'inActive' LIMIT 1";
       if (!$database->query($q, $con)) {
           die('Error: ' . mysql_error());
       }
       header("location:" .$_SERVER['HTTP_REFERER']);
    }


    // decline or cancel request, also remove friend
    if ($action == 'decline') {
       $q1 = "DELETE FROM `".TBL_FRIENDS."` WHERE `person_id` = '$pid' AND `friend_id` = '$fid' OR `person_id` = '$fid' AND `friend_id` = '$pid' LIMIT 1";
       if (!$database->query($q1, $con)) {
           die('Error: ' . mysql_error());
       }
       header("location:" .$_SERVER['HTTP_REFERER']);
    }

    // view persons friends
    if ($action == 'view') {
       echo "<h1>$pid's Friend's</h1>";
       $view_friends = $database->query("SELECT * FROM `".TBL_FRIENDS."` WHERE `person_id` = '$pid' AND `status` = 'Active' OR `friend_id` = '$pid' AND `status` = 'Active'");
       
       while($f = mysql_fetch_array($view_friends)){
          if($f['friend_id'] == "$session->username"){
             
          }elseif($f['person_id'] == "$session->username"){
             
          }else{
             echo '<a href="userinfo.php?user='.$f['person_id'].'">'.$f['person_id'].'</a><br />';
          }
       }
    }

    if (!$action or $action == '') {
       // show requests

       echo "<h1>$session->username's Friend Requests</h1>";
       $requests = $database->query("SELECT * FROM `".TBL_FRIENDS."` WHERE `friend_id` = '$session->username' AND `status` = 'inActive'");
       echo "<table>";
       while($r = mysql_fetch_array($requests)){
          echo"
             <tr><td><a href='userinfo.php?user=".$r["person_id"]."'>".$r["person_id"]."</a></td>
             <td><a href='".$_SERVER["HTTP_SELF"]."?action=accept&person=".$r["person_id"]."&friend=".$session->username."'>Accept</a>
             <a href='".$_SERVER["HTTP_SELF"]."?action=decline&person=".$r["person_id"]."&friend=".$session->username."'>Decline</a></td>";
       }
       echo "</table>";
     
     
       // friends with

       echo "<h1>Friends</h1>";

       $friends = $database->query("SELECT * FROM `".TBL_FRIENDS."` WHERE `person_id` = '$session->username' AND `status` = 'Active' OR `friend_id` = '$session->username' AND `status` = 'Active'");

       while($f = mysql_fetch_array($friends)){
          if($f['friend_id'] == "$session->username"){
             echo '<a href="userinfo.php?user='.$f['person_id'].'">'.$f['person_id'].'</a><br />';
          }else{
             echo '<a href="userinfo.php?user='.$f['friend_id'].'">'.$f['friend_id'].'</a><br />';
          } 
       }
    }

    echo "<br />Back To [<a href=\"main.php\">Main</a>]";

    }else{
       header("location: main.php");   
    }

    ?>


    add to the "userinfo.php" where you wan't the add friend link to be
    Code:
    if(!mysql_num_rows($database->query("SELECT * FROM `".TBL_FRIENDS."` WHERE `person_id` = '$req_user' AND `friend_id` = '$session->username' OR `person_id` = '$session->username' AND `friend_id` = '$req_user' LIMIT 1")) > 0){
          echo "[<a href=\"friends.php?action=add&person=$session->username&friend=$req_user\">Add Friend</a>]";
       }else{
          if(mysql_num_rows($database->query("SELECT * FROM `".TBL_FRIENDS."` WHERE `person_id` = '$req_user' AND `friend_id` = '$session->username' AND `status` = 'inActive' OR `person_id` = '$session->username' AND `friend_id` = '$req_user' AND `status` = 'inActive' LIMIT 1")) > 0){
             echo "[<a href=\"friends.php?action=decline&person=$session->username&friend=$req_user\">Cancel friends request</a>]";
          }
          
          if(mysql_num_rows($database->query("SELECT * FROM `".TBL_FRIENDS."` WHERE `person_id` = '$req_user' AND `friend_id` = '$session->username' AND `status` = 'Active' OR `person_id` = '$session->username' AND `friend_id` = '$req_user' AND `status` = 'Active' LIMIT 1")) > 0){
             echo "[<a href=\"friends.php?action=decline&person=$session->username&friend=$req_user\">Remove friend</a>]";
          }
       }

    and again in "userinfo.php" add the following line where you would like the users friends link and number of friends to be
    Code:
    if(strcmp($session->username,$req_user) == 0){
          echo "<tr><td><b>Friends:</b> </td><td>$numFriends (<a href=\"friends.php?action=view&person=".$req_user_info['username']."\">View Friends</a>)</td></tr>";
    }else{
          echo "<tr><td><b>Friends:</b> </td><td>".($numFriends-1)." (<a href=\"friends.php?action=view&person=".$req_user_info['username']."\">View Friends</a>)</td></tr>";
    }

    add link friends to the user menu in "main.php"
    Code:
       if($numFriendRequests == 0){
          echo "[<a href=\"friends.php?user=$session->username\">$numFriends Friends</a>] &nbsp;&nbsp;";
       }else{
          echo "[<a href=\"friends.php?user=$session->username\">$numFriendRequests Friends Requests</a>] &nbsp;&nbsp;";
       }

    and finaly in "session.php" add this at the very bottom
    Code:
    /**
        * This will get the number of new friends requests
        * sent to this user's session.
        */
          $requests = "SELECT * FROM `friends` WHERE `friend_id` = '$session->username' AND `status` = 'inActive'";
          $numFriendRequests = $database->query($requests) or die(mysql_error());
          $numFriendRequests = mysql_num_rows($numFriendRequests);
        
    /**
        * This will get the number of friends
        * sent to this user's session.
        */
          $friends = "SELECT * FROM `friends` WHERE `person_id` = '$session->username' AND `status` = 'Active' OR `friend_id` = '$session->username' AND `status` = 'Active'";
          $numFriends = $database->query($friends) or die(mysql_error());
          $numFriends = mysql_num_rows($numFriends);

    I think I have give you everything you need If not reply to this message and I'll try and find what I have missed

    Jamie Bonnett,

      Current date/time is Tue Sep 19, 2017 9:34 am