Create a Twitter Tweets like publishing system
This post guides you to create your own Twitter. Well that's not completely true but this post will guide you to create a Twitter Tweets like publishing System. Using this system, your users can register and sign in to their profiles and can update their tweets just like in twitter. This system is just designed to show the user who tweeted and at what time. You can add extra functionalities. The complete system is based of PHP and MYSQL and a lot of CSS styles. This system contains mainly 3 files, firstly the config.php, secondly the index.php and lastly the profile.php. After uploading the files to your web server, first edit the details of the config.php file, namely the database user,name and password. The run in on the web browser, just like http://www.xyz.com/config.php now you are just good to go. Your users can access the system by just going to your url. i.e. http://www.xyz.com/.
Demo
Lets Create the Config.php file
Lets Create the index.php file
Lets Create the profile.php file
Alternatively you can download the whole code bundled in a single zip by clicking here.
Feel free to report bugs, we will be glad to fix them up ! :)
Demo
The Home Page of the system where user can login
The Profile page of the user where user can post their tweets
Lets Create the Config.php file
<?php $dbhost = 'localhost';// Database Host. $dbuser = 'sid';// Database User. $dbpass = 'Sidharth12';// User's Password. $dbname = 'sample'; // Database Name $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(!$conn) { die('Could not connect: ' . mysql_error()); } $sql='CREATE TABLE USER ('. 'username VARCHAR(20) NOT NULL,'. 'password VARCHAR(20) NOT NULL,'. 'primary key(username))'; mysql_select_db($dbname); $retval = mysql_query($sql,$conn); if(! $retval ) { die('Could not create table: ' . mysql_error()); } $sql='CREATE TABLE USER_DATA ('. 'id INT NOT NULL AUTO_INCREMENT, '. 'username VARCHAR(20) NOT NULL,'. 'tweet VARCHAR(200) NOT NULL,'. 'time VARCHAR(20) NOT NULL, '. 'primary key ( id ))'; mysql_select_db($dbname); $retval = mysql_query($sql,$conn); if(! $retval ) { die('Could not create table: ' . mysql_error()); } echo "Tables created successfully<br>"; ?>
Lets Create the index.php file
<?php $dbhost = 'localhost';// Database Host. $dbuser = 'sid';// Database User. $dbpass = 'Sidharth12';// User's Password. $dbname = 'sample';//Database Name $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } if(isset($_POST['in'])) { $sql = 'SELECT username, password from USER where username=\''.$_POST['name'].'\''; mysql_select_db($dbname); $retval = mysql_query( $sql, $conn ); if(! $retval ) { echo 'Invalid Username Entered, Please Check Again'; } $row = mysql_fetch_row($retval); //echo $row[0].' '.$row[1]; if($_POST['pass']==$row[1]) { echo '<script>window.alert(\'Logging you please wait!\');</script>'; session_start(); $_SESSION['user'] = $_POST['name']; $_SESSION['time'] = time(); ?> <meta http-equiv="refresh" content="1;URL=profile.php" /> <?php } else { echo '<script>window.alert(\'Invalid Username Entered, Please Check Again\');</script>'; } } else if (isset($_POST['up'])) { $sql = "INSERT INTO user ". "VALUES('".$_POST['name']."','".$_POST['pass']."')"; mysql_select_db($dbname); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } echo '<script>window.alert(\'Logging you please wait!\');</script>'; session_start(); $_SESSION['user'] = $_POST['name']; $_SESSION['time'] = time(); ?> <meta http-equiv="refresh" content="1;URL=profile.php" /> <?php } ?> <html> <head> <title> Twitter Tweet System</title> <style> #container { position:relative; width:700px; left:240px; background-color: lightgoldenrodyellow; padding-left: 30px; padding-right: 30px; border-bottom-left-radius: 30%; border-top-right-radius: 20%; } #sidebar_box { position:relative; top:30px; background-color: cadetblue; width: 200px; height: 100px; PADDING: 10 10 10 10; BORDER-RADIUS: 10px; box-sizing: content-box; box-shadow: 2px 2px 2px chocolate; } #main_box { position:relative; top: -106px; left: 230; background-color: cadetblue; width: 450px; height: 100px; PADDING: 10 10 10 10; BORDER-RADIUS: 10px; box-sizing: content-box; box-shadow: 2px 2px 2px chocolate; color:white; } .inp_st { position: relative; border-radius: 5px; padding: 2 2 2; box-shadow: 2px 2px 2px chocolate; } .inp_bt { position: relative; top: 25px; border-radius: 5px; box-shadow: 2px 2px 2px aqua; } .header { border: cadetblue; border-style: dotted; border-left: none; border-right: none; border-top: none; } .head { position: relative; top: 10px; font-size: -webkit-xxx-large; font-family: cursive; color: cadetblue; } </style> </head> <body> <div id="container"> <div class="header"> <b class="head">Twitter Like Tweets</b></div> <form action="<?php $_PHP_SELF ?>" method ="post" id="sidebar_box"> <center><input style="top:14px;" type="text" name="name" autocomplete="off" placeholder="Username" required class="inp_st"/><br/> <input type="password" style="top:18px;" autocomplete="off" name="pass" placeholder="Password" required class="inp_st"/><br/> <input type="submit" value="Sign In" class="inp_bt" name="in"/> <input type="submit" value="Sign Up" class="inp_bt" name="up"/> </center> </form> <div id="main_box"> <p>Login or Signup to connect and post your status update.<br/>This is a demo of just the tweeting feature of twitter.</p> </div> </div> </body> </html>
Lets Create the profile.php file
<?php $dbhost = 'localhost';// Database Host. $dbuser = 'sid';// Database User. $dbpass = 'Sidharth12';// User's Password. $dbname = 'sample'; date_default_timezone_set("Asia/Kolkata"); session_start(); ?> <html> <head> <title>Profile Page</title> <style> #container { position:relative; width:700px; left:240px; color:white; background-color: lightgoldenrodyellow; padding-left: 30px; padding-right: 30px; padding-bottom: 10px; border-bottom-left-radius: 30%; border-top-right-radius: 20%; } #sidebar_box { text-align: center; position:relative; top:30px; background-color: cadetblue; width: 200px; height: 100px; PADDING: 10 10 10 10; BORDER-RADIUS: 10px; box-sizing: content-box; box-shadow: 2px 2px 2px chocolate; } #main_box { position:relative; top: -90px; left: 230; background-color: cadetblue; width: 450px; height: 100px; PADDING: 10 10 10 10; BORDER-RADIUS: 10px; box-sizing: content-box; box-shadow: 2px 2px 2px chocolate; color:white; } #feeds { position:relative; top: -80px; background-color: cadetblue; width: 680px; PADDING: 10 10 10 10; BORDER-RADIUS: 10px; box-sizing: content-box; box-shadow: 2px 2px 2px chocolate; color:white; } .inp_st { position: relative; border-radius: 5px; padding: 2 2 2; box-shadow: 2px 2px 2px chocolate; } .inp_bt { position: relative; top: 25px; border-radius: 5px; box-shadow: 2px 2px 2px aqua; } .header { border: cadetblue; border-style: dotted; border-left: none; border-right: none; border-top: none; } .head { position: relative; top: 10px; font-size: -webkit-xxx-large; font-family: cursive; color: cadetblue; } .usern { border-bottom: sandybrown; border-style: solid; border-width: 1px; border-top: none; border-left: none; border-right: none; text-align: -webkit-right; padding-right: 10px; font-weight: 800; font-family: monospace; } .menu { position: relative; top: 5px; left: 580px; } .menu a{ text-decoration:none; font-family:cursive; color:white; } </style> </head> <?php $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } if(isset($_POST['post'])) { $sql = "INSERT INTO USER_DATA ". "(username,tweet,time) ". "VALUES('".$_SESSION['user']."','".$_POST['tweet']."','".date('H:i', time())."')"; mysql_select_db($dbname); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } //echo '<script>window.alert(\'Successfully Posted !\');</script>'; ?><meta http-equiv="refresh" content="0;URL=profile.php" /> <?php } else if(isset($_POST['log'])) { echo '<script>window.confirm(\'Are Your Sure ?\');</script>'; ?><meta http-equiv="refresh" content="0;URL=index.php" /> <?php } ?> <body> <div id="container"> <div class="header"> <b class="head">Twitter Like Tweets</b></div> <?php echo '<div id="sidebar_box">'.' <b>Hello '.$_SESSION['user'].', Welcome! ;)</b><br/> '. '<b style="font-family: cursive;"> Last Login: '.date('H:i', $_SESSION['time']).' IST</b><br/><br/>'. '<form action="?" method ="post"><input type="submit" value="Logout" name="log"/></form>'. '</div>'; // Username ?> <div id="main_box"> <script language="javascript" type="text/javascript"> function limitText(limitField, limitNum) { if (limitField.value.length > limitNum) { limitField.value = limitField.value.substring(0, limitNum); } } </script> <form action="<?php $_PHP_SELF ?>" method ="post"> <textarea onKeyDown="limitText(this,180);" onKeyUp="limitText(this,180);" placeholder="Enter Your Tweet max 180 chars" name="tweet" style=" width: 450px;height: 60px;border-bottom-right-radius: 30%;box-shadow: 2px 2px chocolate;"></textarea> <input type="submit" value="Post" name="post" style=" position: relative; top: 20px; left: 406px;"/> </form></div> <div id="feeds"> <?php $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $rec_limit = 6; mysql_select_db($dbname); /* Get total number of records */ $sql = "SELECT count(username) FROM USER_DATA "; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } $row = mysql_fetch_array($retval, MYSQL_NUM ); $rec_count = $row[0]; if( isset($_GET{'page'} ) ) { $page = $_GET{'page'} + 1; $offset = $rec_limit * $page ; } else { $page = 0; $offset = 0; } $left_rec = $rec_count - ($page * $rec_limit); $sql = "SELECT username, tweet, time ". "FROM USER_DATA ORDER BY id DESC ". "LIMIT $offset, $rec_limit"; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "<div class='usern'>@{$row['username']}, {$row['time']} Hours</div> ". "<b>{$row['tweet']}</b><br/> "; } if( $page <ceil($rec_count/6) ) { $last = $page - 2; echo "<div class='menu'>"; echo "<a href=\"?page=$last\">Newer</a> |"; echo "<a href=\"?page=$page\">Older</a> "; echo "</div>"; //echo "pages found: ".ceil($rec_count/3)." and page=".$page; } else if( $page == 0 ) { echo "<div class='menu'>"; echo "<a href=\"?page=$page\">Older</a>"; echo "</div>"; } else { echo "<div class='menu' style='left:330px'>"; echo "<a>You Have Reached The END, So Most More ! ;)</a>"; } ?> </div> <div style="position:relative; width:100%;top: 210;background-color: aliceblue;"></div> </body> </html>
Alternatively you can download the whole code bundled in a single zip by clicking here.
Feel free to report bugs, we will be glad to fix them up ! :)
Comments
Post a Comment