├── .htaccess ├── README.md ├── connect.php ├── dashboard.php ├── default.jpg ├── follow.php ├── hashtag.php ├── index.php ├── logout.php ├── profile.php ├── register.php ├── tweet.php └── unfollow.php /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteCond %{REQUEST_FILENAME} !-f 3 | RewriteCond %{REQUEST_FILENAME} !-d 4 | RewriteRule ^hashtag/([A-Za-z0-9_]+)$ hashtag.php?hashtag=$1 5 | RewriteRule ^([A-Za-z0-9_]+)$ profile.php?username=$1 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Twitter-Like-System-PHP 2 | ======================= 3 | 4 | A simple twitter clone using PHP and a MySQL database. 5 | 6 | Try out the Demo for Twitter-Like-System-PHP 7 | 8 | 9 | 10 | 11 | Setup 12 | ----------- 13 | 1) Create a new database in MySQL - recommended
14 | 2) Run the following SQL code to create the tables in the database 15 | 16 | ``` 17 | CREATE TABLE users( 18 | id int NOT NULL AUTO_INCREMENT, 19 | username varchar(15) NOT NULL, 20 | password varchar(32) NOT NULL, 21 | followers int DEFAULT 0, 22 | following int DEFAULT 0, 23 | tweets int DEFAULT 0, 24 | PRIMARY KEY (id) 25 | ); 26 | 27 | CREATE TABLE following( 28 | id int NOT NULL AUTO_INCREMENT, 29 | user1_id int REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, 30 | user2_id int REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, 31 | PRIMARY KEY (id) 32 | ); 33 | 34 | CREATE TABLE tweets( 35 | id int NOT NULL AUTO_INCREMENT, 36 | username varchar(15) NOT NULL, 37 | user_id int REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, 38 | tweet varchar(140) NOT NULL, 39 | timestamp bigint(20) NOT NULL, 40 | PRIMARY KEY (id) 41 | ); 42 | ``` 43 | 3) Open `connect.php` and edit the database information. The following three setting variables are required in order to establish a connection with the database. 44 | 45 | ``` 46 | $dbuser = "USERNAME"; //Username that is allowed to access the database 47 | $dbpass = "PASSWORD"; //Password 48 | $dbname = "NAME-OF-THE-DATABASE"; //Name of the database 49 | ``` 50 | 51 | 52 | -------------------------------------------------------------------------------- /connect.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dashboard.php: -------------------------------------------------------------------------------- 1 | =86400) 5 | $p = date("F j, Y",$t_time); 6 | elseif ($pt>=3600) 7 | $p = (floor($pt/3600))."h"; 8 | elseif ($pt>=60) 9 | $p = (floor($pt/60))."m"; 10 | else 11 | $p = $pt."s"; 12 | return $p; 13 | } 14 | if($user_id){ 15 | include "connect.php"; 16 | $query = mysql_query("SELECT username, followers, following, tweets 17 | FROM users 18 | WHERE id='$user_id' 19 | "); 20 | mysql_close($conn); 21 | $row = mysql_fetch_assoc($query); 22 | $username = $row['username']; 23 | $tweets = $row['tweets']; 24 | $followers = $row['followers']; 25 | $following = $row['following']; 26 | echo " 27 |
Logout
28 | 29 | 30 | 33 | 37 | 38 |
31 | display picture 32 | 34 |
@$username
35 |
Tweets: $tweets | Followers: $followers | Following: $following
36 |
39 |
40 | 41 | 42 |
43 |
44 |
45 | "; 46 | include "connect.php"; 47 | $tweets = mysql_query("SELECT username, tweet, timestamp 48 | FROM tweets 49 | WHERE user_id = $user_id OR (user_id IN (SELECT user2_id FROM following WHERE user1_id='$user_id')) 50 | ORDER BY timestamp DESC 51 | LIMIT 0, 10 52 | "); 53 | while($tweet = mysql_fetch_array($tweets)){ 54 | echo "
"; 55 | echo "
".getTime($tweet['timestamp'])."
"; 56 | echo ""; 57 | echo ""; 58 | echo "";; 61 | echo ""; 67 | echo ""; 68 | echo "
"; 59 | echo "display picture"; 60 | echo ""; 62 | echo "@".$tweet['username'].""; 63 | $new_tweet = preg_replace('/@(\\w+)/','$0',$tweet['tweet']); 64 | $new_tweet = preg_replace('/#(\\w+)/','$0',$new_tweet); 65 | echo "
".$new_tweet."
"; 66 | echo "
"; 69 | echo "
"; 70 | } 71 | mysql_close($conn); 72 | } 73 | ?> 74 |
75 |
76 |
Made by Simar
77 |
This is Open Source - Fork it on GitHub
78 |
79 |
80 | -------------------------------------------------------------------------------- /default.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iSimar/Twitter-Like-System-PHP/8a9c350cd2c17d8de6309f3ffae8fd5000489199/default.jpg -------------------------------------------------------------------------------- /follow.php: -------------------------------------------------------------------------------- 1 | 5 | =1)){ 17 | include 'connect.php'; 18 | mysql_query("INSERT INTO following(user1_id, user2_id) 19 | VALUES ('$user_id', '$follow_userid') 20 | "); 21 | mysql_query("UPDATE users 22 | SET following = following + 1 23 | WHERE id='$user_id' 24 | "); 25 | mysql_query("UPDATE users 26 | SET followers = followers + 1 27 | WHERE id='$follow_userid' 28 | "); 29 | mysql_close($conn); 30 | } 31 | header("Location: ./".$follow_username); 32 | } 33 | } 34 | ?> -------------------------------------------------------------------------------- /hashtag.php: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Twitter-like-system-PHP 16 | 17 | 18 |

