Welcome Guest [Log In] [Register]
Add Reply
Friend System
Topic Started: Jul 15 2008, 01:05 AM (411 Views)
Admin
Administrator
Fully working code (I have a slightly better one, might post it later). This is intergrated with my PM System, which I will also post later. If you want you can remove the part where it sends a pm when it sends a friends request (if you cant do that you dont deserve this script).

table
Code:
 

CREATE TABLE `friend_requests` (
`id` int(10) NOT NULL auto_increment,
`username` varchar(225) NOT NULL default '',
`by` varchar(225) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `friends` (
`id` int(10) NOT NULL auto_increment,
`friendname` varchar(225) NOT NULL default '',
`username` varchar(225) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;



friendrequest.php
Code:
 

<title>Friends Request</title>
<head>
<link rel="stylesheet" type="text/css"
href="style/main.css" />
</head>


<blockquote>
<div align="center">
<? include("include/session.php"); ?>
<? include 'including/logo.php'; ?>
<? include 'including/menu.php'; ?>


<table width="922" height="31" border="2">
<tr>
<td height="23" bordercolor="#FFFFFF" bgcolor="#FFFFFF">

<p align="center" class="logotext">Request to be Friend </p>
<p>
<?
session_start(); //starts session

/* Requested Username error checking */


$req_user = trim($_GET['user']);

if ($session->logged_in){ //checks user is logged in

if ($req_user){ //gets username
$username = htmlspecialchars($req_user); //friend
$by = $session->username; //you


$query = mysql_query("INSERT INTO `friend_requests` ( `username` , `by` ) VALUES ( '$username' , '$by' )"); //inserts the request
echo ( "$username has been sent a request you must now wait for it to be accepted" ); //echos completion
} else {
echo ( "No request was made" ); // or no request sent
}
} else {
echo ( "You need to be logged in" ); //not logged in
}
?>

</p></td>
</tr>
</table>
</div>
</blockquote>


friendlist.php
Code:
 

<title>Friends List</title>
<head>
<link rel="stylesheet" type="text/css"
href="style/main.css" />
</head>


<blockquote>
<div align="center">
<? include("include/session.php"); ?>
<? include 'including/logo.php'; ?>
<? include 'including/menu.php'; ?>



<table width="922" height="31" border="2">
<tr>
<td height="23" bordercolor="#FFFFFF" bgcolor="#FFFFFF">



<p align="center" class="logotext">My Friends </p>
<p>
<?
$getfriends = mysql_query( "SELECT * FROM `friends` WHERE `username` = '$session->username'" );
while ($friends = mysql_fetch_array($getfriends))
{
echo "<a href='userinfo.php?user=$friends[friendname]'>$friends[friendname]</a> <br>";
}

?>


</p></td>
</tr>
</table>
</div>
</blockquote>



newfriends.php
Code:
 

<title>Friends Request</title>
<head>
<link rel="stylesheet" type="text/css"
href="style/main.css" />
</head>


<blockquote>
<div align="center">
<? include("include/session.php"); ?>
<? include 'including/logo.php'; ?>
<? include 'including/menu.php'; ?>


<table width="922" height="31" border="2">
<tr>
<td height="23" bordercolor="#FFFFFF" bgcolor="#FFFFFF">


<p align="center"><span class="logotext">Friend Requests </span></p>
<p>
<?
/* Requested Username error checking */
$req_user = trim($_GET['user']);


if ($session->logged_in) { //checks user is logged in
switch ($_GET[friends]) { //allows multiple pages
default:
$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = '$session->username' "); //gets requests
while ($reqs = mysql_fetch_array($get))
{

echo ( "<center><b>Friend Requests</b></center><br>
$reqs[by] wants to be friends with you.<br>
<a href='newfriends.php?friends=accept&user=$reqs[by]'>Accept</a><br/>
<a href='newfriends.php?friends=delete&user=$reqs[by]'>Delete</a><br><br>" ); //displays requests and shows accept delete links
}
break;

case 'accept': //accept page
if ($session->logged_in) { //get username

$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$req_user' , '$session->username') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]' "); // deletes friend request
echo ( "$req_user has been added as a friend and the request has been deleted" ); // echos the completion
}
break; //ends accept page

case 'delete': // delete page
if ($req_user) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$req_user' "); // deletes friend request
echo ( "$req_user's request has been deleted" ); // echos completion
}
break; //ends delete page
} // ends switch
} else {
echo ( "You need to be logged in" ); // not logged in
}
?>

</p></td>
</tr>
</table>
</div>
</blockquote>





add this to the userinfo page
Code:
 

echo "<p class='maintext'><a href='friendrequest.php?user=$req_user_info[username]'>Add as Friend</a></p>";


and I added the two links on the user edit page

Code:
 

<p><a href="newfriends.php">Friend Requests</a> - Accept or Deny Friends Requests from other people. </p>
<p><a href="friendlist.php">My Friends </a>- View a list of all my friends. </p>

Offline Profile Quote Post Goto Top
 
stevecode

Just a little thing, on newfriends.php

Find
$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$req_user' , '$session->username') "); // add to your friends list

and under that add this
$add = mysql_query( "INSERT INTO `friends` (`username` , `friendname`) VALUES ('$req_user' , '$session->username') "); // add to your friends list

That way two entries will go in, one for the person who sent the request and the other who got it. That way both members show up in each others page

Also I would add a new column on the friends table, like
(Friends) and have it like usernamefriendname and friendnameusername and then make it a unique column that way if by a chance the two friends send an invite, again. They won't be re-added as friends.


Offline Profile Quote Post Goto Top
 
Adrian

I suggest to add this in frend request... because without it you can be your own frend...
if($username == $by) {
die('You cant add yourself.');
}
Offline Profile Quote Post Goto Top
 
php-coder
Member Avatar

I am just finalising a few things within a new friends system wich is alot better than this or any others posted on here, its clean, it works 100% and also has the ignore user feature added into it, I'll post it up once i get more time on my hands right now im limited.

Pc~
Offline Profile Quote Post Goto Top
 
scottjcampbell
Member Avatar

php-coder
Oct 6 2009, 04:55 PM
I am just finalising a few things within a new friends system wich is alot better than this or any others posted on here, its clean, it works 100% and also has the ignore user feature added into it, I'll post it up once i get more time on my hands right now im limited.

Pc~
woo hoo!
cannot wait :)
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Friends System · Next Topic »
Add Reply