Header Ads Widget

Computing Category

Social Networking project part 2

 


Social Networking website part 2

This is the second part of the social networking project ,if you have not done the first part check it out first here

profile.php

One of the first things that new users may want to do after signing up and logging in is to create a profile.

Here is a program to upload,resize and sharpen the images:


. profile.php

<?php

 require_once 'header.php';

 if (!$loggedin) die();

 echo "<div class='main'><h3>Your Profile</h3>";

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

 if (isset($_POST['text']))

 {

 $text = sanitizeString($_POST['text']);

 $text = preg_replace('/\s\s+/', ' ', $text);

 if ($result->num_rows)

queryMysql("UPDATE profiles SET text='$text' where user='$user'");

 else queryMysql("INSERT INTO profiles VALUES('$user', '$text')");

 }

 else

 {

 if ($result->num_rows)

 {

 $row = $result->fetch_array(MYSQLI_ASSOC);

 $text = stripslashes($row['text']);

}

 else $text = "";

 }

 $text = stripslashes(preg_replace('/\s\s+/', ' ', $text));

 if (isset($_FILES['image']['name']))

 {

 $saveto = "$user.jpg";

 move_uploaded_file($_FILES['image']['tmp_name'], $saveto);

 $typeok = TRUE;

 switch($_FILES['image']['type'])

 {

 case "image/gif": $src = imagecreatefromgif($saveto); break;

 case "image/jpeg": // Both regular and progressive jpegs

 case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;

 case "image/png": $src = imagecreatefrompng($saveto); break;

 default: $typeok = FALSE; break;

 }

 if ($typeok)

{

 list($w, $h) = getimagesize($saveto);

 $max = 100;

 $tw = $w;

 $th = $h;

 if ($w > $h && $max < $w)

 {

 $th = $max / $w * $h;

 $tw = $max;

 }

 elseif ($h > $w && $max < $h)

 {

 $tw = $max / $h * $w;

 $th = $max;

 }

 elseif ($max < $w)

 {

 $tw = $th = $max;

 }

 $tmp = imagecreatetruecolor($tw, $th);

 imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);

 imageconvolution($tmp, array(array(-1, -1, -1),

 array(-1, 16, -1), array(-1, -1, -1)), 8, 0);

 imagejpeg($tmp, $saveto);

imagedestroy($tmp);

 imagedestroy($src);

 }

}

 showProfile($user);

 echo <<<_END

 <form method='post' action='profile.php' enctype='multipart/form-data'>

 <h3>Enter or edit your details and/or upload an image</h3>

 <textarea name='text' cols='50' rows='3'>$text</textarea><br>

_END;

?>

 Image: <input type='file' name='image' size='14'>

 <input type='submit' value='Save Profile'>

 </form></div><br>

</body>

</html>

Explanation of the code:

Beginning the the HTML form at the end of the code,the form has the parameter

enctype='multipart/form-data'.

This helps us send more than one type of data at a time eg: an image and text.There’s also an input type of file, which creates a Browse button that a user can press to select a file to be uploaded.

When the form is submitted, the code at the start of the program is executed.

 The first thing it does is ensure that a user is logged in before the program execution to proceed.


Adding the “About Me” Text

The  Post variable text is checked if there is text that has been added.If so,then it undergoes sanitization for security purposes but If no text was posted, the database is queried to see whether any text already exists in order to prepopulate the textarea for the user to edit it.


Adding a Profile Image

Here the $_FILES system variable is checked to see whether an image has been uploaded. If so, a string variable called $saveto is created,based on the user’s username followed by the extension .jpg. For example, user Jill will cause $saveto to have the value Jill.jpg.

Then  the uploaded image type is examined and is accepted only if it is a jpeg, png, or gif image.

Upon success, the variable $src is populated with the uploaded image using one of the imagecreatefrom functions according to the image type uploaded.

 The image is now in a raw format that PHP can process. If the image is not of an allowed type, the flag $typeok is set to FALSE, preventing the final section of image upload code from being processed.


Processing the Image

First we store the image dimensions using this statement

list($w, $h) = getimagesize($saveto);

If you want a smaller or larger thumbnail we use $max (which is set to 100), which gets the new dimensions which will result in a new image.

The function imagecreatetruecolor is called to create a new, blank canvas $tw wide and $th high in $tmp. imagecopyresampled is called to resample the image

from $src, to the new $tmp


Finally, the image is saved as a jpeg file in the location defined by the variable $saveto, after which we remove both the original and the resized image canvases from memory using the imagedestroy function, returning the memory that was used.


members.php

Using members.php,your users will find other memebers and choose to add them as friends or drop them.Here is  code snippet that does that work.

members.php