Twitter-Like-System-PHP

19 | Go Home 20 | =86400) 24 | $p = date("F j, Y",$t_time); 25 | elseif ($pt>=3600) 26 | $p = (floor($pt/3600))."h"; 27 | elseif ($pt>=60) 28 | $p = (floor($pt/60))."m"; 29 | else 30 | $p = $pt."s"; 31 | return $p; 32 | } 33 | if(!$user_id){ 34 | echo "Signup"; 35 | } 36 | if($_GET['hashtag']!=""){ 37 | $hashtag = $_GET['hashtag']; 38 | echo "
Tweets with #".$hashtag."
"; 39 | include "connect.php"; 40 | $tweets = mysql_query("SELECT username, tweet, timestamp 41 | FROM tweets 42 | WHERE tweet REGEXP '^#$hashtag' OR tweet REGEXP ' #$hashtag' 43 | ORDER BY timestamp DESC 44 | LIMIT 0, 10 45 | "); 46 | if(mysql_num_rows($tweets)>0){ 47 | while($tweet = mysql_fetch_array($tweets)){ 48 | echo "
"; 49 | echo "
".getTime($tweet['timestamp'])."
"; 50 | echo ""; 51 | echo ""; 52 | echo "";; 55 | echo ""; 61 | echo ""; 62 | echo "
"; 53 | echo "display picture"; 54 | echo ""; 56 | echo "@".$tweet['username'].""; 57 | $new_tweet = preg_replace('/@(\\w+)/','$0',$tweet['tweet']); 58 | $new_tweet = preg_replace('/#(\\w+)/','$0',$new_tweet); 59 | echo "
".$new_tweet."
"; 60 | echo "
"; 63 | echo "
"; 64 | } 65 | } 66 | else{ 67 | echo "
No tweets found.
Be the first one to use #$hashtag
"; 68 | } 69 | mysql_close($conn); 70 | } 71 | else{ 72 | echo "
Sorry, invalid hashtag.
"; 73 | echo "Go Home"; 74 | } 75 | ?> 76 |
77 |
78 |
79 |
Made by Simar
80 |
This is Open Source - Fork it on GitHub
81 |
82 |
83 | 84 | 85 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 5 | =1){ 16 | $password = md5($_POST['password']); 17 | $row = mysql_fetch_assoc($query); 18 | if($password==$row['password']){ 19 | $_SESSION['user_id']=$row['id']; 20 | header('Location: .'); 21 | exit; 22 | } 23 | else{ 24 | $error_msg = "Incorrect username or password"; 25 | } 26 | } 27 | else{ 28 | $error_msg = "Incorrect username or password"; 29 | } 30 | } 31 | else{ 32 | $error_msg = "All fields must be filled out"; 33 | } 34 | } 35 | ?> 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | Twitter-like-system-PHP 47 | 48 | 49 |

Twitter-Like-System-PHP

50 | 56 |
57 |
58 | @ 59 | 60 |
61 | 62 | ".$error_msg.""; 65 | } 66 | ?> 67 |
68 | Register 69 | 70 |
71 |
72 |
73 |
74 |
75 |
Made by Simar
76 |
This is Open Source - Fork it on GitHub
77 |
78 |
79 | 80 | 81 | -------------------------------------------------------------------------------- /logout.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /profile.php: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Twitter-like-system-PHP 16 | 17 | 18 |

Twitter-Like-System-PHP

