小型 Twitter 的系統 原始碼+註釋,PHP
阿新 • • 發佈:2019-01-29
今天重新吧 小型twitter系統的原始碼 認真研究了一邊 算是熟悉php把
為今後一個月的畢業設計做打算
下載
http://dl.vmall.com/c0nkwafdqz
index
<?php session_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> <?php if (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使用者的五條post if (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>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>Sorry, could not connect to database.</h3><br/> Please contact your system's admin for more help\n"; exit (); } mysql_select_db ( $DATABASE ); ?>
users
<?php session_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>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>
<?php
function 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
<?php
session_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" );
?>
<?php
session_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" );
?>