search
HomeBackend DevelopmentPHP Tutorial小型 Twitter 的系统 流碼+註釋,PHP

小型 Twitter 的系统 源碼+註釋,PHP

?

今天重新吧 小型twitter系統的源碼 認真研究了一邊 算是熟悉php把?

爲今後一個月的畢業設計做打算

?

下載

http://dl.vmall.com/c0nkwafdqz

?

index

?

<?phpsession_start ();include_once ('header.php');include_once ('functions.php');$_SESSION ['userid'] = 1;//设置session真正情况是在登录的时候设置?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>Microblogging Application</title></head><p>	<a href='users.php'>see list of users</a></p><?phpif (isset ( $_SESSION ['message'] )) {//如果session中设置了message就显示出来.然后释放	echo "<b>" . $_SESSION ['message'] . "</b>";	unset ( $_SESSION ['message'] );}?><form method='post' action='add.php'>	<p>Your status:</p>	<textarea name='body' rows='5' cols='40' wrap=VIRTUAL></textarea>	<p>		<input type='submit' value='submit' />	</p><?php$users = show_users($_SESSION['userid']);//显示用户follow的用戶if (count($users)){	$myusers = array_keys($users);//返回數組中所有的key}else{	$myusers = array();}$myusers[] = $_SESSION['userid'];//應該在myusers數據末尾添加用戶自己$posts = show_posts($myusers,5);//顯示用戶follow用戶的五條postif (count ( $posts )) {	?><table border='1' cellspacing='0' cellpadding='5' width='500'><?php	foreach ( $posts as $key => $list ) {		echo "<tr valign='top'>\n";		echo "<td>" . $list ['userid'] . "</td>\n";		echo "<td>" . $list ['body'] . "<br/>\n";		echo "<small>" . $list ['stamp'] . "</small></td>\n";		echo "</tr>\n";	}	?></table><?php} else {	?><p>		<b>You haven't posted anything yet!</b>	</p><?php}?><h2 id="Users-you-re-following">Users you're following</h2><?php$users = show_users ( $_SESSION ['userid'] );if (count ( $users )) {	?><ul><?php	foreach ( $users as $key => $value ) {		echo "<li>" . $value . "</li>\n";	}	?></ul><?php} else {	?><p>		<b>You're not following anyone yet!</b>	</p><?php}?></form></body></html>


headers

?

?

<?php$SERVER = 'localhost:3306';$USER = 'root';$PASS = 'root';$DATABASE = 'tweet';if (! ($mylink = mysql_connect ( $SERVER, $USER, $PASS ))) {	echo "<h3 id="Sorry-could-not-connect-to-database">Sorry, could not connect to database.</h3><br/>	Please contact your system's admin for more help\n";	exit ();}mysql_select_db ( $DATABASE );?>

users

