PHP Friends request system!

Thread Solved
Reply

Join Date: Jan 2009
Posts: 6
Reputation: shibobo2001 is an unknown quantity at this point 
Solved Threads: 0
shibobo2001 shibobo2001 is offline Offline
Newbie Poster

PHP Friends request system!

 
0
  #1
Jan 14th, 2009
I`m creating a simple social network site, I need to implement a sophisticated friend request system, one which works like facebook, this will work as follows: If James wants John to be his friend, he will click John's name. John will be notified that James has asked to be his friend( notification will appear on John's profile page) John will then need to agree for James to be his friend. Once agreed, they will both be friends.
And I would also like to restrict access to the user's profile to people who that users friends, in orther words, if the logged in user is trying to view a profile of someone who is not their friend, they should not be able to.
Anyone who have idea on how i can solve this i will really appreciate.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: TommyBs is an unknown quantity at this point 
Solved Threads: 11
TommyBs's Avatar
TommyBs TommyBs is offline Offline
Junior Poster in Training

Re: PHP Friends request system!

 
0
  #2
Jan 15th, 2009
Well you would need some kind of 'friend' table that would link together the different user_ids. It could also have an 'active' flag on it that represents whether a request is pending or active (0 for pending, 1 for active). Depending on how the login system works, if you use classes, you could add an array property that contains a list of the logged in users friend_ids. Then when they hit another members page you check that the member id is within the users friend list.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 6
Reputation: shibobo2001 is an unknown quantity at this point 
Solved Threads: 0
shibobo2001 shibobo2001 is offline Offline
Newbie Poster

Re: PHP Friends request system!

 
0
  #3
Jan 15th, 2009
Thanks very much I have a set up a "friends" table on my database, but i am not sure how to link this with user_ids, and also how to present an active flag representing whether the request is pending or not. this is how my login code looks like.
  1. <?php
  2. session_start();
  3.  
  4. $i = $_POST["username"];
  5. $e = $_POST["password"];
  6.  
  7. $conn = mysql_connect("localhost", "PeterJones", "sksksksks");
  8. mysql_select_db("PeterJones");
  9.  
  10. $q=("SELECT username FROM users WHERE username = '$i' AND password ='$e' ");
  11. $result=mysql_query($q) or die(mysql_error());
  12. $row= mysql_fetch_array($result);
  13.  
  14. if(mysql_num_rows($result)==0)
  15.  
  16. {
  17. }
  18. else
  19. {
  20. // Correct password : set up the authentication session variable
  21. // and store the user ID in it
  22. $_SESSION["gatekeeper"] = $i;
  23.  
  24. }
  25.  
  26. mysql_close($conn);
  27.  
  28. ?>
Sorry to be a pain, but could you please show me on the code(php script how this will be represented.

Kind regards
Last edited by peter_budo; Jan 17th, 2009 at 6:10 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: TommyBs is an unknown quantity at this point 
Solved Threads: 11
TommyBs's Avatar
TommyBs TommyBs is offline Offline
Junior Poster in Training

Re: PHP Friends request system!

 
0
  #4
Jan 15th, 2009
Ok so for the friends table (this is a basic example) you could have 3 fields:

user_id(int), friend_id(int), active(bool)

when a user decides to add a friend you put the original user_id in the user_id field. The id of the member they want to add goes in the friend_id field and set active to 0. Then send a notification to the user with id 'friend_id' that 'user_id' wants to be their friend. If they accept update that row on the table so that active = 1.

then when a user signs in create an array of all their friends. (this is where someone else might have a better idea of table structure) but what you would do is something like

  1. <?php
  2. //Your login code here
  3. $user_id = $id; //id of logged in user however u store this
  4. $arrFriendlist = new array();
  5. $q = @mysql_query("SELECT friend_id FROM friends WHERE user_id = $user_id AND active = 1");
  6. while($r = @mysql_fetch_array($q)){
  7. array_push($arrFriendList, $r['friend_id']);
  8. }
  9. //also have to get friends where request was reversed
  10. $q = @mysql_query("SELECT user_id FROM friends WHERE friend_id = $user_id AND active = 1");
  11. while($r = @mysql_fetch_array($q)){
  12. array_push($arrFriendList, $r['user_id']);
  13. }
  14. ?>

Then when a user hits a member page and you want to check if they are friends, get the id of the member they are trying to view and do:
  1. <?php
  2. $mem_id = $_GET['id']; // GRab the id of member you want to view
  3. if(in_array($mem_id,$arrFriendList)){
  4. //code to show member details
  5. }else{
  6. echo "sorry but this user isn't your friend";
  7. }
  8. ?>
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 6
Reputation: shibobo2001 is an unknown quantity at this point 
Solved Threads: 0
shibobo2001 shibobo2001 is offline Offline
Newbie Poster

Re: PHP Friends request system!

 
0
  #5
Jan 27th, 2009
thanx for your previous advice on this!!!
Still on the same issue, i need to send a notification whereby if John wants to be Peter's friend, John will click on Peter's name hyperlink "Make Friend", then a notification will be sent to peter and will appear on Peter,s profile page, then once Peter has accepted friendship, these two people can then become friends, then ofcause the "friends" table will be updated, how do I write a PHP code for this, so far

I have got:
  1. <?php
  2. session_start();
  3. $a = $_SESSION["gatekeeper"];
  4. $conn = mysql_connect("localhost", "PeterJones", "PeterJones");
  5. mysql_select_db("JonesPeter");
  6.  
  7. if ( !isset ($_SESSION["gatekeeper"]))
  8. {
  9. echo "You're not logged in. Go away!";
  10.  
  11. }
  12. else
  13. {
  14.  
  15. $result=mysql_query("SELECT * FROM users WHERE username = '$a'");
  16.  
  17. while($row=mysql_fetch_array($result))
  18. {
  19. echo "You are logged in as!";
  20. echo " $row[username] <br />";
  21.  
  22. // How do I make the currently user's online friends to display like this????? Please help me on this one as well!!!
  23. echo " Online friends <br> <br />";
  24. //John, Josh, Joseph etc ???? Please help me on this one as well!!!!
  25.  
  26. echo " Search for your Friends <br> <br />";
  27. }
  28.  
  29. }
  30.  
  31. ?>
  32.  
  33. <form method="post" action="search.php"
  34. <label for="username">Name</label>
  35. <input type="text" name="username" />
  36. <input type="submit" value="Search" />
  37. </form>
  38. </body>
  39.  
  40. <div>
  41. <a href="logout.php"> Logout</a>
Last edited by peter_budo; Jan 30th, 2009 at 2:19 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: TommyBs is an unknown quantity at this point 
Solved Threads: 11
TommyBs's Avatar
TommyBs TommyBs is offline Offline
Junior Poster in Training

Re: PHP Friends request system!

 
0
  #6
Jan 28th, 2009
Ok so assuming $arrFriendList from the previous example holds all the user_ids of the friends you then use these in a query against the user table. Again there will be a more efficient way to do this probably but it would go something like:
  1. $Friends = ""; //string of friends
  2. foreach( $arrFriends as $item=>$value){
  3. $q = @mysql_query("SELECT username, user_id FROM users WHERE user_id = $value");
  4. while($r = @mysql_fetch_array($q)){
  5. $Friends .= $r['username']."<br />";
  6. }
  7. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1
Reputation: Charca is an unknown quantity at this point 
Solved Threads: 1
Charca Charca is offline Offline
Newbie Poster

Re: PHP Friends request system!

 
0
  #7
Jan 29th, 2009
Hi, I'm Maxi from Argentina, and I'm here looking for something very similar to what shibobo2001 is asking for.
In my case I'm trying to integrate a phpBB forum with a custom portal I'm developing, but the friend system of phpBB doesn't really works for me.
phpBB comes with this friend & foe system that adds people to your profile in your friend/foe list, but the user you are becoming a friend of, doesn't need to accept any request or something to appear in your list. And your friend doesn't even know you've added him to your list unless he visit your profile and see his name in there, 'cause you don't appear in his friend list either.

And I'm looking for something more reciprocal, a system that works the way Facebook, MySpace, or any other social network does. That's why I've been trying to make some changes:

First of all I added a new colum to the "phpbb_zebra" table, the table that phpBB use for this friend & foe system. This new colum (called "active"), is to verificate if the relationship is pending (0), or active (1), just like TommyBs said in his first post.
So the phpbb_zebra table is now like this:

  1. +------------------+---------------------+--------+-----------+
  2. | Field | Type | Null | Default |
  3. +------------------+---------------------+--------+-----------+
  4. | user_id | mediumint(8) | NO | 0 |
  5. | zebra_id | mediumint(8) | NO | 0 |
  6. | friend | tinyint(1) | NO | 0 |
  7. | foe | tinyint(1) | NO | 0 |
  8. | active | tinyint(1) | NO | 0 |
  9. +------------------+---------------------+--------+-----------+

Don't worry about the foe colum, it's just the way phpBB has to determinate the relationship, if the foe field is set to 1, then the friend field is set to 0, wich means the zebra_id user is a foe of the user_id user.
Anyway I deactivate the foe list functionality in phpBB, so this field is always set to 0.

Now, all the code I've made is for my custom portal, it's just PHP and it has nothing to do with phpBB, so it won't affect the way the default system works (and you can use this even if you're not using phpBB integration).

To send a request to the user I want to become a friend of, I use this:

  1. // login to the database
  2.  
  3. $query="SELECT * FROM phpbb_zebra WHERE (user_id = '$_SESSION[user_id]' AND zebra_id = '$profile_id') OR (zebra_id = '$_SESSION[user_id]' AND user_id = '$profile_id')";
  4. $result = mysql_query($query,$cnx);
  5. $row = mysql_num_rows($result);
  6.  
  7. if ($row == 1) { // means the relationship exist, whether it's active or not
  8. while($row = mysql_fetch_assoc($result)) {
  9. if ($row['active'] == 1) {
  10. echo "You are a friend of this user.";
  11. } else {
  12. echo "You've already sent a friend request to this user";
  13. }
  14. }
  15. } else { // if the relationship don't exist
  16. echo "<a href='add_friend.php?zebra_id=".$profile_id."'>Add this user as a friend</a>";
  17. }

This could be placed at the profile of every user. First, the query check if the relationship exist, to avoid sending requests to users that are already your friends, or to users you've sent a request before. In case this two conditions are false, then you can see the link to the add_friend.php script.
For this example, $_SESSION['user_id'] is you, the user logged in into the site, and $profile_id is the user you want to become a friend of.

Now, add_friend.php will be something like this:

  1. // login to the database
  2.  
  3. if ((!isset($_GET['zebra_id'])) || (!isset($_SESSION['user_id']))) {
  4. header("Location: index.php"); exit(); // the zebra_id variable is not set in the URL, or there is no user logged in, so get out the page
  5. }
  6.  
  7. $profile_id = $_GET['zebra_id'];
  8.  
  9. $query="SELECT * FROM phpbb_zebra WHERE (user_id = '$_SESSION[user_id]' AND zebra_id = '$profile_id') OR (zebra_id = '$_SESSION[user_id]' AND user_id = '$profile_id')";
  10. $result = mysql_query($query,$cnx);
  11. $row = mysql_num_rows($result);
  12.  
  13. if ($row == 1) { // check again for the existence of the relationship
  14. while($row = mysql_fetch_assoc($result)) {
  15. if ($row['active'] == 1) {
  16. echo "You are a friend of this user.";
  17. } else {
  18. echo "You've already sent a friend request to this user";
  19. }
  20. }
  21. } else { // the relationship don't exist, so it's time for the real job
  22. $insert_query = "INSERT INTO phpbb_zebra ('user_id', 'zebra_id', 'friend') VALUES ('$_SESSION[user_id]', '$profile_id', '1')";
  23. $insert_result = mysql_query($insert_query,$cnx);
  24. if (!$insert_result) {
  25. // error here
  26. } else {
  27. echo "The request has been sent.";
  28. }
  29. }

To notice users they have new friends requests:

  1. // login to the database
  2. $query = "SELECT * FROM phpbb_zebra WHERE zebra_id = '$_SESSION[user_id]' AND active = '0'";
  3. $result = mysql_query($query,$cnx);
  4. $row = mysql_num_rows($result);
  5. if ($row > 0) { // there are new requests
  6. echo "<a href='requests.php'>You have ".$row." new friend requests</a>";
  7. } else {
  8. echo "No new requests.";
  9. }

The rest is very simple, the page requests.php show all the new friends requests (rows where zebra_id is the id of the user logged in and viewing his requests, and where the active field is set to 0). All you have to do is give the user two options, to accept or to reject the request. If he accepts, just uptade that row on the data base and change the active field from 0 to 1. And if he rejects the request, you can delete that row or you can change the active value to something like 9, or anything other than 0 or 1.
The difference is that if you delete the row, it's like the request never existed, so the same user can make the request again, and if he gets rejected again, he can do it one more time, and so.
But if you change the active value to 9 (or 8, or 7, or 6...), to the requesting user, it will appear as pendient, but the requested one don't have to worry about being requested by the same user again.

Well, that's all. I really hope this can help you, and if not, you can ask all the questions you want and I'll do my best to answer them.
I know this might not be the best way to implement this functionality, so feedback and corrections to the code are also appreciated =)

And sorry about my english

.:ChrC:.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 6
Reputation: shibobo2001 is an unknown quantity at this point 
Solved Threads: 0
shibobo2001 shibobo2001 is offline Offline
Newbie Poster

Re: PHP Friends request system!

 
0
  #8
Feb 5th, 2009
Thanx for your previous help on this,

I am finally making progress with my site!!

Could you please help me with the below
I am looking to implement a birthday notification system working similar to facebook, i would like the birthdays of the users or friends in my table to display as follows.
January:
February:
10th-James Smith
16th-Luke Samuel
19th-Jim Jones
March:
April:
etc

could you please help me write the script for this, or a sample script so i can get some ideas!

Regards
shibobo2005
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC