How to make that by pressing the keys ctrl + u in php, the code that recorded the client who pressed this button in .htaccess ip worked?

  • Well, an event in JS, like keydown, keypress, and there is an AJAX request for a PHP file (or action, if it’s MV *), and in this file in PHP we get the IP as usual and write where we want. But, first of all, I’m not sure that it will be possible to track exactly these keys, Ctrl is the system one. Secondly, it costs the same easily, especially when writing a bot. Any specialist will help the suffering person to solve the problem. - SmInc
  • @SmInc well, and it is necessary to help a person, the site for this is made - Vadim
  • @Vadim Only now, judging by your answer, it didn’t work out for you. And people with higher qualifications have already enough tasks. - SmInc
  • @Vadim send. And more often. After the first short-term ban, keep sending ... As a result, for example, I will only be happy. - SmInc
  • @SmInc yes ban what's the problem, I give the detailed answers - Vadim

1 answer 1

Writing in htaccess ip addresses honestly is not very convenient, it’s better to put them in the database, and then check them here is an example of the code I hope it will be useful for you 1. First you need to create a table in the database

CREATE TABLE `ip_block` ( `id` INT(2) NOT NULL AUTO_INCREMENT, `ip` VARCHAR(250) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci', PRIMARY KEY (`id`) ) COLLATE='utf8_unicode_ci' ENGINE=InnoDB 

2. Make a connection to the database for this we will use the PDO wrapper sample file db.php

 <?php $host = 'localhost'; $db = 'ваша бд'; $user = 'имя пользователя'; $pass = 'ваш пароль'; $charset = 'utf8'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $pdo = new PDO($dsn, $user, $pass, $opt); 

  1. You need to create the IDEX file itself, which will check whether the given IP address is in the database of blocked users, if not, then display the page you need. And most importantly, if the user pressed Ctrl + U, then we send his ip address to the database in h / s. Next, we print an infinite loop in which alert is displayed an infinite number of times. This is due to the fact that the user could not open the view-source: page. When he opens the site again, he will see a block page. The index.php file itself

 <?php require_once 'db.php'; $ip = $_SERVER["REMOTE_ADDR"]; $stmt = $pdo->prepare("SELECT COUNT(ip) as ip FROM ip_block WHERE ip=:ip"); $stmt->bindParam(':ip', $ip); $stmt->execute(); $block = $stmt->fetch(); if($block["ip"] > 0){ header('Location: /test/ban.php'); exit; } ?> <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1>Пользователь ещё не нажал CTRL+U</h1> </body> </html> <script> var message="Правая кнопка мыши отключена!"; function click(e) { if (document.all) { // IE if (event.button == 2) { // Чтобы отключить левую кнопку поставьте цифру 1 alert(message); // чтобы отключить среднюю кнопку поставьте цифру 1 return false;} } if (document.layers) { // NC if (e.which == 3) { alert(message); return false;} } } if (document.layers) {document.captureEvents(Event.MOUSEDOWN);} document.onmousedown=click; document.oncontextmenu=function(e){return false}; document.mousedown=function(e){ if(event.which == 3) { alert(); } } let isCtrl = false; document.onkeyup=function(e){ if(e.which == 17) isCtrl=false; } document.onkeydown=function(e) { if(e.which == 17) isCtrl=true; if(e.which == 85 && isCtrl === true) { let xhr = new XMLHttpRequest(); xhr.open('GET', 'check.php', false); xhr.send(); if (xhr.status != 200) { alert( 'что-то не работает' ); } else { location.reload(); } let b = 1; for (var i = 0; i < b; i++) { alert(); b++; } } } </script> </body> </html> </script> 

  1. Add user ip to database check.php file

 require_once 'db.php'; $ip = $_SERVER["REMOTE_ADDR"]; $stmt = $pdo->prepare("SELECT COUNT(ip) as ip FROM ip_block WHERE ip=:ip"); $stmt->bindParam(':ip', $ip); $stmt->execute(); $block = $stmt->fetch(); if($block["ip"] > 0){ header('Location: /test/ban.php'); exit; } ?> <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1>Пользователь ещё не нажал CTRL+U</h1> </body> </html> <script> var message="Правая кнопка мыши отключена!"; function click(e) { if (document.all) { // IE if (event.button == 2) { // Чтобы отключить левую кнопку поставьте цифру 1 alert(message); // чтобы отключить среднюю кнопку поставьте цифру 1 return false;} } if (document.layers) { // NC if (e.which == 3) { alert(message); return false;} } } if (document.layers) {document.captureEvents(Event.MOUSEDOWN);} document.onmousedown=click; document.oncontextmenu=function(e){return false}; document.mousedown=function(e){ if(event.which == 3) { alert(); } } let isCtrl = false; document.onkeyup=function(e){ if(e.which == 17) isCtrl=false; } document.onkeydown=function(e) { if(e.which == 17) isCtrl=true; if(e.which == 85 && isCtrl === true) { let xhr = new XMLHttpRequest(); xhr.open('GET', 'check.php', false); xhr.send(); if (xhr.status != 200) { alert( 'что-то не работает' ); } else { location.reload(); } let b = 1; for (var i = 0; i < b; i++) { alert(); b++; } } } </script> </body> </html> </script> 

  1. File if user is banned. Ban.php file

 <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1>Умник ты забанен</h1> </body> </html> 

Download zip archive https://drive.google.com/open?id=1WIRJsJF6YPi6IvV_HtreHFaxSp9BHoYL

Watch the video https://youtu.be/5oKPTEXL2oU

  1. Links to useful articles and documentation

Introduction to browser events - https://learn.javascript.ru/introduction-browser-events

Virtual key codes for javascript (event.keyCode) - http://umi-cms.spb.su/ref/javascript/251/

PHP PDO data objects - http://php.net/manual/ru/book.pdo.php#book.pdo

How to work with PDO? A complete guide (very useful) - http://phpfaq.ru/pdo

Server and runtime information - http://php.net/manual/ru/reserved.variables.server.php

XMLHttpRequest Basics - https://learn.javascript.ru/ajax-xmlhttprequest

Cycle (programming) - https://ru.wikipedia.org/wiki/%D0%A6%D0%B8%D0%BA%D0%BB_(%D0%BF%D1%80%D0%BE%D0%B3 % D1% 80% D0% B0% D0% BC% D0% BC% D0% B8% D1% 80% D0% BE% D0% B2% D0% B0% D0% BD% D0% B8% D0% B5)

  • 2
    Are you serious? On the client to run PHP? About everything else, I am silent. - Deonis
  • Why not make a working snippet? @Deonis, in fact, is a server, it’s just written in a shuffle and not quite right, but basically it is a working code. - And
  • @Deonis Is this even possible? Or is it just an author's mistake? - Telion
  • @And are you kidding me ?? - Qwertiy
  • I guess that under the phrase " basically this is a working code " means @And)) Imagine that there is no fopen with the w flag that will "kill" htaccess and that would be the end of the story. Briefly ... Accessing a file ( page ) > PHP script is running. Ctrl + U > not in all browsers ( ! ) It causes a new request > PHP code is executed, as in the first case. It turns out that for any access to the file, the script will be executed on the server. ( continued below ) - Deonis