Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. <?php
  2. ####### db config ##########
  3. /*$db_username = 'dont need already connected';
  4. $db_password = '';
  5. $db_name = '';
  6. $db_host = '';*/
  7. ####### db config end ##########
  8.  
  9. if($_POST)
  10. {
  11.  
  12. ### connect to mySql
  13. //$sql_con = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
  14.  
  15. //get type of vote from client
  16. $user_vote_type = trim($_POST["vote"]);
  17.  
  18. //get unique content ID and sanitize it (cos we never know).
  19. $unique_content_id = filter_var(trim($_POST["unique_id"]),FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
  20.  
  21. //Convert content ID to MD5 hash (optional)
  22. $unique_content_id = hash('md5', $unique_content_id);
  23.  
  24. //check if its an ajax request, exit if not
  25. if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  26. die();
  27. }
  28.  
  29.  
  30. switch ($user_vote_type)
  31. {
  32.  
  33. ##### User liked the content #########
  34. case 'up':
  35.  
  36. //check if user has already voted, determined by unique content cookie
  37. if (isset($_COOKIE["voted_".$unique_content_id]))
  38. {
  39. header('HTTP/1.1 500 Already Voted'); //cookie found, user has already voted
  40. exit(); //exit script
  41. }
  42.  
  43. //get vote_up value from db using unique_content_id
  44. $result = mysqli_query($sql_con,"SELECT vote_up FROM crewlist WHERE unique_content_id='$unique_content_id' LIMIT 1");
  45. $get_total_rows = mysqli_fetch_assoc($result);
  46.  
  47. if($get_total_rows)
  48. {
  49. //found record, update vote_up the value
  50. mysqli_query($sql_con,"UPDATE crewlist SET vote_up=vote_up+1 WHERE unique_content_id='$unique_content_id'");
  51. }else{
  52. //no record found, insert new record in db
  53. mysqli_query($sql_con,"INSERT INTO crewlist (unique_content_id, vote_up) value('$unique_content_id',1)");
  54. }
  55.  
  56. setcookie("voted_".$unique_content_id, 1, time()+7200); // set cookie that expires in 2 hour "time()+7200".
  57. echo ($get_total_rows["vote_up"]+1); //display total liked votes
  58. break;
  59.  
  60. ##### User disliked the content #########
  61. case 'down':
  62.  
  63. //check if user has already voted, determined by unique content cookie
  64. if (isset($_COOKIE["voted_".$unique_content_id]))
  65. {
  66. header('HTTP/1.1 500 Already Voted this Content!'); //cookie found, user has already voted
  67. exit(); //exit script
  68. }
  69.  
  70. //get vote_up value from db using unique_content_id
  71. $result = mysqli_query($sql_con,"SELECT vote_down FROM crewlist WHERE unique_content_id='$unique_content_id' LIMIT 1");
  72. $get_total_rows = mysqli_fetch_assoc($result);
  73.  
  74. if($get_total_rows)
  75. {
  76. //found record, update vote_down the value
  77. mysqli_query($sql_con,"UPDATE crewlist SET vote_down=vote_down+1 WHERE unique_content_id='$unique_content_id'");
  78. }else{
  79.  
  80. //no record found, insert new record in db
  81. mysqli_query($sql_con,"INSERT INTO crewlist (unique_content_id, vote_down) value('$unique_content_id',1)");
  82. }
  83.  
  84. setcookie("voted_".$unique_content_id, 1, time()+7200); // set cookie that expires in 2 hour "time()+7200".
  85. echo ($get_total_rows["vote_down"]+1);//display total disliked votes
  86. break;
  87.  
  88. ##### respond votes for each content #########
  89. case 'fetch':
  90. //get vote_up and vote_down value from db using unique_content_id
  91. $result = mysqli_query($sql_con,"SELECT vote_up,vote_down FROM crewlist WHERE unique_content_id='$unique_content_id' LIMIT 1");
  92. $row = mysqli_fetch_assoc($result);
  93.  
  94. //making sure value is not empty.
  95. $vote_up = ($row["vote_up"])?$row["vote_up"]:0;
  96. $vote_down = ($row["vote_down"])?$row["vote_down"]:0;
  97.  
  98. //build array for php json
  99. $send_response = array('vote_up'=>$vote_up, 'vote_down'=>$vote_down);
  100. echo json_encode($send_response); //display json encoded values
  101. break;
  102.  
  103. }
  104.  
  105. }
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement