Chống click tặc cho adsense bằng javascript
Thấy nhiều bạn dùng Blogspot đã đăng ký được quảng cáo Google Adsense (mặc dù mình chưa đăng ký được :v) nhưng do một lý do nào đó mà bị click tặc hoành hành làm cho tài khoản bị vô hiệu. Sau đây là cách giúp các bạn hạn chế click tặc.
Thấy nhiều bạn dùng Blogspot đã đăng ký được quảng cáo Google Adsense (mặc dù mình chưa đăng ký được :v) nhưng do một lý do nào đó mà bị click tặc hoành hành làm cho tài khoản bị vô hiệu. Sau đây là cách giúp các bạn hạn chế click tặc.Cơ chế hoạt động như sau : Thực ra khi có một client truy cập vào website của chúng ta thì bao giờ cũng có 1 số IP của client đó. Vậy với IP đó, nếu chúng ta kiểm tra nếu chưa có click vào quảng cáo Adsense hoặc số lần click cho phép (trong ví dụ thỉ cho phép click 3 lần trong 1 ngày) thì ta sẽ cho hiển thị quảng cáo. Còn click quá số lần cho phép thì ta không cho hiển thị quảng cáo.
Trong ví dụ, số lần cho phép click trong ngày của mình là 3.
P/S: Chúc cách bạn thành công, có điều gì không hiểu bạn vui lòng để lại nhận xét bên dưới !
Demo chống click tặc: (click vào hình để xem demo)
Hiển thị quảng cáo
<script> /* Đây là plugin cookie */ (function(factory) { if (typeof define === 'function' && define.amd) { // AMD (Register as an anonymous module) define(['jquery'], factory); } else if (typeof exports === 'object') { // Node/CommonJS module.exports = factory(require('jquery')); } else { // Browser globals factory(jQuery); } }(function($) { var pluses = /\+/g; function encode(s) { return config.raw ? s : encodeURIComponent(s); } function decode(s) { return config.raw ? s : decodeURIComponent(s); } function stringifyCookieValue(value) { return encode(config.json ? JSON.stringify(value) : String(value)); } function parseCookieValue(s) { if (s.indexOf('"') === 0) { // This is a quoted cookie as according to RFC2068, unescape... s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); } try { // Replace server-side written pluses with spaces. // If we can't decode the cookie, ignore it, it's unusable. // If we can't parse the cookie, ignore it, it's unusable. s = decodeURIComponent(s.replace(pluses, ' ')); return config.json ? JSON.parse(s) : s; } catch (e) {} } function read(s, converter) { var value = config.raw ? s : parseCookieValue(s); return $.isFunction(converter) ? converter(value) : value; } var config = $.cookie = function(key, value, options) { // Write if (arguments.length > 1 && !$.isFunction(value)) { options = $.extend({}, config.defaults, options); if (typeof options.expires === 'number') { var days = options.expires, t = options.expires = new Date(); t.setMilliseconds(t.getMilliseconds() + days * 864e+5); } return (document.cookie = [ encode(key), '=', stringifyCookieValue(value), options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE options.path ? '; path=' + options.path : '', options.domain ? '; domain=' + options.domain : '', options.secure ? '; secure' : '' ].join('')); } // Read var result = key ? undefined : {}, // To prevent the for loop in the first place assign an empty array // in case there are no cookies at all. Also prevents odd result when // calling $.cookie(). cookies = document.cookie ? document.cookie.split('; ') : [], i = 0, l = cookies.length; for (; i < l; i++) { var parts = cookies[i].split('='), name = decode(parts.shift()), cookie = parts.join('='); if (key === name) { // If second argument (value) is a function it's a converter... result = read(cookie, value); break; } // Prevent storing a cookie that we couldn't decode. if (!key && (cookie = read(cookie)) !== undefined) { result[name] = cookie; } } return result; }; config.defaults = {}; $.removeCookie = function(key, options) { // Must not alter options, thus extending a fresh object... $.cookie(key, '', $.extend({}, options, { expires: -1 })); return !$.cookie(key); }; })); /* Kết thúc plugin cookie */ </script> //CODE HTML <div id="quang_cao">Hiển thị quảng cáo</div> <script> //Hàm load quảng cáo function LoadQuangCao() { $("#quang_cao").empty().append("<a target='_blank' href='http://adf.ly/1VQms2'><img src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguMXCoZ3X9EN-pQZP_T45iAAlUIFnz_tRg0Lv3Am2eDdci85MtN47IiH3aCIb62mq6CXgKnmKGsxlLisUJ38wnLFaqtDZc1XW2tiGl_YAO09V2GeacII1kEHzFpHyAAZu26NhIOG48WLnF/s360-Ic42/banner-thietkewebsite.jpg'/></a>") } //Số lần click tối đa cho 1 quảng cáo là 3 lần var number_click = 3; var ip_current = ""; var arr_anti_click = []; $.getJSON('//api.ipify.org?format=jsonp&callback=?', function(data) { //Lấy ip hiện tại ip_current = data.ip; if (typeof $.cookie('arr_anti_click') === 'undefined') { //tạo item cho cookie gổm có ip và click var anti_click = { "ip": data.ip, "click": 0 }; //ghi ip vào cookie với thời gian là 1 ngày arr_anti_click.push(anti_click); $.cookie('arr_anti_click', JSON.stringify(arr_anti_click), { expires: 1 }); //Hiển thị quảng cáo tại đây LoadQuangCao(); } else { arr_anti_click = $.parseJSON($.cookie('arr_anti_click')); //Kiễm tra trùng IP hoặc không và số lần click < 3 thì hiện quảng cáo if (ip_current != arr_anti_click[0].ip) { LoadQuangCao(); } else if (ip_current == arr_anti_click[0].ip && arr_anti_click[0].click < number_click) { LoadQuangCao(); } } $("#quang_cao").click(function() { arr_anti_click[0].click = arr_anti_click[0].click + 1; $.removeCookie('arr_anti_click'); $.cookie('arr_anti_click', JSON.stringify(arr_anti_click), { expires: 1 }); alert("Bạn đã click quảng cáo lần " + arr_anti_click[0].click + " trong ngày !"); if (arr_anti_click[0].click >= number_click) $("#quang_cao").remove(); }); }); </script>
Các service api có sẵn bằng javascript:
db-ip.com
http://api.db-ip.com/addrinfo?api_key=<your_api_key>&addr=<your_ip_address>
Kết quả trả về:
{ "address": "116.12.250.1", "country": "SG", "stateprov": "Central Singapore", "city": "Singapore" }
Giới hạn:
2,500 requests per hour
Doesn't support JSONP callbacks
Requires IP address parameter
geobytes.com
$.getJSON('//gd.geobytes.com/GetCityDetails?callback=?', function(data) { console.log(JSON.stringify(data, null, 2)); });
Kết quả trả về:
{ "geobytesforwarderfor": "", "geobytesremoteip": "116.12.250.1", "geobytesipaddress": "116.12.250.1", "geobytescertainty": "99", "geobytesinternet": "SA", "geobytescountry": "Saudi Arabia", "geobytesregionlocationcode": "SASH", "geobytesregion": "Ash Sharqiyah", "geobytescode": "SH", "geobyteslocationcode": "SASHJUBA", "geobytescity": "Jubail", "geobytescityid": "13793", "geobytesfqcn": "Jubail, SH, Saudi Arabia", "geobyteslatitude": "27.004999", "geobyteslongitude": "49.660999", "geobytescapital": "Riyadh ", "geobytestimezone": "+03:00", "geobytesnationalitysingular": "Saudi Arabian ", "geobytespopulation": "22757092", "geobytesnationalityplural": "Saudis", "geobytesmapreference": "Middle East ", "geobytescurrency": "Saudi Riyal", "geobytescurrencycode": "SAR", "geobytestitle": "Saudi Arabia" }
Giới hạn:
16,384 requests per hour
Returns the wrong location (I'm in Singapore, not Saudi Arabia)
geoplugin.com
$.getJSON('//www.geoplugin.net/json.gp?jsoncallback=?', function(data) { console.log(JSON.stringify(data, null, 2)); });
Kết quả trả về:
{ "geoplugin_request": "116.12.250.1", "geoplugin_status": 200, "geoplugin_credit": "Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\\'http://www.maxmind.com\\'>http://www.maxmind.com</a>.", "geoplugin_city": "Singapore", "geoplugin_region": "Singapore (general)", "geoplugin_areaCode": "0", "geoplugin_dmaCode": "0", "geoplugin_countryCode": "SG", "geoplugin_countryName": "Singapore", "geoplugin_continentCode": "AS", "geoplugin_latitude": "1.2931", "geoplugin_longitude": "103.855797", "geoplugin_regionCode": "00", "geoplugin_regionName": "Singapore (general)", "geoplugin_currencyCode": "SGD", "geoplugin_currencySymbol": "$", "geoplugin_currencySymbol_UTF8": "$", "geoplugin_currencyConverter": 1.4239 }
Giới hạn:
120 requests per minute
hackertarget.com
http://api.hackertarget.com/geoip/?q=<your_ip_address>
Kết quả trả về:
IP Address: 116.12.250.1
Country: SG
State: N/A
City: Singapore
Latitude: 1.293100
Longitude: 103.855797
Giới hạn:
50 requests per day
Only returns plain text
Requires IP address parameter
ip-api.com
$.getJSON('//ip-api.com/json?callback=?', function(data) { console.log(JSON.stringify(data, null, 2)); });
Kết quả trả về:
{ "as": "AS3758 SingNet", "city": "Singapore", "country": "Singapore", "countryCode": "SG", "isp": "SingNet Pte Ltd", "lat": 1.2931, "lon": 103.8558, "org": "Singapore Telecommunications", "query": "116.12.250.1", "region": "", "regionName": "", "status": "success", "timezone": "Asia/Singapore", "zip": "" }
Giới hạn:
150 requests per minute
ipify.org
$.getJSON('//api.ipify.org?format=jsonp&callback=?', function(data) { console.log(JSON.stringify(data, null, 2)); });
Kết quả trả về:
{ "ip": "116.12.250.1" }
Giới hạn:
None (unlimited bandwidth)
ipinfo.io
$.getJSON('//ipinfo.io/', function(data) { console.log(JSON.stringify(data, null, 2)); });
Kết quả trả về:
{ "ip": "116.12.250.1", "hostname": "No Hostname", "city": "Singapore", "region": "", "country": "SG", "loc": "1.2931,103.8558", "org": "AS3758 SingNet" }
Giới hạn:
1,000 requests per day
ipinfodb.com
$.getJSON('//api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&format=json&callback=?', function(data) { console.log(JSON.stringify(data, null, 2)); });
Kết quả trả về:
{ "statusCode": "OK", "statusMessage": "", "ipAddress": "116.12.250.1", "countryCode": "SG", "countryName": "Singapore", "regionName": "Singapore", "cityName": "Singapore", "zipCode": "048941", "latitude": "1.28967", "longitude": "103.85", "timeZone": "+08:00" }
Giới hạn:
Two requests per second
Requires registration to get your API key
ip-json
http://ip-json.rhcloud.com/json
Kết quả trả về:
{ "site": "http://ip-json.rhcloud.com", "city": "Singapore", "region_name": null, "region": "00", "area_code": 0, "time_zone": "Asia/Singapore", "longitude": 103.85579681396484, "metro_code": 0, "country_code3": "SGP", "latitude": 1.2930999994277954, "postal_code": null, "dma_code": 0, "country_code": "SG", "country_name": "Singapore", "q": "116.12.250.1" }
Giới hạn:
Quota not stated, but may be limited by OpenShift PaaS
Doesn't support JSONP callbacks
jsonip.com
$.getJSON('//jsonip.com/?callback=?', function(data) { console.log(JSON.stringify(data, null, 2)); });
Kết quả trả về:
{ "ip": "116.12.250.1", "about": "/about", "Pro!": "http://getjsonip.com" }
Giới hạn:
None (unlimited bandwidth)
jsontest.com
$.getJSON('//ip.jsontest.com/?callback=?', function(data) { console.log(JSON.stringify(data, null, 2)); });
Kết quả trả về:
{ "ip": "116.12.250.1" }
Giới hạn:
Goes down a lot (over quota), so I wouldn't use it for production
snoopi.io
http://api.snoopi.io/v1/
Kết quả trả về:
{ "remote_address": "116.12.250.1", "requested_address": "116.12.250.1", "requested_time": "11:25:35 pm", "requested_date": "January 31, 2016", "CountryCode": "SG", "Region": null, "Region_Full": "Singapore", "City": "Ubi", "PostalZip": "96940", "TimeZone_Name": "Asia\/Singapore", "Latitude": "1.31865", "Longitude": "103.894" }
Giới hạn:
2,500 requests per day
Doesn't support JSONP callbacks
Làm sao để chèn mã adsense vào vậy, mình chèn vào mà toàn bị lỗi không thể hiển thị được
ReplyDeleteCho mình hỏi với code này chỉ cần đặt vào Function là ok hả ad?
ReplyDeleteLàm như demo là chạy nhé bạn !
Deletechả có tác dụng, không có tác dụng với ip, sau khi xóa cookies vẫn hiện thị quảng cáo bình thường.
ReplyDelete