19 | Go Home 20 | =86400) 24 | $p = date("F j, Y",$t_time); 25 | elseif ($pt>=3600) 26 | $p = (floor($pt/3600))."h"; 27 | elseif ($pt>=60) 28 | $p = (floor($pt/60))."m"; 29 | else 30 | $p = $pt."s"; 31 | return $p; 32 | } 33 | if($_GET['username']){ 34 | include 'connect.php'; 35 | $username = strtolower($_GET['username']); 36 | $query = mysql_query("SELECT id, username, followers, following, tweets 37 | FROM users 38 | WHERE username='$username' 39 | "); 40 | mysql_close($conn); 41 | if(mysql_num_rows($query)>=1){ 42 | $row = mysql_fetch_assoc($query); 43 | $id = $row['id']; 44 | $username = $row['username']; 45 | $tweets = $row['tweets']; 46 | $followers = $row['followers']; 47 | $following = $row['following']; 48 | if($user_id){ 49 | if($user_id!=$id){ 50 | include 'connect.php'; 51 | $query2 = mysql_query("SELECT id 52 | FROM following 53 | WHERE user1_id='$user_id' AND user2_id='$id' 54 | "); 55 | mysql_close($conn); 56 | if(mysql_num_rows($query2)>=1){ 57 | echo "Unfollow"; 58 | } 59 | else{ 60 | echo "Follow"; 61 | } 62 | } 63 | } 64 | else{ 65 | echo "Signup"; 66 | } 67 | echo " 68 | 69 | 70 | 73 | 87 | 88 |
71 | display picture 72 | 74 |
@$username"; 75 | include 'connect.php'; 76 | $query3 = mysql_query("SELECT id 77 | FROM following 78 | WHERE user1_id='$id' AND user2_id='$user_id' 79 | "); 80 | mysql_close($conn); 81 | if(mysql_num_rows($query3)>=1){ 82 | echo " - Follows You"; 83 | } 84 | echo "
85 |
Tweets: $tweets | Followers: $followers | Following: $following
86 |
89 | "; 90 | include "connect.php"; 91 | $tweets = mysql_query("SELECT username, tweet, timestamp 92 | FROM tweets 93 | WHERE user_id = $id 94 | ORDER BY timestamp DESC 95 | LIMIT 0, 10 96 | "); 97 | while($tweet = mysql_fetch_array($tweets)){ 98 | echo "
"; 99 | echo "
".getTime($tweet['timestamp'])."
"; 100 | echo ""; 101 | echo ""; 102 | echo "";; 105 | echo ""; 111 | echo ""; 112 | echo "
"; 103 | echo "display picture"; 104 | echo ""; 106 | echo "@".$tweet['username'].""; 107 | $new_tweet = preg_replace('/@(\\w+)/','$0',$tweet['tweet']); 108 | $new_tweet = preg_replace('/#(\\w+)/','$0',$new_tweet); 109 | echo "
".$new_tweet."
"; 110 | echo "
"; 113 | echo "
"; 114 | } 115 | mysql_close($conn); 116 | } 117 | else{ 118 | echo "
Sorry, this profile doesn't exist.
"; 119 | echo "Go Home"; 120 | } 121 | } 122 | ?> 123 |
124 |
125 |
126 |
Made by Simar
127 |
This is Open Source - Fork it on GitHub
128 |
129 |
130 | 131 | 132 | -------------------------------------------------------------------------------- /register.php: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Twitter-like-system-PHP 17 | 18 | 19 |
20 |

Twitter-Like-System-PHP

21 |

Register For An Account

22 | =1)){ 34 | $password = md5($_POST['password']); 35 | include 'connect.php'; 36 | mysql_query("INSERT INTO users(username, password) 37 | VALUES ('$username', '$password') 38 | "); 39 | mysql_close($conn); 40 | echo "
Your account has been created!
"; 41 | echo "Go Home"; 42 | echo "
"; 43 | echo "
"; 44 | echo "
45 |
46 |
Made by Simar
47 |
This is Open Source - Fork it on GitHub
48 |
49 |
"; 50 | echo ""; 51 | echo ""; 52 | exit; 53 | 54 | } 55 | else{ 56 | $error_msg="Username already exists please try again"; 57 | } 58 | } 59 | else{ 60 | $error_msg="Passwords did not match"; 61 | } 62 | } 63 | else{ 64 | $error_msg="All fields must be filled out"; 65 | } 66 | } 67 | ?> 68 |
69 | @ 70 | 71 |
72 | 73 | 74 | ".$error_msg.""; 77 | } 78 | ?> 79 | 80 | Go Home 81 | 82 |
83 |
84 |
85 |
Made by Simar
86 |
This is Open Source - Fork it on GitHub
87 |
88 |
89 | 90 | 91 | -------------------------------------------------------------------------------- /tweet.php: -------------------------------------------------------------------------------- 1 | 5 | 29 | -------------------------------------------------------------------------------- /unfollow.php: -------------------------------------------------------------------------------- 1 | 5 | =1){ 17 | include 'connect.php'; 18 | mysql_query("DELETE FROM following 19 | WHERE user1_id='$user_id' AND user2_id='$unfollow_userid' 20 | "); 21 | mysql_query("UPDATE users 22 | SET following = following - 1 23 | WHERE id='$user_id' 24 | "); 25 | mysql_query("UPDATE users 26 | SET followers = followers - 1 27 | WHERE id='$unfollow_userid' 28 | "); 29 | mysql_close($conn); 30 | } 31 | header("Location: ./".$unfollow_username); 32 | } 33 | } 34 | ?> --------------------------------------------------------------------------------