<?php

 require_once 'header.php';

 if (!$loggedin) die();

 echo "<div class='main'>";

 if (isset($_GET['view']))

 {

 $view = sanitizeString($_GET['view']);

 if ($view == $user) $name = "Your";

 else $name = "$view's";

echo "<h3>$name Profile</h3>";

 showProfile($view);

 echo "<a class='button' href='messages.php?view=$view'>" .

 "View $name messages</a><br><br>";

 die("</div></body></html>");

 }

 if (isset($_GET['add']))

 {

 $add = sanitizeString($_GET['add']);

 $result = queryMysql("SELECT * FROM friends WHERE user='$add'

 AND friend='$user'");

if (!$result->num_rows)

 queryMysql("INSERT INTO friends VALUES ('$add', '$user')");

 }

 elseif (isset($_GET['remove']))

 {

 $remove = sanitizeString($_GET['remove']);

 queryMysql("DELETE FROM friends WHERE user='$remove' AND friend='$user'");

 }

 $result = queryMysql("SELECT user FROM members ORDER BY user");

 $num = $result->num_rows;

 echo "<h3>Other Members</h3><ul>";

 for ($j = 0 ; $j < $num ; ++$j)

{

$row = $result->fetch_array(MYSQLI_ASSOC);

 if ($row['user'] == $user) continue;

 echo "<li><a href='members.php?view=" .

 $row['user'] . "'>" . $row['user'] . "</a>";

 $follow = "follow";

 $result1 = queryMysql("SELECT * FROM friends WHERE

 user='" . $row['user'] . "' AND friend='$user'");

 $t1 = $result1->num_rows;

 $result1 = queryMysql("SELECT * FROM friends WHERE

 user='$user' AND friend='" . $row['user'] . "'");

 $t2 = $result1->num_rows;

 if (($t1 + $t2) > 1) echo " &harr; is a mutual friend";

 elseif ($t1) echo " &larr; you are following";

 elseif ($t2) { echo " &rarr; is following you";

 $follow = "recip"; }

 if (!$t1) echo " [<a href='members.php?add=" .

 $row['user'] . "'>$follow</a>]";

 else echo " [<a href='members.php?remove=" .

 $row['user'] . "'>drop</a>]";

 }

?>

 </ul></div>

 </body>

</html>

Explanation of the code:

Adding and Dropping Friends

We can be able to add  or drop friends  by looking up the user in the MySQL friends table and either inserting a friend username or removing it from the table.


Listing All The Members

The final section of code issues a SQL query to list all usernames. The code places the number returned in the variable $num before outputting the page heading.


friends.php

This module shows a user's friends and followers.This is made possible by displaying afriend's table of a single user.

All the followers are saved into an array called $followers, and all the people being followed are placed in an array called $following. Then a neat piece of code is used to extract all those who are both following and followed by the user, like this:

$mutual = array_intersect($followers, $following);

Here is the code snippet that does that work:

friends.php

friends.php

<?php

 require_once 'header.php';

 if (!$loggedin) die();

 if (isset($_GET['view'])) $view = sanitizeString($_GET['view']);

 else $view = $user;

 if ($view == $user)

 {

 $name1 = $name2 = "Your";

 $name3 = "You are";

 }

 else

 {

 $name1 = "<a href='members.php?view=$view'>$view</a>'s";

 $name2 = "$view's";

 $name3 = "$view is";

 }

 echo "<div class='main'>";

 // Uncomment this line if you wish the user's profile to show here

 // showProfile($view);

$followers = array();

 $following = array();

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

 $num = $result->num_rows;

 for ($j = 0 ; $j < $num ; ++$j)

 {

 $row = $result->fetch_array(MYSQLI_ASSOC);

 $followers[$j] = $row['friend'];

 }

 $result = queryMysql("SELECT * FROM friends WHERE friend='$view'");

 $num = $result->num_rows;

 for ($j = 0 ; $j < $num ; ++$j)

 {

 $row = $result->fetch_array(MYSQLI_ASSOC);

 $following[$j] = $row['user'];

 }

 $mutual = array_intersect($followers, $following);

 $followers = array_diff($followers, $mutual);

 $following = array_diff($following, $mutual);

 $friends = FALSE;

 if (sizeof($mutual))

 {

 echo "<span class='subhead'>$name2 mutual friends</span><ul>";

 foreach($mutual as $friend)

 echo "<li><a href='members.php?view=$friend'>$friend</a>";

 echo "</ul>";

$friends = TRUE;

 }

 if (sizeof($followers))

 {

 echo "<span class='subhead'>$name2 followers</span><ul>";

 foreach($followers as $friend)

 echo "<li><a href='members.php?view=$friend'>$friend</a>";

 echo "</ul>";

 $friends = TRUE;

 }

 if (sizeof($following))

 {

 echo "<span class='subhead'>$name3 following</span><ul>";

 foreach($following as $friend)

 echo "<li><a href='members.php?view=$friend'>$friend</a>";

 echo "</ul>";

 $friends = TRUE;

 }

if (!$friends) echo "<br>You don't have any friends yet.<br><br>";

 echo "<a class='button' href='messages.php?view=$view'>" .

 "View $name2 messages</a>";

?>

 </div><br>

 </body>

</html>

Post a Comment

0 Comments