Sei sulla pagina 1di 12

TwitterlikeSysteminPHP

Connect.php
<?php
//EditthefollowingfieldsinordertoestablishaconnectionwithMySQL
//********************************************************************************
$dbhost ="localhost";//Leavethisas'localhost'onceuploadedonaserver
$dbuser ="USERNAME";//Usernamethatisallowedtoaccessthedatabase
$dbpass ="PASSWORD";//Password
$dbname="NAMEOFTHEDATABASE";//Nameofthedatabase
//********************************************************************************

$conn=mysql_connect($dbhost,$dbuser,$dbpass)ordie("Errorconnectingtodatabase");
mysql_select_db($dbname);
?>

Dashboard.php
<?php
functiongetTime($t_time){

$pt=time()$t_time;

if($pt>=86400)

$p=date("Fj,Y",$t_time);

elseif($pt>=3600)

$p=(floor($pt/3600))."h";

elseif($pt>=60)

$p=(floor($pt/60))."m";

else

$p=$pt."s";

return$p;
}

if($user_id){

include"connect.php";

$query=mysql_query("SELECTusername,followers,following,tweets
FROMusers
WHEREid='$user_id'
");

mysql_close($conn);

$row=mysql_fetch_assoc($query);

$username=$row['username'];

$tweets=$row['tweets'];

$followers=$row['followers'];

$following=$row['following'];

echo"

<h6><ahref='logout.php'style='float:right;'>Logout</a></h6>

<table>

<tr>

<td>

<imgsrc='./default.jpg'style='width:35px;'alt='displaypicture'/>

</td>

<tdvalign='top'style='paddingleft:8px;'>

<h6><ahref='./$username'>@$username</a></h6>

<h6font=2style='margintop:10px;'>Tweets:<ahref='#'>$tweets</a>|
Followers:<ahref='#'>$followers</a>|Following:<ahref='#'>$following</a></h6>

</td>

</tr>

</table>

<formaction='tweet.php'method='POST'>

<textareaclass='formcontrol'placeholder='Typeyourtweethere'name='tweet'></textarea>

<buttontype='submit'style='float:right;margintop:3px;'class='btnbtninfobtn
xs'>Tweet</button>

</form>

<br>

<br>

";

include"connect.php";

$tweets=mysql_query("SELECTusername,tweet,timestamp

FROMtweets

WHEREuser_id=$user_idOR(user_idIN(SELECT
user2_idFROMfollowingWHEREuser1_id='$user_id'))

ORDERBYtimestampDESC

LIMIT0,10

");

while($tweet=mysql_fetch_array($tweets)){

echo"<divclass='wellwellsm'style='paddingtop:4px;paddingbottom:8px;marginbottom:8px;
overflow:hidden;'>";

echo"<divstyle='fontsize:10px;float:right;'>".getTime($tweet['timestamp'])."</div>";

echo"<table>";

echo"<tr>";

echo"<tdvalign=topstyle='paddingtop:4px;'>";

echo"<imgsrc='./default.jpg'style='width:35px;'alt='displaypicture'/>";

echo"</td>";;

echo"<tdstyle='paddingleft:5px;wordwrap:breakword;'valign=top>";

echo"<astyle='fontsize:12px;'href='./".$tweet['username']."'>@".$tweet['username']."</a>";

$new_tweet=preg_replace('/@(\\w+)/','<ahref=./$1>$0</a>',$tweet['tweet']);

$new_tweet=preg_replace('/#(\\w+)/','<ahref=./hashtag/$1>$0</a>',$new_tweet);

echo"<divstyle='fontsize:10px;margintop:3px;'>".$new_tweet."</div>";

echo"</td>";

echo"</tr>";

echo"</table>";

echo"</div>";

mysql_close($conn);

}
?>
<divclass="jumbotron"style="padding:3px;">

<divclass="container">

<h5>Madeby<ahref="http://simarsingh.ca">Simar</a></h5>

<h5>ThisisOpenSourceForkiton<iclass="fafagithub"></i><a
href="https://github.com/iSimar/TwitterLikeSystemPHP">GitHub</a></h5>

</div>
</div>

Index.php
<?php
session_start();
$user_id=$_SESSION['user_id'];
?>
<?php
if($_POST['loginbtn']=="loginsubmit"){
if($_POST['username']!=""&&$_POST['password']!=""){
$username=strtolower($_POST['username']);
include"connect.php";
$query=mysql_query("SELECTid,password
FROMusers
WHEREusername='$username'
");
mysql_close($conn);
if(mysql_num_rows($query)>=1){
$password=md5($_POST['password']);
$row=mysql_fetch_assoc($query);
if($password==$row['password']){
$_SESSION['user_id']=$row['id'];
header('Location:.');
exit;
}
else{
$error_msg="Incorrectusernameorpassword";
}
}
else{
$error_msg="Incorrectusernameorpassword";
}
}
else{
$error_msg="Allfieldsmustbefilledout";
}
}
?>
<!DOCTYPEhtml>
<html>
<head>
<metaname="viewport"content="width=425px,userscalable=no">

<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstraptheme.min.css">
<linkhref="//netdna.bootstrapcdn.com/fontawesome/4.0.3/css/fontawesome.css"rel="stylesheet">
<scriptsrc="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

<title>TwitterlikesystemPHP</title>
</head>
<bodystyle="marginleft:20px;width:300px;zoom:125%;">
<h3>TwitterLikeSystemPHP</h3>
<?php
if($user_id){
include"dashboard.php";
exit;
}
?>
<formrole="form"action="index.php"method="POST"style="width:300px;">

<divclass="inputgroup"style="marginbottom:10px;">
<spanclass="inputgroupaddon">@</span>
<inputtype="text"class="formcontrol"placeholder="Username"name="username">
</div>
<inputtype="password"style="marginbottom:10px;"class="formcontrol"placeholder="Password"name="password">
<?php
if($error_msg){
echo"<divclass='alertalertdanger'>".$error_msg."</div>";
}
?>
<divclass="btngroup">
<ahref="register.php"style="width:150px;"class="btnbtnsuccess">Register</a>
<buttontype="submit"style="width:150px;"class="btnbtninfo"name="loginbtn"value="loginsubmit">LogIn</button>
</div>
</form>
<br>
<divclass="jumbotron"style="padding:3px;">
<divclass="container">
<h5>Madeby<ahref="http://simarsingh.ca">Simar</a></h5>
<h5>ThisisOpenSourceForkiton<iclass="fafagithub"></i><ahref="https://github.com/iSimar/TwitterLikeSystem
PHP">GitHub</a></h5>
</div>
</div>
</body>
</html>

Profile.php
<?php
session_start();
$user_id=$_SESSION['user_id'];
?>
<!DOCTYPEhtml>
<html>
<head>

<metaname="viewport"content="width=425px,userscalable=no">

<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstraptheme.min.css">

<linkhref="//netdna.bootstrapcdn.com/fontawesome/4.0.3/css/fontawesome.css"rel="stylesheet">

<scriptsrc="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

<title>TwitterlikesystemPHP</title>
</head>
<bodystyle="marginleft:20px;width:300px;zoom:125%;">

<h3>TwitterLikeSystemPHP</h3>

<ahref='.'>GoHome</a>

<?php

functiongetTime($t_time){

$pt=time()$t_time;

if($pt>=86400)

$p=date("Fj,Y",$t_time);

elseif($pt>=3600)

$p=(floor($pt/3600))."h";

elseif($pt>=60)

$p=(floor($pt/60))."m";

else

$p=$pt."s";

return$p;

if($_GET['username']){

include'connect.php';

$username=strtolower($_GET['username']);

$query=mysql_query("SELECTid,username,followers,following,tweets

FROMusers

WHEREusername='$username'

");

mysql_close($conn);

if(mysql_num_rows($query)>=1){

$row=mysql_fetch_assoc($query);

$id=$row['id'];

$username=$row['username'];

$tweets=$row['tweets'];

$followers=$row['followers'];

$following=$row['following'];

if($user_id){

if($user_id!=$id){

include'connect.php';

$query2=mysql_query("SELECTid

FROMfollowing

WHEREuser1_id='$user_id'
ANDuser2_id='$id'

");

mysql_close($conn);

if(mysql_num_rows($query2)>=1){

echo"<ahref='unfollow.php?userid=$id&username=$username'
class='btnbtndefaultbtnxs'style='float:right;'>Unfollow</a>";

else{

echo"<ahref='follow.php?userid=$id&username=$username'
class='btnbtninfobtnxs'style='float:right;'>Follow</a>";

else{

echo"<ahref='./register.php'class='btnbtninfobtnxs'style='float:right;'>Signup</a>";

echo"

<tablestyle='marginbottom:5px;'>

<tr>

<td>

<imgsrc='./default.jpg'style='width:35px;'alt='displaypicture'/>

</td>

<tdvalign='top'style='paddingleft:8px;'>

<h6><ahref='./$username'>@$username</a>";

include'connect.php';

$query3=mysql_query("SELECTid

FROMfollowing

WHEREuser1_id='$id'ANDuser2_id='$user_id'

");

mysql_close($conn);

if(mysql_num_rows($query3)>=1){

echo"<i>FollowsYou</i>";

echo

"</h6>

<h6style='width:300px;margintop:10px;'>Tweets:<a
href='#'>$tweets</a>|Followers:<ahref='#'>$followers</a>|Following:<ahref='#'>$following</a></h6>

</td>

</tr>

</table>

";

include"connect.php";

$tweets=mysql_query("SELECTusername,tweet,timestamp

FROMtweets

WHEREuser_id=$id

ORDERBYtimestampDESC

LIMIT0,10

");

while($tweet=mysql_fetch_array($tweets)){

echo"<divclass='wellwellsm'style='paddingtop:4px;paddingbottom:8px;margin
bottom:8px;overflow:hidden;'>";

echo"<divstyle='fontsize:10px;float:right;'>".getTime($tweet['timestamp'])."</div>";

echo"<table>";

echo"<tr>";

echo"<tdvalign=topstyle='paddingtop:4px;'>";

echo"<imgsrc='./default.jpg'style='width:35px;'alt='displaypicture'/>";

echo"</td>";;

echo"<tdstyle='paddingleft:5px;wordwrap:breakword;'valign=top>";

echo"<astyle='fontsize:12px;'
href='./".$tweet['username']."'>@".$tweet['username']."</a>";

$new_tweet=preg_replace('/@(\\w+)/','<ahref=./$1>$0</a>',$tweet['tweet']);

$new_tweet=preg_replace('/#(\\w+)/','<ahref=./hashtag/$1>$0</a>',$new_tweet);

echo"<divstyle='fontsize:10px;margintop:3px;'>".$new_tweet."</div>";

echo"</td>";

echo"</tr>";

echo"</table>";

echo"</div>";

mysql_close($conn);

else{

echo"<divclass='alertalertdanger'>Sorry,thisprofiledoesn'texist.</div>";

echo"<ahref='.'style='width:300px;'class='btnbtninfo'>GoHome</a>";

?>

<br>

<divclass="jumbotron"style="padding:3px;">

<divclass="container">

<h5>Madeby<ahref="http://simarsingh.ca">Simar</a></h5>

<h5>ThisisOpenSourceForkiton<iclass="fafagithub"></i><a
href="https://github.com/iSimar/TwitterLikeSystemPHP">GitHub</a></h5>

</div>

</div>
</body>
</html>

Register.php
<?php
//error_reporting(E_All^E_Notice);
session_start();
$user_id=$_SESSION['user_id'];
?>
<!DOCTYPEhtml>
<html>
<head>
<metaname="viewport"content="width=425px,userscalable=no">

<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstraptheme.min.css">
<linkhref="//netdna.bootstrapcdn.com/fontawesome/4.0.3/css/fontawesome.css"rel="stylesheet">
<scriptsrc="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

<title>TwitterlikesystemPHP</title>
</head>
<bodystyle="marginleft:20px;width:300px;zoom:125%;">
<formaction="register.php"method="POST"role="form"style="width:300px;">
<h3>TwitterLikeSystemPHP</h3>
<h4>RegisterForAnAccount</h4>
<?php
if($_POST['btn']=="submitregisterform"){
if($_POST['username']!=""&&$_POST['password']!=""&&$_POST['confirmpassword']!=""){
if($_POST['password']==$_POST['confirmpassword']){
include'connect.php';
$username=strtolower($_POST['username']);
$query=mysql_query("SELECTusername
FROMusers
WHEREusername='$username'
");
mysql_close($conn);
if(!(mysql_num_rows($query)>=1)){
$password=md5($_POST['password']);
include'connect.php';
mysql_query("INSERTINTOusers(username,password)
VALUES('$username','$password')
");
mysql_close($conn);
echo"<divclass='alertalertsuccess'>Youraccounthasbeencreated!</div>";
echo"<ahref='.'style='width:300px;'class='btnbtninfo'>GoHome</a>";
echo"</form>";
echo"<br>";
echo"<divclass='jumbotron'style='padding:3px;'>
<divclass='container'>
<h5>Madeby<ahref='http://simarsingh.ca'>Simar</a></h5>
<h5>ThisisOpenSourceForkiton<iclass='fafagithub'></i><ahref='https://github.com/iSimar/TwitterLike
SystemPHP'>GitHub</a></h5>
</div>
</div>";
echo"</body>";
echo"</html>";
exit;

}
else{

$error_msg="Usernamealreadyexistspleasetryagain";
}
}
else{
$error_msg="Passwordsdidnotmatch";
}
}
else{
$error_msg="Allfieldsmustbefilledout";
}
}
?>
<divclass="inputgroup"style="marginbottom:10px;">
<spanclass="inputgroupaddon">@</span>
<inputtype="text"class="formcontrol"placeholder="Username"name="username"value="<?phpecho
$_POST['username'];?>">
</div>
<inputtype="password"style="marginbottom:10px;"class="formcontrol"placeholder="Password"name="password">
<inputtype="password"style="marginbottom:10px;"class="formcontrol"placeholder="ConfirmPassword"
name="confirmpassword">
<?php
if($error_msg){
echo"<divclass='alertalertdanger'>".$error_msg."</div>";
}
?>
<buttontype="submit"style="width:300px;marginbottom:5px;"class="btnbtnsuccess"name="btn"value="submit
registerform">Register</button>
<ahref="."style="width:300px;"class="btnbtninfo">GoHome</a>
</form>
<br>
<divclass="jumbotron"style="padding:3px;">
<divclass="container">
<h5>Madeby<ahref="http://simarsingh.ca">Simar</a></h5>
<h5>ThisisOpenSourceForkiton<iclass="fafagithub"></i><ahref="https://github.com/iSimar/TwitterLikeSystem
PHP">GitHub</a></h5>
</div>
</div>
</body>
</html>

Tweet.php
<?php
session_start();
$user_id=$_SESSION['user_id'];
?>
<?php
if($user_id){

if($_POST['tweet']!=""){

$tweet=htmlentities($_POST['tweet']);

$timestamp=time();

include'connect.php';

$query=mysql_query("SELECTusername

FROMusers

WHEREid='$user_id'

}
?>

");
$row=mysql_fetch_assoc($query);
$username=$row['username'];
mysql_query("INSERTINTOtweets(username,user_id,tweet,timestamp)

VALUES('$username','$user_id','$tweet',$timestamp)

");
mysql_query("UPDATEusers

SETtweets=tweets+1

WHEREid='$user_id'

");
mysql_close($conn);
header("Location:.");

Follow.php
<?php
session_start();
$user_id=$_SESSION['user_id'];
?>
<?php
if($_GET['userid']&&$_GET['username']){

if($_GET['userid']!=$user_id){

$follow_userid=$_GET['userid'];

$follow_username=$_GET['username'];

include'connect.php';

$query=mysql_query("SELECTid

FROMfollowing

WHEREuser1_id='$user_id'AND
user2_id='$follow_userid'

");

mysql_close($conn);

if(!(mysql_num_rows($query)>=1)){

include'connect.php';

mysql_query("INSERTINTOfollowing(user1_id,user2_id)

VALUES('$user_id','$follow_userid')

");

mysql_query("UPDATEusers

SETfollowing=following+1

WHEREid='$user_id'

");

mysql_query("UPDATEusers

SETfollowers=followers+1

WHEREid='$follow_userid'

");

mysql_close($conn);

header("Location:./".$follow_username);

}
}
?>

Unfollow.php
<?php
session_start();
$user_id=$_SESSION['user_id'];
?>
<?php
if($_GET['userid']&&$_GET['username']){

if($_GET['userid']!=$user_id){

$unfollow_userid=$_GET['userid'];

$unfollow_username=$_GET['username'];

include'connect.php';

$query=mysql_query("SELECTid

FROMfollowing

WHEREuser1_id='$user_id'AND
user2_id='$unfollow_userid'

");

mysql_close($conn);

if(mysql_num_rows($query)>=1){

include'connect.php';

mysql_query("DELETEFROMfollowing

WHEREuser1_id='$user_id'ANDuser2_id='$unfollow_userid'

");

mysql_query("UPDATEusers

SETfollowing=following1

WHEREid='$user_id'

");

mysql_query("UPDATEusers

SETfollowers=followers1

WHEREid='$unfollow_userid'

");

mysql_close($conn);

header("Location:./".$unfollow_username);

}
}
?>

Hashtag.php
<?php
session_start();
$user_id=$_SESSION['user_id'];
?>
<!DOCTYPEhtml>
<html>
<head>

<metaname="viewport"content="width=425px,userscalable=no">

<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstraptheme.min.css">

<linkhref="//netdna.bootstrapcdn.com/fontawesome/4.0.3/css/fontawesome.css"rel="stylesheet">

<scriptsrc="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

<title>TwitterlikesystemPHP</title>

10

</head>
<bodystyle="marginleft:20px;width:300px;zoom:125%;">

<h3>TwitterLikeSystemPHP</h3>

<ahref='..'>GoHome</a>
<?php
functiongetTime($t_time){

$pt=time()$t_time;

if($pt>=86400)

$p=date("Fj,Y",$t_time);

elseif($pt>=3600)

$p=(floor($pt/3600))."h";

elseif($pt>=60)

$p=(floor($pt/60))."m";

else

$p=$pt."s";

return$p;
}
if(!$user_id){

echo"<ahref='../register.php'class='btnbtninfobtnxs'style='float:right;'>Signup</a>";
}
if($_GET['hashtag']!=""){

$hashtag=$_GET['hashtag'];

echo"<divstyle='fontsize:20px;'>Tweetswith<a>#".$hashtag."</a></div>";

include"connect.php";

$tweets=mysql_query("SELECTusername,tweet,timestamp

FROMtweets

WHEREtweetREGEXP'^#$hashtag'ORtweetREGEXP'#$hashtag'

ORDERBYtimestampDESC

LIMIT0,10

");

if(mysql_num_rows($tweets)>0){

while($tweet=mysql_fetch_array($tweets)){

echo"<divclass='wellwellsm'style='paddingtop:4px;paddingbottom:8px;marginbottom:8px;
overflow:hidden;'>";

echo"<divstyle='fontsize:10px;float:right;'>".getTime($tweet['timestamp'])."</div>";

echo"<table>";

echo"<tr>";

echo"<tdvalign=topstyle='paddingtop:4px;'>";

echo"<imgsrc='../default.jpg'style='width:35px;'alt='displaypicture'/>";

echo"</td>";;

echo"<tdstyle='paddingleft:5px;wordwrap:breakword;'valign=top>";

echo"<astyle='fontsize:12px;'href='../".$tweet['username']."'>@".$tweet['username']."</a>";

$new_tweet=preg_replace('/@(\\w+)/','<ahref=../$1>$0</a>',$tweet['tweet']);

$new_tweet=preg_replace('/#(\\w+)/','<ahref=./$1>$0</a>',$new_tweet);

echo"<divstyle='fontsize:10px;margintop:3px;'>".$new_tweet."</div>";

echo"</td>";

echo"</tr>";

echo"</table>";

echo"</div>";

else{

echo"<h5><i>Notweetsfound.</i><br>Bethefirstonetouse<ahref='..'>#$hashtag</a></h5>";

mysql_close($conn);
}
else{

echo"<divclass='alertalertdanger'>Sorry,invalidhashtag.</div>";

11


echo"<ahref='.'style='width:300px;'class='btnbtninfo'>GoHome</a>";
}
?>
<br>
<divclass="jumbotron"style="padding:3px;">

<divclass="container">

<h5>Madeby<ahref="http://simarsingh.ca">Simar</a></h5>

<h5>ThisisOpenSourceForkiton<iclass="fafagithub"></i><a
href="https://github.com/iSimar/TwitterLikeSystemPHP">GitHub</a></h5>

</div>
</div>
</body>
</html>

Logout.php
<?php
session_start();
$user_id=$_SESSION['user_id'];
session_destroy();
header('Location:.');
?>

12

Potrebbero piacerti anche