PHP Example Tutorial (1): Building a micro-blogging service based on PHP

Source: Internet
Author: User
Tags header integer mysql mysql in php file php and php and mysql php example

If you've ever noticed, Twitter is one of the biggest buzz events in the Web 2.0 world. In short, Twitter (a service offered on twitter.com) is a simple microblogging service that allows users to post up to 140 characters (called tweets) and answer questions like "What are you doing now?" Users can follow the people they are interested in, as well as their followers. In this way, information can be published to followers or widely forwarded.

A random glance at a Twitter account reveals that users often post tweets about many different topics, from everyday life (such as "I'm eating sandwiches") to more mundane topics. These are often embedded with links to images, media files, and logs. These URLs are often shortened by services such as tinyurl, primarily to make the total number of characters in a post no more than 140 characters.

Many people like Twitter, making the short format an art form and even talking to other users (such as directing their comments to @user). Starting with this simple starting point, there has been a surge of mobile apps and other tools that support Twitter. There are even awards for the most interesting, outstanding, and informative tweets, as well as online apps that track the status of different Twitter applications.

Many other sites and services, such as LinkedIn and Facebook, now allow users to update their current status in a way that mimics Twitter. In other words, it takes a short message to update the status on Facebook, and, of course, the status is usually the answer to the question "What are you doing now"?

Adding a microblogging or status update tool to your own site does not require a lot of work, but it can be fun and easy to communicate with users. The goal of this article is to show how to achieve this. However, you first need to make some assumptions about you.

First, let's say you know something about PHP and MySQL. Also, let's say you have access to a local Apache Web server running PHP and MySQL. For this article, I'm developing on a MacBook products using Macintosh, Apache, MySQL, and PHP (Mamp), a free program that packs the entire development environment into one package. However, you should be able to develop on microsoft®windows® or linux® without any difficulty. Finally, let's say you already have an application that you can run immediately, and there are a number of users that you intend to add to the application in some way. To do this, I've simplified some aspects of the application that focus on the user (such as logging in, managing personal files, and so on) and focusing on posts.

Designing the backend for an application

In short, the Twitter service is centered on two nouns: users and messages. If you've built an application and want to add Twitter-like services to your application, you probably already have user-management capabilities. If not, there is a way to use a database table (a primary key, usually an integer), a username (also unique), an e-mail address, and a password to identify each user.

Tweets (i.e. posts) are stored in a posts table, each posting has a primary key (a sequential integer), a foreign key relationship to the user who issued the post, the posting itself (limited to a certain number of characters), and a date/time stamp.

The most confusing is the database table that displays the user's following relationship. There is a way to record the user ID and the follower ID, so that the application can quickly build a list of followers and easily forward the information to other users who have been registered to follow a user.

Once you understand the content, you can start building these 3 database tables now. Use the SQL code in Listing 1 to create the first table, the Users table (if you already have a users table, you can skip this step).


Listing 1. Users Table
				
CREATE TABLE ' users ' (
' ID ' INT not NULL auto_increment PRIMARY KEY,
' Username ' VARCHAR (255) Not NULL,
' Email ' VARCHAR (255) Not NULL,
' Password ' VARCHAR (8) Not NULL,
' Status ' ENUM (' active ', ' inactive ') not NULL
) ENGINE = MYISAM;


Here is the second table, the posts table.


Listing 2 posts table
				
CREATE TABLE ' posts ' (
' ID ' INT not NULL auto_increment PRIMARY KEY,
' user_id ' INT not NULL,
' Body ' VARCHAR (140) is not NULL,
' Stamp ' DATETIME not NULL
) ENGINE = MYISAM;

Listing 3 shows the last table, the following table. Note that this table has two primary keys.


Listing 3 following table
				
CREATE TABLE ' following ' (
' user_id ' INT not NULL,
' follower_id ' INT not NULL,
PRIMARY KEY (' user_id ', ' follower_id ')
) ENGINE = MYISAM;

Then, first create a file named header.php, and put all the connection strings for MySQL in the file. If you already have a file like this, you can skip this step. Be sure to include this file everywhere because you need it in the future. Listing 4 shows the contents of this file.


Listing 4: Sample header.php file
				
$SERVER = ' localhost ';
$USER = ' username ';
$PASS = ' password ';
$DATABASE = ' microblogger ';


if (!) ( $mylink = mysql_connect ($SERVER, $USER, $PASS))) {
Echo " Please contact your system's admin for more help\n;
Exit
}

mysql_select_db ($DATABASE);

Keep in mind that you can also add any other type of security check to this header.php file at your own discretion. For example, you can check whether a user ID is set in a session variable (indicating that the user is logged in). If the user is not logged on, the user can be redirected to the login page. This is not discussed in depth, but it is easy to add security checks when needed.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.