Last active
August 5, 2016 00:03
-
-
Save prinsss/f8626711835718c1ad68 to your computer and use it in GitHub Desktop.
Add page view counter for Ghost blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add to $(document).ready() | |
* Will auto load page view for each post on the page | |
*/ | |
$('[id=post-view]').each(function(){ | |
url = 'http://127.0.0.1/count.php?action=get&slug='+$(this).attr('data').slice(1,-1); | |
var span = $(this); | |
$.getJSON(url, function(data) { | |
span.html(data.count); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @Author: printempw | |
* @Date: 2016-01-31 17:08:29 | |
* @Last Modified by: prpr | |
* @Last Modified time: 2016-02-01 23:27:28 | |
* | |
* Configure your database info here | |
* Database name should be same with which you set for ghost | |
*/ | |
define('DB_NAME', 'ghost'); | |
define('DB_USER', 'root'); | |
define('DB_PASSWD', 'root'); | |
define('DB_HOST', 'localhost'); | |
/* Allowed time cap between each addition request in seconds */ | |
define('TIME_CAP', 5); | |
header('Access-Control-Allow-Origin: *'); | |
session_start(); | |
if (isset($_GET['action'])) { | |
// Simple SQL injection prevention | |
$action = stripslashes(trim($_GET['action'])); | |
$slug = isset($_GET['slug']) ? stripslashes(trim($_GET['slug'])) : ""; | |
if ($action == "get") { | |
$count = getCount($slug); | |
echo $count ? toJson($count) : toJson(0, "No such slug."); | |
} else if ($action == "add") { | |
// Simple prevention for evil post | |
if (!isset($_SESSION['last_post'])) { | |
$_SESSION['last_post'] = time(); | |
} else { | |
$post_time_cap = time() - $_SESSION['last_post']; | |
if ($post_time_cap < TIME_CAP) { | |
die(toJson(0, 'Wow you so faaaaaaaaast! Wait for some secs, man.')); | |
} | |
} | |
$count = addCount($slug); | |
echo $count ? toJson($count) : toJson(0, "No such slug."); | |
$_SESSION['last_post'] = time(); | |
} else if ($action == "order") { | |
// Check input | |
$limit = (isset($_GET['limit']) && is_numeric($_GET['limit'])) ? $_GET['limit'] : 10; | |
echo getPopularSlug($limit); | |
} | |
} else { | |
echo toJson(0, 'Illegal access.'); | |
} | |
function addCount($slug) { | |
$conn = connect(); | |
$result = $conn->query("SELECT * FROM posts WHERE slug='$slug'"); | |
if (checkSlugExist($slug)) { | |
$conn->query("UPDATE post_views SET pv=pv+1 WHERE slug='$slug'"); | |
return getCount($slug); | |
} else { | |
return false; | |
} | |
} | |
function getCount($slug) { | |
$conn = connect(); | |
if (checkSlugExist($slug)) { | |
$count = $conn->query("SELECT * FROM post_views WHERE slug='$slug'")->fetch_array()['pv']; | |
return $count; | |
} else { | |
return false; | |
} | |
} | |
function getPopularSlug($limit) { | |
$conn = connect(); | |
$order_sql = "SELECT * FROM `post_views` ORDER BY `pv` DESC LIMIT $limit"; | |
$title_sql = "SELECT * FROM `posts` WHERE `slug`="; | |
$result = $conn->query($order_sql); | |
$popular_posts = []; | |
while ($row = $result->fetch_array()) { | |
$slug = $row['slug']; | |
$title = $conn->query($title_sql."'$slug'")->fetch_array()['title']; | |
$popular_posts[$slug] = [$title, $row['pv']]; | |
} | |
echo json_encode($popular_posts); | |
} | |
function checkSlugExist($slug) { | |
$conn = connect(); | |
$result = $conn->query("SELECT * FROM post_views WHERE slug='$slug'"); | |
if ($result->num_rows != 0) { | |
return true; | |
} else { | |
$result = $conn->query("SELECT * FROM posts WHERE slug='$slug'"); | |
/** | |
* If requested slug doesnt exist in `post_views` but exist in `posts`, | |
* then insert a record. | |
*/ | |
if ($result->num_rows != 0) { | |
return insertRecord($slug, $conn); | |
} else { | |
// Non-existent slug | |
return false; | |
} | |
} | |
} | |
function insertRecord($slug, $conn) { | |
$sql = "INSERT INTO post_views(slug, pv) VALUES ('$slug', 1)"; | |
if ($conn->query($sql)) { | |
return true; | |
} else { | |
die($conn->error); | |
} | |
} | |
function connect() { | |
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME); | |
if ($conn->connect_error) { | |
die($conn->connect_error); | |
} | |
$conn->query("SET names UTF8"); | |
return $conn; | |
} | |
function toJson($count, $msg="") { | |
return $msg != "" ? | |
json_encode(array("count" => $count, "msg" => $msg)) : json_encode(array("count" => $count)); | |
} | |
/* -------CREATE-TABLE-SQL--------- | |
CREATE TABLE IF NOT EXISTS `post_views` ( | |
`id` int(10) NOT NULL AUTO_INCREMENT, | |
`slug` varchar(150) NOT NULL, | |
`pv` int(10) NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
--------------------------------- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
url = "http://127.0.0.1/count.php?action=order&limit=20"; | |
function getJson(url, callback) { | |
var request = new XMLHttpRequest(); | |
request.open("GET", url, true); | |
request.send(); | |
request.onreadystatechange = function(){ | |
if (request.readyState == 4 && request.status == 200) { | |
callback(JSON.parse(request.responseText)); | |
} | |
}; | |
} | |
/** | |
* Respensed Json will be like this | |
* { | |
* "build-shadowsocks-sharing-site-with-ss-panel": | |
* ["\u53ef\u80fd\u662f\u6700\u597d\u7684 ss-panel \u90e8\u7f72\u6559\u7a0b","3925"], | |
* "ubuntu-14-04-change-kernel-to-install-serverspeeder": | |
* ["Ubuntu 14.04 \u66f4\u6362\u5185\u6838\u4ee5\u5b89\u88c5\u9510\u901f","2058"] | |
* } | |
*/ | |
getJson(url, function(popular_posts){ | |
for (var slug in popular_posts) { | |
title = popular_posts[slug][0]; | |
pv = popular_posts[slug][1]; | |
var post = document.createElement('li'); | |
post.setAttribute('id', 'post'); | |
var link = document.createElement('a'); | |
link.setAttribute('href', ('/'+slug+'/')); | |
link.innerHTML = title; | |
var view = document.createElement('small'); | |
view.innerHTML = pv + " views"; | |
view.setAttribute('id', 'view'); | |
post.appendChild(link); | |
post.appendChild(view); | |
document.getElementById('posts').appendChild(post); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{! Add "post-view" id and set data attribute for which tag you wanna display views on }} | |
<span class="pms fa-eye"> | |
<span id="post-view" data="{{url}}">Loading</span> Hits | |
</span> | |
<!-- Example of popular posts: --> | |
<ul id="posts"></ul> | |
<style> #view { float: right; } </style> | |
<!-- Will be like this: --> | |
<ul id="posts"> | |
<li id="post"> | |
<a href="/build-shadowsocks-sharing-site-with-ss-panel/">可能是最好的 ss-panel 部署教程</a> | |
<small id="view">3931 views</small> | |
</li> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, ghost use
node
, so how thephp
file works?