PHP 5 Social Networking

2.5 (2 reviews total)
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. PHP Social Networking

About this book

Social Networking has quickly become a very popular activity on the Internet, particularly with the emergence of sites like Facebook and MySpace. PHP is a flexible web development language used on a wide variety of sites, including Facebook and Yahoo! But building a social networking site from scratch using PHP involves some complex logic, and also some serious coding.

This book will demonstrate how to create, maintain, and extend a powerful social network site. It shows how to create your own flexible social networking framework, allowing users to sign up, customize their profile, befriend other users, share information, communicate, interact with one another, and much more.

Taking best practices, such as object-oriented programming, and the Model-View-Controller architecture, this book will begin by walking through the development of a simple development framework, which is then used to rapidly build a Social Networking platform, building upon the social features chapter by chapter. At the end of this book, you will have a powerful Social Networking platform loaded with many features which can take the user from signing up and connecting with other users to posting and sharing information. With an API in place, the platform can be extended and used by other Social Networks. This book doesn’t just stop with the code and the logic behind it; useful supplementary information is also covered, such as marketing the Social Network, keeping Search Engine Optimization in mind when developing, backing up and maintaining the site, and how to deal with large amounts of traffic through scaling.

By the end of this book, you will be left fully prepared to build, manage, maintain, and extend a powerful Social Network for your business, hobby, organization, family, and clients.

Publication date:
October 2010


Chapter 1. PHP Social Networking

Welcome to PHP social networking! During the course of this book, we are going to build a flexible social networking site and framework using PHP, which we can easily extend to meet the needs of our social network.

In this chapter, you will learn:

  • More about social networks

  • About existing social networks

  • Existing social networking software

  • Why and when to roll your own system

We will also discuss the social networking website that we will create during the course of this book: DinoSpace—a social network for keepers of pet dinosaurs.


Introduction to social networks

Social networks are now one of the most widely used aspects of the Web and have really taken off over the past few years. Many businesses, organizations, communities, and families are using social networking to promote themselves, to communicate better with others, and to engage with their audience.

Social networking relies upon users building up their own network of contacts on the site. This, in turn, introduces them to new contacts and—on many social networking websites—allows them to be found more easily. Also, this allows new contacts to be recommended or introduced, helping to grow the user's network.

Let's look at an example of how a user's network of contacts can be built up:

This social network representation shows the connections between contacts. It also illustrates how a user may be able to discover friends of a friend and friend recommendations (based on friends in common). This makes it easy for the users to build up their social network, to communicate with new people, or reconnect with lost contacts.

Social networks generally serve two primary functions. Firstly, they allow users to connect with each other and build a contact network, as we have just discussed. They provide a community with collaboration and contribution features as well. This allows the content and information within the social network to be grown by the users themselves. Later in this chapter, we will discuss some of the features available in existing social networks and social networking software, to build up a list of key features we will need to include as well as things we might like to include.

Business logic to social networks

There is some very powerful business logic to using both existing and custom social networks. Creating your own social network or social network tools gives a dedicated customer area, where feedback on products and services can be obtained, for instance, use of support forums to discuss and resolve problems. Areas that allow customers to share tips, resources, and product care tips help promote those products and services.

Examples: Businesses making use of existing social networks and their own social networks

There are some examples of businesses making great use of existing social networks and their own social networking type websites to improve their businesses. Let's have a look at three specific examples.

NameCheap: Twitter

