comment.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. header('content-type:application/json');
  3. session_start();
  4. include('../config.php');
  5. include('../includes/function.php');
  6. $id = htmlspecialchars($_POST['id']);
  7. $nickname = htmlspecialchars($_POST['nickname']);
  8. $content = htmlspecialchars($_POST['content']);
  9. $timestamp = intval(htmlspecialchars($_POST['timestamp']));
  10. if ($_SESSION['vcode'] != md5($_POST['vCode'] . $VERIFICATION_KEY) && $IMAGE_VERIFICATION) {
  11. exit('{"code":-2,"msg":"抱歉,人机验证失败","result":""}');
  12. }
  13. if ($timestamp - time() > 60 || time() - $timestamp > 60) {
  14. exit('{"code":-5,"msg":"提交失败!请检查您的系统时间!"}');
  15. }
  16. if (empty($id) || empty($nickname) || empty($content)) {
  17. exit('{"code":-6,"msg":"表单提交失败!某些参数为空!"}');
  18. }
  19. try {
  20. $pdo = pdoConnect();
  21. $stmt = $pdo->prepare("select * from loveway_data WHERE id=?");
  22. $stmt->bindValue(1, $id);
  23. if ($stmt->execute()) {
  24. $rows = $stmt->fetchAll();
  25. $row = $rows[0];
  26. $inputContent = array("time" => time(), "nickname" => $nickname, "content" => $content);
  27. $commentArr = json_decode($row['comment']);
  28. $commentArr[count($commentArr)] = $inputContent;
  29. $stmt = $pdo->prepare("UPDATE loveway_data SET comment=? WHERE id=?");
  30. $stmt->bindValue(1, json_encode($commentArr));
  31. $stmt->bindValue(2, $id);
  32. if ($stmt->execute()) {
  33. exit('{"code":1,"commentNum":'.strval(count($commentArr)).',"commentSrc":'.json_encode($commentArr).',"msg":"评论提交成功!"}');
  34. } else {
  35. exit('{"code":-2,"msg":"抱歉,出现了一个未知错误!请与管理员联系!"}');
  36. }
  37. } else {
  38. exit('{"code":-2,"msg":"抱歉,出现了一个未知错误!请与管理员联系!"}');
  39. }
  40. } catch (PDOException $e) {
  41. exit('{"code":-1,"msg":"抱歉,出现了一个致命错误!请与管理员联系!"}');
  42. }