Where to put PHP code that takes user machine information?

Asked

Viewed 1,286 times

0

I created some functions responsible for taking the user’s IP (and ISP, parents, browser, OS) in order to keep a control of who accesses, how many times in a given minute I do not know where to insert in the pages of content of the site. Another question is the best way to get these kinds of information?

Code that picks up the information

<?php
    class ClientData{

        public $ip;
        public $isp;
        public $browser;
        public $so;
        public $city;
        public $country;
        public $referrer;

        /**
        *StartCatchData() 
        *Este método tem o objetivo de realizar todas as chamadas a métodos, obtendo 
        *assim todos os dados dos cliente.
        */
        public function StartCatchData(){
            $this->CatchIP();
        }

        public function CatchIP(){
            if(array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)){
                $this->ip = $_SERVER["HTTP_X_FORWARDED_FOR"];  
            }else if(array_key_exists('REMOTE_ADDR', $_SERVER)) { 
                $this->ip = $_SERVER["REMOTE_ADDR"]; 
            }else if(array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
                $this->ip = $_SERVER["HTTP_CLIENT_IP"]; 
            }else{
                $this->ip = false;
            }
        }   
    }
?>

Content page

<html>
<head>
    <title>
        Teste
    </title>
</head>
<body>
<?php
    require_once 'ClientData.php';

    $clientData = new ClientData();

    $clientData->StartCatchData();
?>
Hello World!
</body> de exemplo:
  • "however do not know where to insert into the pages of content of the site"? Insert what? The information handles or the code? You want to display the data to the user?

  • insert the code that triggers the methods that capture user information, in my example I put just below the tag <body>

1 answer

5


Its doubt is not very clear, but if I understood it has two possible "environments":

I recommend using include, require, etc before anything else, to keep the project organization

  1. Before rendering:

    Run before the page is completely delivered to the customer, as your system needs to have customer data even able to see anything from the page.

    <?php
        require_once 'ClientData.php';
    
        $clientData = new ClientData();
        $clientData->StartCatchData();
    ?><html>
    <head>
    ...
    
  2. After uploading the whole content

    This process should be done if you need to know that the customer has already received the whole page, for example if you are creating a download counter, the counter should only "add up" when the download is completed:

    <?php
        require_once 'ClientData.php';
    ?><html>
    
    ...
    
    </html><?php
        $clientData = new ClientData();
        $clientData->StartCatchData();
    ?>
    
  • ,@Is this the best way to get this kind of information? and to check if I’m suffering I’m suffering DDOS is a good way?

  • This your code does not present any DDOS question, so my answer is based on user use and collect data only, if your question is about security you have to specify in the question. Really about DDOS, you can work with PHP (and the first example is the best way to prevent the attack), but you don’t necessarily need to use PHP against DDOS, I don’t know if it’s the best way, I believe on Linux servers for example, you can install applications to prevent this, or some module by apache, I will inform me right after I return

Browser other questions tagged

You are not signed in. Login or sign up in order to post.