How to choose a user from among thousands of non-logged visitors?

0

Hello, I have a web page in which I have a code ajax $ post that what makes every time the user enters the web this produces a request for refreshments of links that I get from other pages of mine.

The problem is that I receive thousands of visits on the page that I have said ajax code so that oversaturation of requests to the other pages. So what I need is that from the thousands of unidentified and anonymous users, choose a user to make the request.

Thank you in advance for the help.

    
asked by t8kcontrol 28.06.2017 в 09:27
source

2 answers

0

Seeing the comments I tell you that I do not know a way to do it without using a database to post the users (Thing that does not make sense because for that you would make requests to the server that is what you do not want)

However, I present a possible solution to fix your problem:

Is to create a script that is based on mathematics, and control everything by probability, you can change the probability:

<script>
    //use window en lugar del método de jQuery para que sin importar dónde coloque este script este espere a que carguen todos los recuesos (incluendo jQuery) y funcione de manera correcta, de otro modo podrían haber fallos.
    window.onload = function (){
        let num = Math.round(Math.random()*100); //generar un numero entre 0 y 100 y guardarlo en una variable llamada num
        if(num==0){num = 1} //para evitar que sean 101 numeros y sólo sean 100
        if(num>=75 && num<=100){ //sólo hay 25 numeros que corresponden osea 25%
            $.post(tusParametros); //tu peticion post de ajax
        }
    }
</script>

For this script, a 25% chance is generated that the request is made but you can change it, that way not all users will generate the ajax request.

Limit it to anonymous or unlogged users

I suppose you should have an authentication system, I do not know it but being the most common php sessions I recommend the following, if you use another system you tell me and I'll see if I can find how to implement it.

<?php
    if(isset($_SESSION['usuario'])){ //algo muy simple pero es la idea
         require 'somepage.php';
    }else{
        require 'miScript.php';
    }
?>

In this case if you are a user you will include the code which always makes the request but you will not include the code of the request that is based on the probability.

For my example somepage.php would contain the request that is always made so that the users with session started have the benefit and miScript.php would have only the code that makes random requests.

Why do the files that contain the script have an .php extension?  -If you need to have HTML or PHP or JS code you can have it in that same file  -Follow my knowledge is the same do a require a .js file, this would literally get the text that is in the

    
answered by 28.06.2017 в 11:09
0

Why do not you try to do it for time and not because a person does it? I mean, by Javascript, call AJAX every so often, for example, 5 minutes.

$(function() {
  setInterval(function() {
    $.ajax({
      ...
    });
  }, 300000);
});

This way you avoid flooding your request server and stop thinking about complex algorithms to know which user is going to refresh your links.

That it serves you.

    
answered by 28.06.2017 в 15:23