NameCheap is a domain name registrar, and they use Twitter ( for two purposes. Firstly, they collect and respond to feedback from customers mentioning their company, and more prominently, they run various competitions giving away free domain names. These viral competitions encourage more users to follow them, and promote the competition, therefore increasing their brand awareness.

Dell: Twitter

Recently, Dell announced that their Twitter presence ( generated $6.5 million in revenue, with orders being placed as a result of the links or discounts placed on their Twitter feed. More information is available on the Mashable website:

BT: Twitter

British Telecom uses Twitter ( to help improve customer service and manage their reputation. In the most instances I've seen this used, it has primarily been in response to customer complaints, to try and assist them with their problems, and escalate matters such as fault testing and engineer call out. This makes them seem more caring (also emphasized by their choice of Twitter username), increases customer satisfaction by resolving problems more quickly.

Netgear: custom

While not strictly a social network, Netgear have various social aspects to their website, both through a dedicated community area ( and the support section of their website ( The support section integrates community generated content from their discussion forums and brings this into product pages, making it easier for customers to find answers to questions staff have not answered directly. Discussion forum software is also quickly becoming social networking software to an extent, in its own right.


Existing social networks

There are many existing social networks available, some of which are already very popular and have some excellent features. Let's take a look at the most prominent features of some of these more popular sites.


Facebook ( is very much a global social networking website for everyone over the age of 13. It started out for students at Harvard University, branching out to all the universities, and now available for everyone. Features available include:

  • A customizable profile

  • Users can update their statuses

  • Users can connect with other users by adding them as "friends"

  • Statuses of friends can be commented upon and users can indicate that they like a particular status

  • Friends can post messages to each other's profiles

  • Photos can be posted and shared

  • Events can be posted and shared, with attendees sending their RSVPs online

  • Groups can be created and joined, promoting specific activities or interests

  • Topics can be discussed

  • Third-party developers can create their own applications for Facebook, to add more to the platform


LinkedIn ( is a social networking site for business contacts, colleagues, and classmates, which primarily encourages business contacts to connect. Features available on LinkedIn include allowing the users to:

  • Customize their profile

  • Connect with colleagues

  • See how users are connected to other

  • Recommend other users with respect to a job

  • Integrate Twitter with their account profiles

  • Create and view business profiles

  • Third-party developers can create their own applications too (


MySpace ( is a social networking website used primarily by a younger audience. It is very popular with bands, particularly because of how much profiles can be customized with HTML and how music can be embedded within profiles. Features available include:

  • Customizable profiles, complete with:

    • HTML customization: allowing users to customize the colors, look, and feel of MySpace

    • Music integration

    • The user's current mood

    • Comments

  • Groups: small subsets of users

  • MySpace TV: video sharing

  • Integration and development of third-party applications via an (a suite of) API(s). We will discuss these further in Chapter 11, Developing an API.

  • Forums: for discussions.

  • Polls: to get user opinion.


Twitter ( is a micro-blogging social networking website, which primarily deals with very short messages of 140 characters or less. Despite this, it has a large number of prominent features, including:

  • Profiles can be customized, both in terms of colors and background image

  • Users can update their status

  • Users can reply to each other's status updates

  • Users can repost another user's status update, using the ReTweet function

  • Powerful searching based on users replying to each other (@replies) and tagging of tweets with #hashtags

The ease of use and small set of core features have made Twitter very popular.


Existing social networking software

Just like there a number of fantastic social networking sites, there are a number of software systems available as well. These can be used to develop unique social networking sites.


Drupal ( is a popular, freely available, open source content management system. On its own, Drupal can be used to create easy-to-use, easy-to-update websites. By extending this through the thousands of modules that the communities have developed or by creating new modules, we could create almost any type of website we want, ranging from e-commerce to social networking websites.

Drupal does make an excellent candidate for social networking websites, and Packt Publishing has a book published on this subject: Drupal 6 Social Networking (


Elgg ( is an open source social networking platform, complete with functionality for setting up profiles, sharing files, adding friends, blogging, aggregating RSS, content tagging, and social graphs. Elgg also has an API, allowing developers to extend Elgg by adding additional functionality as well as a RESTful API to allow other applications to interact with the platform.


Joomla! ( is another open source content management system, with a range of built-in social networking features. There is also a commercial add-on, the Jomsocial component (, which turns Joomla! into a truly social network.

Hybrid approaches

There are, of course, options available which combine using an off-the-shelf system and a custom system. However, these mainly facilitate extending the functionality of the existing social networking platform or by integrating some of those social aspects with our own website. Such approaches include:

  • Facebook applications: creating applications that are accessed via Facebook's main site, providing additional features to users. For example, a map of dinosaur-friendly restaurants, which are hosted externally by the developer.

  • Facebook connect: Allows websites to interact with Facebook, using it as an authentication protocol, pulling friend data from it, as well as pushing, and pulling status updates to and from Facebook.

  • Out-of-the-box hosted solutions, such as Ning (, that allow users to create and maintain a social network community direct from their web browser.

  • Google OpenSocial: A set of common APIs that make applications for social networks interoperable with supporting social networking sites. It also enables site developers to integrate the API so that other developers can build applications for that site, as well.


Rolling your own

Throughout the course of this book, we are going to create our own social networking site from scratch (sometimes referred to as rolling your own) using PHP, as opposed to using an existing system, product, or platform (such as Drupal and its social networking modules, Elgg, or leveraging existing social networks such as Facebook).

Why roll your own?

There are a number of very popular and successful social networking websites and social networking products out there, so why would we want to create our own? Some of the benefits for us using our own social networking system are as follows:

  • Easier to update and maintain: As we built it, we will know exactly how it works and so we can easily extend and maintain it.

  • Licensing: Other products and options have different licenses, which dictate how the software can be used, extended, and shared with our own system. We can decide that for ourselves.

  • Enhance knowledge: We can build our own system in order to learn from the process.

  • Efficient code: Some existing software packages make use of third-party add-ons, which are not always well optimized for lots of users. By writing our own code, we can ensure we develop in a scalable, efficient way.

  • Provide a service.

  • Improve business.

  • Improve communication.

Easier to update and maintain

Developers who create their own platforms are generally much more familiar with them than with other platforms. As they build them, they know exactly how the platforms work, how to improve, extend, and enhance them. With existing platforms, there is an additional learning curve to developing with them and complications, should the platforms update. With sites such as Facebook, API changes are frequently rolled out, though with existing products, such as Drupal, installing updates is optional.


Depending on the platform or product used, there may be different licenses associated with them. Licenses restrict what can and can't be done with the product, how improvements, extensions or modifications can be released, enforcing specific copyright notices or design guidelines, and of course, with many commercial licenses, costing money.

With self-built platforms, the license is up to us. If we want to release our social networking site code to the public, we can, and we can use the license terms we choose.

Enhance knowledge

By creating a social networking website from scratch, you can enhance your knowledge of PHP, social networking, and work with various other third-party APIs along the way to create a fantastic platform.

Provide a service

There are many ways in which websites and social networks provide additional services that are relevant to the social network or the target audience, though these are often through third-party applications. For example, there are features for both Facebook and LinkedIn that can provide a list of books which a user has read. These provide links to book retailers so more information can be discovered, and the books can be purchased. Additionally, some social networks contain knowledge bases of information, which can be improved by the user.

With existing social networks, any additional service provided either directly through the social network or through third-party applications and plugins would, or could, be restricted in a number of ways. The terms and conditions of the social network would be the main restriction, followed by how the features themselves can be added.

For example, if we wanted to add a map of dinosaur-friendly restaurants to an existing social network, it would rely upon:

  • Data collection: Use provisions with the social networks terms of service

  • Promotion within that social network, which can be a challenge

  • Provisions for third-party applications, which would most likely limit and restrict the functionality and design

  • Design and user interface guidelines enforced by the social network

Improve business

By tapping into the existing user base of established social networks, we can communicate with a new group of users, increasing awareness, and hopefully, improving business. One slight flaw with existing social networks is providing extra enhancements.

Taking Facebook as an example, third-party developers create additional features and embed them as applications, and some of these applications add business functionality. One example allowed users to book a table at a restaurant. The limitation with using Facebook is that before the information is sent to the application, the user is subjected to several dialogues asking for their confirmation. These dialogues are important to prevent abuse and to ensure user data is used properly. However, it is an obstacle for developers. As more and more applications are available, there is more competition for users' attention, which recently has lead to applications requesting that users invite their friends to use it. These mass invitations have the opposite effect, and discourage users from the applications in question.

With our own social network, the data and functionality would be hosted by ourselves. This gives us the freedom to extend the functionality of the social network to help us improve business as we see fit, leading to a more relevant and user friendly social network!

Improve communication

Social networks remove most barriers to communication, such as geographical location (the only barrier which remains, is Internet access). This is the case for both existing and custom social networks. The primary advantage over using our own system is we are less restricted in how we can communicate with users. With existing social networks, you must be connected to the user and restrictions may be imposed over which communication methods you use within the social network or which external communication details are shown to you.

Why use PHP?

PHP is a popular, open source programming language. Also, unlike some other languages, it isn't a framework in its own right, which means we can structure our application however we wish.

Most modern web hosts support PHP and the database platform we will be using with it (MySQL) and although some other languages are gaining popularity (such as the Ruby on Rails framework), hosting for this isn't as common. Facebook, the world's largest social networking website, is written using PHP (albeit with countless customizations, improvements, and extras), as does Yahoo!, which operates a search engine, news portal websites, and various social websites too. Yahoo! also, until recently, employed Rasmus Lerdorf, the creator of the original PHP engine.

This book assumes we have a reasonable understanding of PHP and some knowledge of object-oriented programming, so another good reason for using PHP is skill level.

When to use something else

As we have discussed earlier, there are already a number of fully featured social networking platforms and products available, written in a variety of different programming languages. Sometimes, it is more appropriate to use one of these, such as:

  • When the project has a tight deadline and a base framework isn't already in place. In the interest of time, it would be more appropriate to leverage something else.

  • When there are lots of developers on the project with varying skill levels, a project or platform with plenty of existing documentation available would allow the whole team to be able to get started right away.

  • If the project is for a client and they have a preferred platform.

  • If an existing product has the required features and works in the way required for the project.


Our site: DinoSpace

Throughout the course of this book we are going to develop a social networking site for keepers of pet dinosaurs (of course nobody owns a real pet dinosaur, it would be too expensive, but for the sake of this book, let's pretend!), which we will call DinoSpace. The social network will enable:

  • Keepers of pet dinosaurs to connect with one another

  • Friendships and other custom relationships (for example, walking buddy) to be maintained with other members of the site

  • Users to share stories about their pets

  • Profiles of pet dinosaurs to be created:

    • Statuses to be updated

  • Dinosaur-friendly places to visit to be promoted:

    • Non-keepers of dinosaurs to use the site to promote businesses and events that dinosaur keepers may find useful or interesting

  • Help and support to be provided to fellow Dinosaur keepers in an interactive way

At the end of this book, we will have a flexible social network for owners of pet dinosaurs. Some screens of the final product are shown. First, we have a basic profile page:

Complete with a customizable user status stream:

As well as a range of other features, which we will discuss now.


Feature list

From looking at the features available in existing social networking platforms and products, the following features seem standard throughout most of them, and so we shall try and incorporate them into our social networking website:

  • Status updates: So that users can update their network with their current status

  • Commenting on status updates: So that friends and connections can comment on these status changes

  • Status stream: So that changes to many contacts statuses can be viewed at a glance

  • Friends and relationships: So that users can connect with one another and define the context of the connection, for example, friend, colleague, or even Dino-walking partner

  • Customizable profiles: So that users can build a profile of themselves with custom information about them

  • Groups: So that smaller subsets can be created and nurtured within the site, focusing on specific interests or discussions

  • Messages: So that users can keep in touch with one another

  • Discussions: Encouraging open discussion amongst users

  • Image sharing

  • Video integration and sharing

  • Calendars, events and birthdays: So that users can see upcoming events, create events, and invite friends, perhaps to promote a local T-Rex immunization day at a health center


Users of large social networks such as Facebook typically have a large network of friends (or contacts) and subsequently a large number of updates, particularly when combined with the third-party applications, which can also post status updates on their behalf. To ensure that feeds of updates don't become too cluttered, these updates go through a special service that they have developed, which allows certain applications to be filtered out and tries to ensure the user's stream is more relevant.

This is something we won't be able to implement ourselves. However, Facebook has released a number of their components as open source projects, which could be integrated into our framework, should we wish to make use of some of their solutions to large scale social networking problems.

More information can be found on the Facebook open source page:



In this chapter, we have looked into what social networking is and why we might wish to use it. Also, we discussed why we created our own site from scratch, as opposed to using an existing system. We have also discussed various existing systems and looked at their features to build a list of features, which we want to use in our site, DinoSpace!

In Chapter 2 we will plan and develop our basic development framework, which we will slowly expand over the course of the book to create a powerful social networking website.

About the Author

Latest Reviews

(2 reviews total)
Glanced through ebook. While not everything in it interested me, some sections were fascinating.
In short, I will not recommend this product. The author needs to revise this product. A beginner like me, who is new to the PHP script would not find bearing, the coding structure or framework are very confusing, even the experienced coder will attest to this.