<?phpsession_start ();include_once ("header.php");include_once ("functions.php");?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>Microblogging Application - Users</title></head><body>	<h1 id="List-of-Users">List of Users</h1><?php$users = show_users ();$following = following(1);if (count ( $users )) {	?><table border='1' cellspacing='0' cellpadding='5' width='500'><?php	foreach ( $users as $key => $value ) {//=>指的是获取数组内某一个单元内的元素的内容,		echo "<tr valign='top'>\n";		echo "<td>" . $key . "</td>\n";//顯示id		echo "<td>" . $value;//顯示id對應的值也就是value					if (in_array ( $key, $following )) {//檢查key是否在following中 然后根据状态显示不同的值显示不同的信息 生成不同的指向action的链接			echo " <small>		<a href='action.php?id=$key&do=unfollow'>unfollow</a>		</small>";		} else {			echo " <small>		<a href='action.php?id=$key&do=follow'>follow</a>		</small>";		}		echo "</td>\n";		echo "</tr>\n";	}	?></table><?php} else {	?><p>		<b>There are no users in the system!</b>	</p><?php}?></body></html>



?

?

<?phpfunction add_post($userid, $body) {	$sql = "insert into posts (user_id, body, stamp) 			values ($userid, '" . mysql_real_escape_string ( $body ) . "',now())";		$result = mysql_query ( $sql );}function show_posts($userid, $limit = 0) {	$posts = array ();		$user_string = implode ( ',', $userid );	$extra = " and id in ($user_string)";		if ($limit > 0) {		$extra = "limit $limit";	} else {		$extra = '';	}		$sql = "select user_id,body, stamp from posts 		where user_id in ($user_string) 		order by stamp desc $extra";	echo $sql;	$result = mysql_query ( $sql );		while ( $data = mysql_fetch_object ( $result ) ) {		$posts [] = array (				'stamp' => $data->stamp,				'userid' => $data->user_id,				'body' => $data->body 		);	}	return $posts;}/** * 显示用户 * 如果user_id =0,直接显示所有用户 * 如果user id >0,显示改用户follow的用户id * @param unknown_type $user_id * @return multitype:|multitype:NULL */function show_users($user_id = 0) {	if ($user_id > 0) {		$follow = array ();		$fsql = "select user_id from following				where follower_id='$user_id'";//從follow中選出該id的follower		$fresult = mysql_query ( $fsql );						while ( $f = mysql_fetch_object ( $fresult ) ) {//把結果作爲一個對象傳入					array_push ( $follow, $f->user_id );//把f中的user_id字段放到follow中		}					if (count ( $follow )) {			$id_string = implode ( ',', $follow );//以","作爲分割符來加工這個字符串,爲了拼接後面的sql			$extra = " and id in ($id_string)";					} else {			return array ();		}	}		$users = array ();	$sql = "select id, username from users 		where status='active' 		$extra order by username";//從user表中選出follower的 id 和 name		$result = mysql_query ( $sql );		while ( $data = mysql_fetch_object ( $result ) ) {		$users [$data->id] = $data->username;//想user中填入用戶名	}	return $users;}/** * 搜索出用户follow的用户的id * @param unknown_type $userid * @return multitype: */function following($userid) {	$users = array ();		$sql = "select distinct user_id from following	where follower_id = '$userid'";	$result = mysql_query ( $sql );		while ( $data = mysql_fetch_object ( $result ) ) {		array_push ( $users, $data->user_id );	}		return $users;}function check_count($first, $second) {	$sql = "select count(*) from following	where user_id='$second' and follower_id='$first'";	$result = mysql_query ( $sql );		$row = mysql_fetch_row ( $result );	return $row [0];}function follow_user($me, $them) {	$count = check_count ( $me, $them );		if ($count == 0) {		$sql = "insert into following (user_id, follower_id)		values ($them,$me)";				$result = mysql_query ( $sql );	}}function unfollow_user($me, $them) {	$count = check_count ( $me, $them );		if ($count != 0) {		$sql = "delete from following		where user_id='$them' and follower_id='$me'		limit 1";				$result = mysql_query ( $sql );	}}?>


add

?

?

<?phpsession_start ();include_once ("header.php");include_once ("functions.php");$userid = $_SESSION ['userid'];$body = substr ( $_POST ['body'], 0, 140 );add_post ( $userid, $body );$_SESSION ['message'] = "Your post has been added!";header ( "Location:index.php" );?>


<?phpsession_start ();include_once ("header.php");include_once ("functions.php");/**  处理follow动作 */$id = $_GET ['id'];//获取get 方法传来的值 $_POST是post$do = $_GET ['do'];switch ($do) {	case "follow" :		follow_user ( $_SESSION ['userid'], $id );		$msg = "You have followed a user!";//设置信息		break;		case "unfollow" :		unfollow_user ( $_SESSION ['userid'], $id );		$msg = "You have unfollowed a user!";		break;}$_SESSION ['message'] = $msg;//在session中发送信息header ( "Location:index.php" );?>



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)