DEVELOPMENT

Today, i'll show you how to create a cookie based like & unlike system. In this type of system, a "decider" cookie is used which tells the server that the user has liked the page or not.

I know that this is definitely a wrong approach, because once the user has liked the page he would not be able to like it again. But this is easily possible, if the user deletes the cookies from web browsers option.

THE SITUATION

like unlike php



BUTTON.PHP
<!DOCTYPE html>
<html>
<body> 
<?php

if(!isset($_COOKIE["cook"]))
{
 echo "<button id=\"button\" type=\"button\" onclick=\"add(this.value)\" value=\"1\"> like </button>";
}
if(isset($_COOKIE["cook"]))
{
 
 echo "<button id=\"button1\" type=\"button\" value=\"2\"> unlike </button>"; 
 
} 
 ?>
</body>
</html>            


INDEX.PHP
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" type="text/css" />
<script> 
$(document).ready(function(){
 $("#btndisp").load("button.php");
 
$(document).on("click", "button", function(){
 var ID=$(this).val();
 $.ajax({

 type: "POST",

 url: "add_del.php",

 data:"id="+ID, 

 cache: false, 

 success: function(data){

 $("#btndisp").load("button.php");

 } });

return false;})

;});
</script>
</head>
<body>
<div id = "content">
<p>It is not secure to store the password of the registered user directly as it is in the sql database, for instance say mr.x has stolen the database file storing the password, then he has full control on our authentication system. This type of security breach is always an online attack. Some old school programmer will tell to use encrypting as a solution. But, in that case also problem will exist because encrypting is a reversible process, thus our mr.x can easily decrypt the value store in our database</p><br></div>
<div id="btndisp"></div>
</body>
</html>
    

COUNT_VOTE.TXT

Pop-up div

ADD_DEL.PHP
<?php

$var=$_POST["id"];

$flin = file("count_vote.txt");


switch($var)
{
 case "1" : setcookie("cook", "article_read", time() + (86400 * 30), "/"); 
 $text = $flin[0]+1;
 $handle = fopen("count_vote.txt","w");
 fwrite($handle,$text);
 fclose($handle); 
 break;
 
 case "2" : unset($_COOKIE["cook"]); 
 setcookie("cook", "", time()-3600,'/');
 $text = $flin[0]-1;
 $handle = fopen("count_vote.txt","w");
 fwrite($handle,$text);
 fclose($handle); 
 break;
 
 default : break;
 
} 
?>



STYLE.CSS
like
{
corsor:pointer;
}
button
{
background: blue;
color:white;
border: 0px;  
cursor: pointer;
height:50px;
width:100px;
font-size:18px;   
}
body
{   
font-family: Arial;
font-size: 18px;
text-align: center;
}
p
{
width:500px;
margin:0 auto;
border: 1px solid gray;
padding:10px;
}
    


Leave a Comment  
Download Code(75)





All rights reserved, the content is copyrighted to the author.

RELATED POST