PHP example tutorial (1): Build a PHP-based microblog service

Source: Internet
Author: User
If you have noticed this, you will know that Twitter is one of the biggest buzz events in the world of Web. To put it simply, Twitter (a service provided on Twitter.com) is a simple microblog service. Users can send up to 140 posts. if you have noticed this, it will be known that Twitter is one of the world's largest sensation in Web 2.0. Simply put, Twitter (Twitter.com
Is a simple microblog service. Users can post up to 140 characters (called tweet) and answer "what are you doing now ?"
And so on. Users can follow the people they are interested in and have their own followers. In this way, information can be published to followers or widely forwarded.

You can view a Twitter account at will to find that users often publish tweets about many different topics, from daily life (for example
"I am eating sandwiches. Images, media files, and log links are often embedded. These URLs are often used by TinyURL
And other services, mainly to make the total number of characters of The Post cannot exceed 140 characters.


Many people like Twitter, making the ultra-short format an artistic form, and even using it to talk to other users (for example, directing their comments
@ User ). From this simple start point, a large number of mobile applications and other tools supporting Twitter have emerged. There are even the most interesting, excellent, and detailed tweets
The awards also include online applications that track the status of different Twitter applications.


Many other sites and services, such as LinkedIn and Facebook, allow users to update their current status in a Twitter-like manner. In other words
Facebook uses short messages to update the status. of course, the status usually answers questions such as "what are you doing now.


Adding a micro-blog or status update tool to your website does not require much work, but it can bring users pleasure and simple communication methods. The goal of this article is to demonstrate how to achieve this goal. However, you must first make some assumptions.


First, assume you have some knowledge about PHP and MySQL. Assume that you can access a local Apache Web running PHP and MySQL.
Server. For this article, I am using the MacBook Products of the Macintosh, Apache, MySQL, and PHP (MAMP)
The free program packages the entire development environment into a package. However, you should be able? Windows? Or Linux?
. Finally, assume that you already have an application that can be run immediately. this application now has some users who intend to use the microblog or tweeting
Add to the application. To this end, I simplified my applications and focused on some aspects of users (such as logon and personal file management), but on posts.


Design the backend of the application


In short, the Twitter service is centered on users and messages. If you have already built an application and want
You may already have the user management function. If not, use a database table (a primary key, usually an integer) and a user name (also unique) in a certain way) and an email address and password.


Tweet (I .e. post) is stored in a posts
In the table, each subscriber has a primary key (a continuous integer), a foreign key relationship pointing to the user who sent the post, and the subscriber itself (limited to a certain number of characters) and date/time stamp.


The most confusing thing is to display the database tables that follow the user relationship. Here, we need to record the user ID and followers in some way.
ID, so that the application can quickly create a list of followers and easily forward information to other users registered to follow a user.


After understanding this, you can create these three database tables. Use the SQL code in listing 1 to create the first table, that is, the users table.
You can skip this step ).


List 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;



The second table, posts table, is shown below.


 List 2. posts table







Create table 'posts '(

'Id' int not null AUTO_INCREMENT primary key,
'User _ id' int not null,

'Body' VARCHAR (140) not null,
'Stamp' DATETIME NOT NULL

) ENGINE = MYISAM;



Listing 3 shows the last table, namely 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;



Create a file named header. php
To the file. If you already have such a file, skip this step. This file must be included everywhere because it will be used in the future. Listing 4 shows the content of this file.


Listing 4. Example header. php file







$ SERVER = 'localhost ';

$ USER = 'username ';
$ PASS = 'password ';
$ DATABASE = 'microblogger ';



If (! ($ Mylink = mysql_connect ($ SERVER, $ USER, $ PASS ))){

Echo "Sorry, cocould not connect to database.


Please contact your system's admin for more help \ n ";
Exit;
}


Mysql_select_db ($ DATABASE );






Remember, you can add any other types of security checks to this header. php file at will. For example, you can check whether a user is set in a session variable.
ID (indicating that the user has logged on ). If you are not logged on, you can redirect the user to the logon page. This article will not discuss this in depth, but it is easy to add security checks when necessary.



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.