Décalage horaire.

WRInaute accro
Bonjour

Je cherche à identifier les visiteurs venant des USA pour les bloquer.

C'est un problème de décalage horaire.

J'ai deux façons pour résoudre celà :

1) Calculer le décalage horaire entre l'heure locale du browser, et celle du serveur.

2) Calculer l'offsettime entre le browser et UTC, et puis entre UTC et Europe/Paris.

Voir ci-dessous :


PHP:
<?php
session_start();
$str  = "<!DOCTYPE html>\n";
$str .= "<html lang=\"fr\">\n";
$str .= "<head>\n";
$str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
$str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
$str .= "<head>\n";
$str .= "    <script>\n";
$str .= "        function lien()\n";
$str .= "        {\n";
$str .= "            date_visitor = new Date();\n";
$str .= "            var year = date_visitor.getFullYear();\n";
$str .= "            var month = date_visitor.getMonth();\n";
$str .= "            var day = date_visitor.getDate();\n";
$str .= "            var hour = date_visitor.getHours();\n";
$str .= "            var minute = date_visitor.getMinutes();\n";
$str .= "            var second = date_visitor.getSeconds();\n";
$str .= "\n";
$str .= "        var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
$str .= "          \n";
$str .= "       var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
$str .= "        location.href=chainurl;\n";
$str .= "        }\n";
$str .= "    </script>\n";
$str .= "    <title>Titre</title>\n";
$str .= "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=iso-8859-1\" />\n";
$str .= "</head>\n";
echo $str;
if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
{
    echo "<body onLoad=lien()>\n";
}
else if (!isset($_SESSION['decalage']))
{
    echo "<body>\n";
    if(isset($_GET['timestamp']))
    {
        $timestamp = (int)$_GET['timestamp'];
//            var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
        $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
        $timestamp = $year % (12 * 50 * 24 * 60 * 60);
        $month = floor($timestamp / (50 * 24 * 60 * 60));
        $timestamp = $month % (50 * 24 * 60 * 60);
        $day = floor($timestamp / (24 * 60 * 60));
        $timestamp = $day % (24 * 60 * 60);
        $hour = floor($timestamp / (60 * 60));
        $timestamp = $hour % (60 * 60);
        $minute = floor($timestamp / 60);
        $second = $minute % 60;
        $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
        $date_browser = new DateTime($date_locale);
        $date_locale = $date_browser->format('Y-m-d H:i:s');
        $date_now = new DateTime("now");
        $date_server = $date_now->format('Y-m-d H:i:s');
        $decalage = date_diff($date_browser, $date_now);
        $timestamp_received = mktime ($hour, $minute, $second, $month, $day, $year);
        $_SESSION['decalage'] = $decalage->format('%Y-%M-%D %H:%I:%S');
        echo "\t\t\t" . '$_SESSION[\'decalage\'] = ' . $_SESSION['decalage'];
        echo "<br /><br />\n";
        exit(0);
    }
}
?>


Le timestamp est virtuel, ses composantes supposent 12 mois/an , 50 jours/mois, 24 heures/jour, 60 minutes/heure, et 60 secondes/minute.

Après, je décompose le timestamp.

Sur mon localhost, au test, j'obtiens ceci :

$_SESSION['decalage'] = 00-11-21 21:53:30

Bizarre, bizarre...

Merci beaucoup pour votre aide.

Respects.
 
WRInaute passionné
JS a la méthode getTimezoneOffset() qui retourne l'offset.
Et comme de toute façon vous êtes obligé d'utiliser JS pour connaître l'heure du browser, autant renvoyer ça...
 
WRInaute accro
PHP:
<?php
session_start();
$str  = "<!DOCTYPE html>\n";
$str .= "<html lang=\"fr\">\n";
$str .= "<head>\n";

J'ai les yeux qui saignent.
 
WRInaute accro
Bonjour rick38

Effectivement la directive Californienne je ne tiens pas à me colleter avec les Américains.

J'ai maxi 5 vu/jour des US...

Et oui j'ai fait une autre implémentation avec getTimezoneOffset(), mais j'ai des difficultés avec le DST Europe/Paris, l'heure d'été/hiver cessera peut-être en 2021.

Je vais investiguer pour ce décalage faux.

Merci beaucoup pour votre aide.

Respects.
 
WRInaute accro
Et voilà :

J'ai rajouté une fonction isLocalDST() qui rend l'offset local en heure et minutes.

Par ailleurs, çà me donne 00:00 de décalage sur mon VPS à Strasbourg.

Merci de tester si vous êtes à un autre fuseau horaire qu'à 'Europe/Paris'.

Voici l'url :

https://www.pronostics-courses.fr/OffsetTime.php

Et :

$date_locale1 = 2019-11-21 11:53:53

$date_locale2 = 2019-11-21 11:53:53+01:00

isLocalDST() : Array ( [0] => 1 [1] => 0 )

$date_server = 2019-11-21 11:53:05+01:00

$_SESSION['decalage'] = 00:00

Amicalement.


PHP:
<?php
session_start();
function isLocalDST() {
    $date = new DateTime("now", new DateTimeZone('Europe/Paris'));
    $str_date = date_format($date, 'Y-m-d H:i:sP');
    $offset_local = array();
    if(preg_match("#[-+]([0-9{2}):([0-9]{2})$#", $str_date, $output))
    {
        print_r($ouput);
        echo "<br /><br />\n";
        $offset_local[0] = (int)$output[1];
        $offset_local[1] = (int)$output[2];
        print_r($offset_local);
        echo "<br /><br />\n";
    }
    else
    {
        $date = new DateTime("now", new DateTimeZone('UTC'));
        $str_date = $date->format('Y-m-d H:i:s');
        $year = $date->format('Y');
        /**
        * Fixer le $isDST
        * suivant la date
        * et la législation
        * Française.
        **/
        $date_heure_ete = $year . '-03-31 01:00:00';
        $date_heure_hiver = $year . '-10-31 01:00:00';
        $date_ete = new DateTime($date_heure_ete, new DateTimeZone('UTC'));
        $date_ete->modify('last sunday');
        $date_ete = date_format($date, 'Y-m-d H:i:s');
        $date_hiver = new DateTime($date_heure_hiver, new DateTimeZone('UTC'));
        $date_hiver->modify('last sunday');
        $date_hiver = date_format($date, 'Y-m-d H:i:s');
        /**
        La période de l'heure d'été commence à 1 h UTC, le dernier dimanche de mars ;
        La période de l'heure d'été se termine à 1 h UTC, le dernier dimanche d'octobre.
        **/
        if(($str_date >= $date_ete) && ($str_date < $date_hiver)) {
            $offset_local[0] = 2;
               
        } else {
            $offset_local[0] = 1;
        }
        $offset_local[1] = 0;
    }
    return $offset_local;
}
$str  = "<!DOCTYPE html>\n";
$str .= "<html lang=\"fr\">\n";
$str .= "<head>\n";
$str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
$str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
$str .= "<head>\n";
$str .= "    <script>\n";
$str .= "    function lien()\n";
$str .= "    {\n";
$str .= "        date_visitor = new Date();\n";
$str .= "        var year = date_visitor.getFullYear();\n";
$str .= "        var month = 1 + date_visitor.getMonth();\n";
$str .= "        var day = date_visitor.getDate();\n";
$str .= "        var hour = date_visitor.getHours();\n";
$str .= "        var minute = date_visitor.getMinutes();\n";
$str .= "        var second = date_visitor.getSeconds();\n";
$str .= "\n";
$str .= "        var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
$str .= "\n";
$str .= "        var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
$str .= "        location.href=chainurl;\n";
$str .= "    }\n";
$str .= "    </script>\n";
$str .= "    <title>Titre</title>\n";
$str .= "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=iso-8859-1\" />\n";
$str .= "</head>\n";
echo $str;
if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
{
    echo "<body onLoad=lien()>\n";
}
else if (!isset($_SESSION['decalage']))
{
    echo "<body>\n";
    if(isset($_GET['timestamp']))
    {
        $timestamp = (int)$_GET['timestamp'];
//        var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
        $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
        $timestamp %= (12 * 50 * 24 * 60 * 60);
        $month = floor($timestamp / (50 * 24 * 60 * 60));
        $timestamp %= (50 * 24 * 60 * 60);
        $day = floor($timestamp / (24 * 60 * 60));
        $timestamp %= (24 * 60 * 60);
        $hour = floor($timestamp / (60 * 60));
        $timestamp %= (60 * 60);
        $minute = floor($timestamp / 60);
        $second = $minute % 60;
        $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
        echo "\t\t\t" . '$date_locale1 = ' . $date_locale;
        echo "<br /><br />\n";
        $date_browser = new DateTime($date_locale, new DateTimeZone('Europe/Paris'));
        $date_locale = $date_browser->format('Y-m-d H:i:sP');
        echo "\t\t\t" . '$date_locale2 = ' . $date_locale;
        echo "<br /><br />\n";
        $array_locale = array();
        $array_locale = isLocalDST();
        echo "isLocalDST() :\n";
        print_r($array_locale);
        echo "<br /><br />\n";
        $date_now = new DateTime("now", new DateTimeZone('Europe/Paris'));
        $date_server = $date_now->format('Y-m-d H:i:sP');
        echo "\t\t\t" . '$date_server = ' . $date_server;
        echo "<br /><br />\n";
        $decalage = $date_browser->diff($date_now);
        $timestamp_received = mktime ($hour, $minute, $second, $month, $day, $year);
//        $_SESSION['decalage'] = $decalage->format('%Y-%M-%D %H:%I:%S');
       
        $_SESSION['decalage'] = $decalage->format('%H:%I');
        echo "\t\t\t" . '$_SESSION[\'decalage\'] = ' . $_SESSION['decalage'];
        echo "<br /><br />\n";
    }
    echo "</body>\n";
    echo "</html>\n";
}
?>
 
WRInaute passionné
J'ai pareil : isLocalDST() : Array ( [0] => 1 [1] => 0 )


(et je me demande en quel fuseau sont les Googlebots :D)
 
WRInaute accro
fantastique!! ce code permet si j'ai bien compris de bloquer des fuseaux horaires
Et bloquer un fuseau horaire en asie, c'est possible et facilement paramétrable ?
Comment ca marche svp?
 
WRInaute accro
Bonsoir Monsieur K0gar

Pardon, j'ai fait une erreur.

Pour catégoriser les fuseaux horaires :

https://www.php.net/manual/fr/class.datetimezone.php

Sinon, même méthode que ci-dessous.

*******************************************************************************

PHP a ces fuseaux horaires à l'url :

https://www.php.net/manual/fr/timezones.php

C'est indiqué : "Note: La dernière version de la base de données des fuseaux horaires peut être installée via la commande PECL » timezonedb"

identifier pour chaque catégories l'offset minimum et maximum.

Et puis vérifier sil'offset local est > min et < max.

Evidemment ce n'est pas fiable ni précis.

Un offset englobe des régions non voulues.

Dans mon cas je vise les US seulement.

Merci beaucoup pour ton aide.

Amicalement.
 
Dernière édition:
WRInaute accro
Bon bon...

D'après le PHP Manual :

PHP:
<?php
$theTime = time(); # specific date/time we're checking, in epoch seconds.

$tz = new DateTimeZone('America/Los_Angeles');
$transition = $tz->getTransitions($theTime,$theTime);

# only one array should be returned into $transition. Now get the data:
$offset = $transition[0]['offset'];
$abbr = $transition[0]['abbr'];
?>
 
WRInaute accro
Voili voilou

Pour les US :

Minimum et maximum d'offset.


PHP:
<?php

$theTime = time(); # specific date/time we're checking, in epoch seconds.
$min = 50000;
$max = -50000;
// 2ème paramètre = 'ISO 3166-1'
$timeZones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'US');
foreach ( $timeZones as $key => $zoneName )
{
    $tz = new DateTimeZone($zoneName);
    $transition = $tz->getTransitions($theTime,$theTime);
    $offset    = $transition[0]['offset'];
    $min = min($min, $offset);
    $max = max($max, $offset);
}
echo '$offset_min = ' . $min . ' $offset_max = ' . $max;
echo "\n\n";

?>


Celà donne : $offset_min = -36000 $offset_max = -18000

Amicalement.
 
WRInaute accro
Nickel ce code qui donne la plage d'un fuseau horaire en PHP :) Tu peux le soumettre a la doc PHP ;)

Je reste un peu sur ma faim, ca aurait été nickel d'avoir le code dans son ensemble.. Il est loin d'etre évident a comprendre ton code avec ce renvoie de header pour obtenir l'heure de connexion coté client. En tout cas merci.
 
WRInaute accro
Voilà



PHP:
<?php
session_start();
require("/var/www/html/php/config/headers_cache.php");
function US_offset()
{
    $array_offset = array();
    $array_zone_name = array();
    $array_zone_indice = array();
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 50000;
    $max = -50000;
    // 2ème paramètre = 'ISO 3166-1'
    $timeZones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'US');
    foreach ( $timeZones as $key => $zoneName )
    {
        $tz = new DateTimeZone($zoneName);
        $transition = $tz->getTransitions($theTime,$theTime);
        $offset    = $transition[0]['offset'];
        $min = min($min, $offset);
        $max = max($max, $offset);
    }
    return array($min, $max);
}
function IsAllowedUS($timestamp)
{
    //    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
    $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
    $timestamp %= (12 * 50 * 24 * 60 * 60);
    $month = floor($timestamp / (50 * 24 * 60 * 60));
    $timestamp %= (50 * 24 * 60 * 60);
    $day = floor($timestamp / (24 * 60 * 60));
    $timestamp %= (24 * 60 * 60);
    $hour = floor($timestamp / (60 * 60));
    $timestamp %= (60 * 60);
    $minute = floor($timestamp / 60);
    $second = $minute % 60;
    $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
    echo "\t\t\t" . '$date_locale = ' . $date_locale;
    echo "<br /><br />\n";
    $date_browser = new DateTime($date_locale, new DateTimeZone('UTC'));
    $dateBrowserUTC = $date_browser->format('Y-m-d H:i:sP');
    echo "\t\t\t" . '$dateBrowserUTC = ' . $dateBrowserUTC;
    echo "<br /><br />\n";
    $array_US = array();
    $array_US = US_offset();
    echo "US_offset() :\n";
    print_r($array_US);
    echo "<br /><br />\n";
    $offsetUS_min = (int)$array_US[0];
    $offsetUS_max = (int)$array_US[1];
   
    $date_now = new DateTime("now", new DateTimeZone('UTC'));
    $dateServerUTC = $date_now->format('Y-m-d H:i:sP');
    echo "\t\t\t" . '$dateServerUTC = ' . $dateServerUTC;
    echo "<br /><br />\n";
    $decalage = $date_browser->diff($date_now);
    // $timestamp_received = mktime ($hour, $minute, $second, $month, $day, $year);
    $offset = (int)$decalage->format('%H%I');
    $_SESSION['decalage'] = $offset;
    echo "\t\t\t" . '$_SESSION[\'decalage\'] = ' . $_SESSION['decalage'];
    echo "<br /><br />\n";
    if(($offsetUS_min <= $offset) && ($offsetUS_max >= $offset))
    {
        return false;
    }
    else
    {
        return true;
    }
}
$str  = "<!DOCTYPE html>\n";
$str .= "<html lang=\"fr\">\n";
$str .= "<head>\n";
$str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
$str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
$str .= "<head>\n";
$str .= "    <script>\n";
$str .= "\n";
$str .= "    window.newLocation = function( location ) {\n";
$str .= "        var newLocation = ( typeof location === 'string' ) ? location : window.location.href,\n";
$str .= "        appendType = ( newLocation.indexOf('?') < 0 ) ? '?' : '&';\n";
$str .= "        window.location = newLocation + appendType + '_t=' + (new Date()).getTime();\n";
$str .= "    }\n";
$str .= "\n";
$str .= "    function lien() {\n";
$str .= "        date_visitor = new Date();\n";
$str .= "        var year = date_visitor.getFullYear();\n";
$str .= "        var month = 1 + date_visitor.getMonth();\n";
$str .= "        var day = date_visitor.getDate();\n";
$str .= "        var hour = date_visitor.getHours();\n";
$str .= "        var minute = date_visitor.getMinutes();\n";
$str .= "        var second = date_visitor.getSeconds();\n";
$str .= "\n";
$str .= "        var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
$str .= "\n";
$str .= "        var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
// $str .= "        location.href=chainurl;\n";
$str .= "        window.newLocation(chainurl);\n";
$str .= "    }\n";
$str .= "    </script>\n";
$str .= "    <title>Titre</title>\n";
$str .= "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=iso-8859-1\" />\n";
$str .= "</head>\n";
echo $str;
if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
{
    echo "<body onLoad=lien()>\n";
}
else if (!isset($_SESSION['decalage']))
{
    echo "<body>\n";
    if(isset($_GET['timestamp']))
    {
        $timestamp = (int)$_GET['timestamp'];
        if(IsAllowedUS($timestamp))
        {
            echo "You are not connecting from the US.";
        }
        else
        {
            echo "You are connecting from the US.";
        }
        echo "<br /><br />\n";
    }
    echo "</body>\n";
    echo "</html>\n";
}
?>


Celà me donne :

$date_locale = 2019-11-22 11:16:16

$dateBrowserUTC = 2019-11-22 11:16:16+00:00

US_offset() : Array ( [0] => -36000 [1] => -18000 )

$dateServerUTC = 2019-11-22 10:16:52+00:00

$_SESSION['decalage'] = 59

You are not connecting from the US.

Amicalement.
 
Dernière édition:
WRInaute accro
Rebond

Avec deux fonctions démarrage/arrêt de la session, agrémentée d'un cache à 0, et avant le Javascript, arrêt de la session.

J'ai tiré ces fonctions du PHP Manual.

J'obtiens 59 comme décalage ?

Tout est en UTC sauf FR_offset() qui ne sert à rien.

Bien à vous.



PHP:
<?php
function start_the_session() {
    /**
    * avoid scrapping
    * headers.
    **/
    session_cache_limiter('nocache');
    session_set_cookie_params(0, '/', '.pronostics-courses.fr', 0, 1);
    session_start();
    $_SESSION = array();
    session_unset();
    session_destroy();
    session_set_cookie_params(0, '/', '.pronostics-courses.fr', 0, 1);
    session_start();
}
function stop_the_session() {
    $_SESSION = array();
    if (ini_get('session.use_cookies'))
    {
        $p = session_get_cookie_params();
        setcookie(session_name(), '', time() - 31536000, $p['path'], $p['domain'], $p['secure'], $p['httponly']);
    }
    session_unset();
    session_destroy();
}
start_the_session();
function US_offset()
{
    $array_offset = array();
    $array_zone_name = array();
    $array_zone_indice = array();
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 50000;
    $max = -50000;
    // 2ème paramètre = 'ISO 3166-1'
    $timeZones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'US');
    foreach ( $timeZones as $key => $zoneName )
    {
        $tz = new DateTimeZone($zoneName);
        $transition = $tz->getTransitions($theTime,$theTime);
        $offset    = $transition[0]['offset'];
        $min = min($min, $offset);
        $max = max($max, $offset);
    }
    return array($min, $max);
}
function FR_offset()
{
    $date = new DateTime("now", new DateTimeZone('Europe/Paris'));
    $str_date = date_format($date, 'Y-m-d H:i:sP');
    echo $str_date;
    echo "<br /><br />\n";
    $offset_local = array();
    if(preg_match("#[-+]([0-9{2}):([0-9]{2})$#", $str_date, $output))
    {
        $offset_local = 60 * $output[1] * 100 + $output[2];
    }
    else
    {
        $date = new DateTime("now", new DateTimeZone('UTC'));
        $str_date = $date->format('Y-m-d H:i:s');
        $year = $date->format('Y');
        /**
        * Fixer le $isDST
        * suivant la date
        * et la législation
        * Française.
        **/
        $date_heure_ete = $year . '-03-31 01:00:00';
        $date_heure_hiver = $year . '-10-31 01:00:00';
        $date_ete = new DateTime($date_heure_ete, new DateTimeZone('UTC'));
        $date_ete->modify('last sunday');
        $date_ete = date_format($date, 'Y-m-d H:i:s');
        $date_hiver = new DateTime($date_heure_hiver, new DateTimeZone('UTC'));
        $date_hiver->modify('last sunday');
        $date_hiver = date_format($date, 'Y-m-d H:i:s');
        /**
        La période de l'heure d'été commence à 1 h UTC, le dernier dimanche de mars ;
        La période de l'heure d'été se termine à 1 h UTC, le dernier dimanche d'octobre.
        **/
        if(($str_date >= $date_ete) && ($str_date < $date_hiver)) {
            $offset_local = 2 * 60 * 100;
        } else {
            $offset_local = 60 * 100;
        }
    }
    return $offset_local;
}
function IsAllowedUS($timestamp)
{
    //    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
    $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
    $timestamp %= (12 * 50 * 24 * 60 * 60);
    $month = floor($timestamp / (50 * 24 * 60 * 60));
    $timestamp %= (50 * 24 * 60 * 60);
    $day = floor($timestamp / (24 * 60 * 60));
    $timestamp %= (24 * 60 * 60);
    $hour = floor($timestamp / (60 * 60));
    $timestamp %= (60 * 60);
    $minute = floor($timestamp / 60);
    $second = $minute % 60;
    $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
    echo "\t\t\t" . '$date_locale = ' . $date_locale;
    echo "<br /><br />\n";
    $date_browser = new DateTime($date_locale, new DateTimeZone('UTC'));
    $dateBrowserUTC = $date_browser->format('Y-m-d H:i:sP');
    echo "\t\t\t" . '$dateBrowserUTC = ' . $dateBrowserUTC;
    echo "<br /><br />\n";
    $array_US = array();
    $array_US = US_offset();
    echo "US_offset() :\n";
    print_r($array_US);
    echo "<br /><br />\n";
    $offsetUS_min = (int)$array_US[0];
    $offsetUS_max = (int)$array_US[1];
    /**
    $offset_FR = FR_offset();
    echo "FR_offset() :\n";
    echo $offset_FR;
    echo "<br /><br />\n";
    **/
    $date_now = new DateTime("now", new DateTimeZone('UTC'));
    $dateServerUTC = $date_now->format('Y-m-d H:i:sP');
    echo "\t\t\t" . '$dateServerUTC = ' . $dateServerUTC;
    echo "<br /><br />\n";
    $decalage = $date_browser->diff($date_now);
    // $timestamp_received = mktime ($hour, $minute, $second, $month, $day, $year);
    $offset = (int)$decalage->format('%H%I');
    $_SESSION['decalage'] = $offset;
    echo "\t\t\t" . '$_SESSION[\'decalage\'] = ' . $_SESSION['decalage'];
    echo "<br /><br />\n";
    if(($offsetUS_min <= $offset) && ($offsetUS_max >= $offset))
    {
        return false;
    }
    else
    {
        return true;
    }
}
$str  = "<!DOCTYPE html>\n";
$str .= "<html lang=\"fr\">\n";
$str .= "<head>\n";
$str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
$str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
$str .= "<head>\n";
$str .= "    <script>\n";
$str .= "\n";
$str .= "    window.newLocation = function( location ) {\n";
$str .= "        var newLocation = ( typeof location === 'string' ) ? location : window.location.href,\n";
$str .= "        appendType = ( newLocation.indexOf('?') < 0 ) ? '?' : '&';\n";
$str .= "        window.location = newLocation + appendType + '_t=' + (new Date()).getTime();\n";
$str .= "    }\n";
$str .= "\n";
$str .= "    function lien() {\n";
$str .= "        date_visitor = new Date();\n";
$str .= "        var year = date_visitor.getFullYear();\n";
$str .= "        var month = 1 + date_visitor.getMonth();\n";
$str .= "        var day = date_visitor.getDate();\n";
$str .= "        var hour = date_visitor.getHours();\n";
$str .= "        var minute = date_visitor.getMinutes();\n";
$str .= "        var second = date_visitor.getSeconds();\n";
$str .= "\n";
$str .= "        var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
$str .= "\n";
$str .= "        var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
// $str .= "        location.href=chainurl;\n";
$str .= "        window.newLocation(chainurl);\n";
$str .= "    }\n";
$str .= "    </script>\n";
$str .= "    <title>Titre</title>\n";
$str .= "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=iso-8859-1\" />\n";
$str .= "</head>\n";
echo $str;
if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
{
    echo "<body onLoad=lien()>\n";
   
    stop_the_session();
}
else if (!isset($_SESSION['decalage']))
{
    echo "<body>\n";
    if(isset($_GET['timestamp']))
    {
        $timestamp = (int)$_GET['timestamp'];
        if(IsAllowedUS($timestamp))
        {
            echo "You are not connecting from the US.";
        }
        else
        {
            echo "You are connecting from the US.";
        }
        echo "<br /><br />\n";
    }
    echo "</body>\n";
    echo "</html>\n";
    stop_the_session();
}
?>
 
WRInaute accro
Il déchire ton code :)

J'ai remplacé 'US' par 'FR' (DateTimeZone::listIdentifiers(DateTimeZone::pER_COUNTRY, 'FR'))
La plage est a 1h (3600) elle devrait pas être a 0h ? car la j'ai pas accès si je navigue de la France.

En sortie :
$date_locale = 41-05-05 03:14:14
$dateBrowserUTC = 2041-05-05 03:14:14+00:00
US_offset() : Array ( [0] => 3600 [1] => 3600 )
$dateServerUTC = 2019-11-23 06:17:35+00:00
$_SESSION['decalage'] = 2056
You are not connecting from the US.
 
WRInaute accro
Pardon K0gar

Le cookie de session a pour ndd : .pronostics-courses.fr

Il faut l'adapter avec ton ndd.


Côté serveur :

US_offset() : Array ( [0] => 3600 [1] => 3600 )

C'est le décalage horaire UTC de la France.

Côté browser :

La date est calculée en Javascript avec : "new Date" ( en UTC ).

Tout est en UTC.

La date de ton browser est incorrecte.

D'un autre côté, même si tes fuseaux horaires ( Français ) sont filtrés, comme il n'y a pas de filtrage réel c'est anormal de ne pas pouvoir se connecter.

Amicalement.
 
Dernière édition:
WRInaute accro
Je te demande pardon

Peut-être que les fonctions de session mettaient le souk.

Voilà le code.

Surtout changer .pronostics-courses.fr par ton ndd.

Si tu met la France à filtrer c'et normal que celà à réponde : "You are connecting from the US."

Amicalement.



PHP:
<?php
function start_the_session() {
    /**
    * avoid scrapping
    * headers.
    **/
    session_cache_limiter('nocache');
    /**
    session_set_cookie_params(0, '/', '.pronostics-courses.fr', 0, 1);
    session_start();
    $_SESSION = array();
    session_unset();
    session_destroy();
    **/
    session_set_cookie_params(0, '/', '.pronostics-courses.fr', 0, 1);
    session_start();
}
function stop_the_session() {
    /**
    $_SESSION = array();
    if (ini_get('session.use_cookies'))
    {
        $p = session_get_cookie_params();
        setcookie(session_name(), '', time() - 31536000, $p['path'], $p['domain'], $p['secure'], $p['httponly']);
    }
    **/
    session_unset();
    session_destroy();
}
start_the_session();
function US_offset()
{
    $array_offset = array();
    $array_zone_name = array();
    $array_zone_indice = array();
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 50000;
    $max = -50000;
    // 2ème paramètre = 'ISO 3166-1'
    $timeZones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'US');
    foreach ( $timeZones as $key => $zoneName )
    {
        $tz = new DateTimeZone($zoneName);
        $transition = $tz->getTransitions($theTime,$theTime);
        $offset    = $transition[0]['offset'];
        $min = min($min, $offset);
        $max = max($max, $offset);
    }
    return array($min, $max);
}
function FR_offset()
{
    $date = new DateTime("now", new DateTimeZone('Europe/Paris'));
    $str_date = date_format($date, 'Y-m-d H:i:sP');
    echo $str_date;
    echo "<br /><br />\n";
    $offset_local = array();
    if(preg_match("#[-+]([0-9{2}):([0-9]{2})$#", $str_date, $output))
    {
        $offset_local = 60 * $output[1] * 100 + $output[2];
    }
    else
    {
        $date = new DateTime("now", new DateTimeZone('UTC'));
        $str_date = $date->format('Y-m-d H:i:s');
        $year = $date->format('Y');
        /**
        * Fixer le $isDST
        * suivant la date
        * et la législation
        * Française.
        **/
        $date_heure_ete = $year . '-03-31 01:00:00';
        $date_heure_hiver = $year . '-10-31 01:00:00';
        $date_ete = new DateTime($date_heure_ete, new DateTimeZone('UTC'));
        $date_ete->modify('last sunday');
        $date_ete = date_format($date, 'Y-m-d H:i:s');
        $date_hiver = new DateTime($date_heure_hiver, new DateTimeZone('UTC'));
        $date_hiver->modify('last sunday');
        $date_hiver = date_format($date, 'Y-m-d H:i:s');
        /**
        La période de l'heure d'été commence à 1 h UTC, le dernier dimanche de mars ;
        La période de l'heure d'été se termine à 1 h UTC, le dernier dimanche d'octobre.
        **/
        if(($str_date >= $date_ete) && ($str_date < $date_hiver)) {
            $offset_local = 2 * 60 * 100;
        } else {
            $offset_local = 60 * 100;
        }
    }
    return $offset_local;
}
function IsAllowedUS($timestamp)
{
    //    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
    $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
    $timestamp %= (12 * 50 * 24 * 60 * 60);
    $month = floor($timestamp / (50 * 24 * 60 * 60));
    $timestamp %= (50 * 24 * 60 * 60);
    $day = floor($timestamp / (24 * 60 * 60));
    $timestamp %= (24 * 60 * 60);
    $hour = floor($timestamp / (60 * 60));
    $timestamp %= (60 * 60);
    $minute = floor($timestamp / 60);
    $second = $minute % 60;
    $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
    echo "\t\t\t" . '$date_locale = ' . $date_locale;
    echo "<br /><br />\n";
    $date_browser = new DateTime($date_locale, new DateTimeZone('UTC'));
    $dateBrowserUTC = $date_browser->format('Y-m-d H:i:sP');
    echo "\t\t\t" . '$dateBrowserUTC = ' . $dateBrowserUTC;
    echo "<br /><br />\n";
    $array_US = array();
    $array_US = US_offset();
    echo "US_offset() :\n";
    print_r($array_US);
    echo "<br /><br />\n";
    $offsetUS_min = (int)$array_US[0];
    $offsetUS_max = (int)$array_US[1];
    /**
    $offset_FR = FR_offset();
    echo "FR_offset() :\n";
    echo $offset_FR;
    echo "<br /><br />\n";
    **/
    $date_now = new DateTime("now", new DateTimeZone('UTC'));
    $dateServerUTC = $date_now->format('Y-m-d H:i:sP');
    echo "\t\t\t" . '$dateServerUTC = ' . $dateServerUTC;
    echo "<br /><br />\n";
    $decalage = $date_browser->diff($date_now);
    // $timestamp_received = mktime ($hour, $minute, $second, $month, $day, $year);
    $offset = (int)$decalage->format('%H%I');
    $_SESSION['decalage'] = $offset;
    echo "\t\t\t" . '$_SESSION[\'decalage\'] = ' . $_SESSION['decalage'];
    echo "<br /><br />\n";
    if(($offsetUS_min <= $offset) && ($offsetUS_max >= $offset))
    {
        return false;
    }
    else
    {
        return true;
    }
}
$str  = "<!DOCTYPE html>\n";
$str .= "<html lang=\"fr\">\n";
$str .= "<head>\n";
$str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
$str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
$str .= "<head>\n";
$str .= "    <script>\n";
$str .= "\n";
$str .= "    window.newLocation = function( location ) {\n";
$str .= "        var newLocation = ( typeof location === 'string' ) ? location : window.location.href,\n";
$str .= "        appendType = ( newLocation.indexOf('?') < 0 ) ? '?' : '&';\n";
$str .= "        window.location = newLocation + appendType + '_t=' + (new Date()).getTime();\n";
$str .= "    }\n";
$str .= "\n";
$str .= "    function lien() {\n";
$str .= "        date_visitor = new Date();\n";
$str .= "        var year = date_visitor.getFullYear();\n";
$str .= "        var month = 1 + date_visitor.getMonth();\n";
$str .= "        var day = date_visitor.getDate();\n";
$str .= "        var hour = date_visitor.getHours();\n";
$str .= "        var minute = date_visitor.getMinutes();\n";
$str .= "        var second = date_visitor.getSeconds();\n";
$str .= "\n";
$str .= "        var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
$str .= "\n";
$str .= "        var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
// $str .= "        location.href=chainurl;\n";
$str .= "        window.newLocation(chainurl);\n";
$str .= "    }\n";
$str .= "    </script>\n";
$str .= "    <title>Titre</title>\n";
$str .= "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=iso-8859-1\" />\n";
$str .= "</head>\n";
echo $str;
if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
{
    echo "<body onLoad=lien()>\n";
  
    stop_the_session();
}
else if (!isset($_SESSION['decalage']))
{
    echo "<body>\n";
    if(isset($_GET['timestamp']))
    {
        $timestamp = (int)$_GET['timestamp'];
        if(IsAllowedUS($timestamp))
        {
            echo "You are not connecting from the US.";
        }
        else
        {
            echo "You are connecting from the US.";
        }
        echo "<br /><br />\n";
    }
    echo "</body>\n";
    echo "</html>\n";
    stop_the_session();
}
?>

{/code]
 
Dernière édition:
WRInaute accro
Excusez-moi

La méthode DateTimeZone::getTransitions($timestamp_min, $timestamp_max), donne "les transitions" ( $result ) de la DateTimeZone entre les timestamp min et max.

En particulier, $result['offset'] est dans quelles unités ?

Merci.
 
WRInaute accro
Bonjour K0ogar

J'ai mis une version plus cool avec les offset en minutes.

Offset min et max US : -600 et +0

Offset d'hiver FR : 60.

Si tu l'installe comme tu dis ( listIdentifiers(DateTimeZone:pER_COUNTRY, 'FR'), c'est normal qu'il réponde 'You are connecting from the US', puisque c'est l"Identifier de la France qui est pris pour le filtrage.

En laissant 'US' au lieu de 'FR', tu ne devrais plus être filtré.

Voici le code :

Merci beaucoup pour ton aide.

Amicalement.


PHP:
<?php
function start_the_session() {
    /**
    * avoid scrapping
    * headers.
    **/
    session_cache_limiter('nocache');
    /**
    session_set_cookie_params(0, '/', '.pronostics-courses.fr', 0, 1);
    session_start();
    $_SESSION = array();
    session_unset();
    session_destroy();
    **/
    session_set_cookie_params(0, '/', '.pronostics-courses.fr', 0, 1);
    session_start();
}
function stop_the_session() {
    /**
    $_SESSION = array();
    if (ini_get('session.use_cookies'))
    {
        $p = session_get_cookie_params();
        setcookie(session_name(), '', time() - 31536000, $p['path'], $p['domain'], $p['secure'], $p['httponly']);
    }
    **/
    session_unset();
    session_destroy();
}
start_the_session();
function US_offset()
{
    $array_offset = array();
    $array_zone_name = array();
    $array_zone_indice = array();
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 5000000.0;
    $max = -5000000.0;
    // 2ème paramètre = 'ISO 3166-1'
    $timeZones = DateTimeZone::listIdentifiers(DateTimeZone::AMERICA);
    foreach ( $timeZones as $zoneName )
    {
        $tz = new DateTimeZone($zoneName);
        $transitions = $tz->getTransitions($theTime,$theTime);
        foreach($transitions as $key => $offset)
        {  
            $tmp_offset    = (int)$offset['offset'];
            $min = min($min, $tmp_offset);
            $max = max($max, $tmp_offset);
        }
    }
    $array_offset_min =  explode(':', sprintf('%+03d:%02u', $min / 3600, abs($min) % 3600 / 60));
    $array_offset_max =  explode(':', sprintf('%+03d:%02u', $max / 3600, abs($max) % 3600 / 60));
    $offset_min = $array_offset_min[0] * 60 + $array_offset_min[1];
    $offset_max = $array_offset_max[0] * 60 + $array_offset_max[1];
    return array($offset_min, $offset_max);
}
function FR_offset()
{
    $date = new DateTime("now", new DateTimeZone('Europe/Paris'));
    $str_date = date_format($date, 'Y-m-d H:i:sP');
    echo "\tDate France locale.\n";
    echo "\t\t" . $str_date;
    echo "<br /><br />\n";
    $offset_local = array();
    if(preg_match("#[-+]([0-9]{2}):([0-9]{2})$#", $str_date, $output))
    {
        $offset_local = (float)$output[1] * 60 + $output[2];
        echo "\tOffset France local.\n";
        echo "\t\t" . $offset_local;
        echo "<br /><br />\n";
    }
    else
    {
        $date = new DateTime("now", new DateTimeZone('UTC'));
        $str_date = $date->format('Y-m-d H:i:s');
        $year = $date->format('Y');
        /**
        * Fixer le $isDST
        * suivant la date
        * et la législation
        * Française.
        **/
        $date_heure_ete = $year . '-03-31 01:00:00';
        $date_heure_hiver = $year . '-10-31 01:00:00';
        $date_ete = new DateTime($date_heure_ete, new DateTimeZone('UTC'));
        $date_ete->modify('last sunday');
        $date_hiver = new DateTime($date_heure_hiver, new DateTimeZone('UTC'));
        $date_hiver->modify('last sunday');
        /**
        La période de l'heure d'été commence à 1 h UTC, le dernier dimanche de mars ;
        La période de l'heure d'été se termine à 1 h UTC, le dernier dimanche d'octobre.
        **/
        if(($str_date >= $date_ete) && ($str_date < $date_hiver))
        {
            $offset_local = (float)(2 * 60 + 0);
        }
        else
        {
            $offset_local = (float)(1 * 60 + 0);
        }
        echo "\tOffset France local.\n";
        echo "\t\t" . $offset_local;
        echo "<br /><br />\n";
    }
    return $offset_local;
}
function IsAllowedUS($timestamp)
{
    //    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
    $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
    $timestamp %= (12 * 50 * 24 * 60 * 60);
    $month = floor($timestamp / (50 * 24 * 60 * 60));
    $timestamp %= (50 * 24 * 60 * 60);
    $day = floor($timestamp / (24 * 60 * 60));
    $timestamp %= (24 * 60 * 60);
    $hour = floor($timestamp / (60 * 60));
    $timestamp %= (60 * 60);
    $minute = floor($timestamp / 60);
    $second = $minute % 60;
    $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
    echo "\t" . '$date_locale = ' . "\t\t" . $date_locale;
    echo "<br /><br />\n";
    $date_browser = new DateTime($date_locale, new DateTimeZone('UTC'));
    $dateBrowserUTC = $date_browser->format('Y-m-d H:i:sP');
    echo "\t\t" . '$dateBrowserUTC = ' . "\t\t" . $dateBrowserUTC;
    echo "<br /><br />\n";
    $offset_FR = FR_offset();
    echo "FR_offset() :\n";
    echo $offset_FR;
    echo "<br /><br />\n";
    $array_US = array();
    $array_US = US_offset();
    $offsetUS_min = $array_US[0];
    $offsetUS_max = $array_US[1];
    echo "\t" . '$offsetUS_min = ' . "\t\t" . $offsetUS_min;
          
    echo "<br /><br />\n";
    echo "\t" . '$offsetUS_max = ' . "\t\t" . $offsetUS_max;
    echo "<br /><br />\n";
    $date_now = new DateTime("now", new DateTimeZone('UTC'));
    $dateServerUTC = $date_now->format('Y-m-d H:i:sP');
    echo "\t" . '$dateServerUTC = ' . "\t\t" . $dateServerUTC;
    echo "<br /><br />\n";
    $decalage = $date_browser->diff($date_now);
    // $timestamp_received = mktime ($hour, $minute, $second, $month, $day, $year);
    $offset = $decalage->format('%H:%I');
    $_SESSION['decalage'] = $offset;
    echo "\t" . '$_SESSION[\'decalage\'] = ' . "\t\t" . $_SESSION['decalage'];
    echo "<br /><br />\n";
    $tmp_offset = explode(':', $offset);
    $offset = (float)$tmp_offset[0] * 60 + $tmp_offset[1];
    echo "\t\t\t" . '$_SESSION[\'offset\'] = ' . $offset;
    echo "<br /><br />\n";
    $offsetALL_min = $offsetUS_min + $offset;
    $offsetALL_max = $offsetUS_max + $offset;
    echo "\t" . '$offsetGLOBAL_min = ' . "\t\t" . $offsetALL_min;
          
    echo "<br /><br />\n";
    echo "\t" . '$offsetGLOBAL_max = ' . "\t\t" . $offsetALL_max;
    echo "<br /><br />\n";
    if(($offsetUS_min <= $offset) && ($offsetUS_max >= $offset))
    {
        return false;
    }
    else
    {
        return true;
    }
}
$str  = "<!DOCTYPE html>\n";
$str .= "<html lang=\"fr\">\n";
$str .= "<head>\n";
$str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
$str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
$str .= "<head>\n";
$str .= "    <script>\n";
$str .= "\n";
$str .= "    window.newLocation = function( location ) {\n";
$str .= "        var newLocation = ( typeof location === 'string' ) ? location : window.location.href,\n";
$str .= "        appendType = ( newLocation.indexOf('?') < 0 ) ? '?' : '&';\n";
$str .= "        window.location = newLocation + appendType + '_t=' + (new Date()).getTime();\n";
$str .= "    }\n";
$str .= "\n";
$str .= "    function lien() {\n";
$str .= "        date_visitor = new Date();\n";
$str .= "        var year = date_visitor.getFullYear();\n";
$str .= "        var month = 1 + date_visitor.getMonth();\n";
$str .= "        var day = date_visitor.getDate();\n";
$str .= "        var hour = date_visitor.getHours();\n";
$str .= "        var minute = date_visitor.getMinutes();\n";
$str .= "        var second = date_visitor.getSeconds();\n";
$str .= "\n";
$str .= "        var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
$str .= "\n";
$str .= "        var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
$str .= "        window.newLocation(chainurl);\n";
$str .= "    }\n";
$str .= "    </script>\n";
$str .= "    <title>Titre</title>\n";
$str .= "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=iso-8859-1\" />\n";
$str .= "</head>\n";
echo $str;
if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
{
    echo "<body onLoad=lien()>\n";
    stop_the_session();
}
else if (!isset($_SESSION['decalage']))
{
    echo "<body>\n";
    if(isset($_GET['timestamp']))
    {
        $timestamp = (int)$_GET['timestamp'];
        echo "<br /><br />\n";
        echo "Tous les offsets sont en minutes.\n";
        echo "<br /><br />\n";
        if(IsAllowedUS($timestamp))
        {
            echo "You are not connecting from the US.";
        }
        else
        {
            echo "You are connecting from the US.";
        }
        echo "<br /><br />\n";
    }
    echo "</body>\n";
    echo "</html>\n";
    stop_the_session();
}
?>
 
WRInaute accro
Bonjour K0ogar

Une ébauche de fonction tout azimut :

( pas testé ).

A améliorer dans les détails.


PHP:
/**
 * Parcours
 * de tous les
 * offsets
 * de tous les
 * fuseaux horaires.
 **/
function ALL_offsets(array $param_zones)
{
    $array_offset = array();
    $array_zone_name = array();
    $array_zone_indice = array();
    $array_zones = array(
        DateTimeZone::AFRICA,
        DateTimeZone::AMERICA,
        DateTimeZone::ANTARCTICA,
        DateTimeZone::ARCTIC,
        DateTimeZone::ASIA,
        DateTimeZone::ATLANTIC,
        DateTimeZone::AUSTRALIA,
        DateTimeZone::EUROPE,
        DateTimeZone::INDIAN,
        DateTimeZone::PACIFIC /*
        DateTimeZone::UTC
        DateTimeZone::ALL
        DateTimeZone::ALL_WITH_BC
        DateTimeZone::PER_COUNTRY */);
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 5000000.0;
    $max = -5000000.0;
    foreach($array_zones as $zone)
    {
        if(in_array($zone, $param_zones))
        {
            unset($timeZones);
            $timeZones = DateTimeZone::listIdentifiers($zone);
            foreach ( $timeZones as $zoneName )
            {
                $tz = new DateTimeZone($zoneName);
                $transitions = $tz->getTransitions($theTime,$theTime);
                foreach($transitions as $key => $offset)
                {  
                    $tmp_offset    = (int)$offset['offset'];
                    $min = min($min, $tmp_offset);
                    $max = max($max, $tmp_offset);
                }
            }
        }
    }
    /**
    $array_offset_min =  explode(':', sprintf('%+03d:%02u', $min / 3600, abs($min) % 3600 / 60));
    $array_offset_max =  explode(':', sprintf('%+03d:%02u', $max / 3600, abs($max) % 3600 / 60));
    $offset_min = $array_offset_min[0] * 60 + $array_offset_min[1];
    $offset_max = $array_offset_max[0] * 60 + $array_offset_max[1];
    **/
    $offset_min = $min;
    $offset_max = $max;
    return array($offset_min, $offset_max);
}
/**
 
Dernière édition:
WRInaute accro
O Jeff !!
je te l'ai dit en MP, pas le temps de coder avant cet été :)
Un rapide coup d'oeil me dit que ca va etre du lourd quand le code sera au point!
Bannir la zone que l'on veut, idéal pour tous les sites mutualisés qui ne peuvent pas avoir matériellement l'ipgeo.
Faut mettre tous ces bouts codes ensembles now.
 
WRInaute accro
Bonjour K0ogar

Je peux aller jusqu'aux longitudes.

Les paramètres longitudes min et max définissent la zone à filtrer et çà roule ?

C'est ultra facile à faire.

Amicalement.
 
WRInaute accro
Je vous demande pardon

DateTimeZone ::getLocation() donne le pays, latitude et longitude.

Correct ?

2 paramètres longitude max et min, et çà roule ?

Merci.
 
WRInaute accro
WRInaute accro
Bonjour K0ogar

Celà donne le country du serveur pas celui du navigateur.

Si on veut filtrer suivant l'emplacement géographique du navigateur client, comme on sait quel panel de longitudes filtrer, le webmaster met ces longitudes min et max en paramètres dans la fonction, la fonction calcule les getlocation() et gettransitions() de time() , en déduit offset max et min, vérifie si l'offset navigateur est > min et < max, et en déduit le filtrage ?

Amicalement.
 
WRInaute accro
Voilà

Le décalage horaire client est transmis par la session ( decalage ) et le get ( $timestamp ).

La fonction ALL_offsets calcule par rapprochement entre getTransitions() et getLocations(), les offsets minimum et maximal correspondants aux longitudes paramètres.

La fonction function IsAllowed(int $timestamp, float $long_min, float $long_max) reçoit le timestamp client et les offsets limites, en déduit l'offset client et par comparaison le filtrage.

Pas testé.

Il faut amorcer la session ad libitum.

Amicalement.


PHP:
<?php
require("/var/www/html/php/config/tmp_config.php");
start_the_session(false, false, false);
/**
 * Longitudes min et max
 * en degrés.
 **/
function ALL_offsets(float $longitude_min, float $longitude_max )
{
    $array_zones = array(
        DateTimeZone::AFRICA, 
        DateTimeZone::AMERICA, 
        DateTimeZone::ANTARCTICA, 
        DateTimeZone::ARCTIC, 
        DateTimeZone::ASIA, 
        DateTimeZone::ATLANTIC, 
        DateTimeZone::AUSTRALIA, 
        DateTimeZone::EUROPE, 
        DateTimeZone::INDIAN, 
        DateTimeZone::PACIFIC);
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 5000000.0;
    $max = -5000000.0;
    /**
    * Parcours
    * de tous les
    * offsets
    * de tous les
    * fuseaux horaires.
    **/
    foreach($array_zones as $value)
    {
        unset($timeZones);
        $timeZones = DateTimeZone::listIdentifiers($value);
        foreach ( $timeZones as $zoneName )
        {
            $tz = new DateTimeZone($zoneName);
            $transitions = $tz->getTransitions($theTime,$theTime);
            $locations = $tz->getLocation();
            foreach($transitions as $offset)
            {   
                $tmp_offset    = (int)$offset['offset'];
                if(isset($locations['longitude']))
                {
                    $longitude = (float)$locations['longitude'];
                    if(($longitude >= $longitude_min)&&
                        ($longitude <= $longitude_max))
                    {
                        $min = min($min, $tmp_offset);
                        $max = max($max, $tmp_offset);
                    }
                }
                else
                {
                    foreach($locations as $location)
                    {
                        $longitude = (float)$location['longitude'];
                        if(($longitude >= $longitude_min)&&
                            ($longitude <= $longitude_max))
                        {
                            $min = min($min, $tmp_offset);
                            $max = max($max, $tmp_offset);
                        }
                    }
                }
            }
        }
    }
    $offset_min = $min;
    $offset_max = $max;
    return array($offset_min, $offset_max);
}
function IsAllowed(int $timestamp, float $long_min, float $long_max)
{
    //    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
    $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
    $timestamp %= (12 * 50 * 24 * 60 * 60);
    $month = floor($timestamp / (50 * 24 * 60 * 60));
    $timestamp %= (50 * 24 * 60 * 60);
    $day = floor($timestamp / (24 * 60 * 60));
    $timestamp %= (24 * 60 * 60);
    $hour = floor($timestamp / (60 * 60));
    $timestamp %= (60 * 60);
    $minute = floor($timestamp / 60);
    $second = $minute % 60;
    $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
    $date_browser = new DateTime($date_locale, new DateTimeZone('UTC'));
    $array_Offset = array();
    $array_Offset = ALL_offsets($long_min, $long_max);
    $offset_min = $array_Offset[0];
    $offset_max = $array_Offset[1];

    $_SESSION['Offset'] = serialize($array_Offset);

    $date_now = new DateTime("now", new DateTimeZone('UTC'));
    $decalage = $date_browser->diff($date_now);
    $offset = $decalage->format('%H:%I:%s');
    $_SESSION['decalage'] = $offset;
    $array_offset = explode(':', $offset);
    $offset = 3600 * $array_offset[0] + 60 * $array_offset[1] + $array_offset[2];

    if(($offset_min <= $offset) && ($offset_max >= $offset))
    {
        return false;
    }
    else
    {
        return true;
    }
}
$str  = "<!DOCTYPE html>\n";
$str .= "<html lang=\"fr\">\n";
$str .= "<head>\n";
$str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
$str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
$str .= "<head>\n";
$str .= "    <script>\n";
$str .= "\n";
$str .= "    window.newLocation = function( location ) {\n";
$str .= "        var newLocation = ( typeof location === 'string' ) ? location : window.location.href,\n";
$str .= "        appendType = ( newLocation.indexOf('?') < 0 ) ? '?' : '&';\n";
$str .= "        window.location = newLocation + appendType + '_t=' + (new Date()).getTime();\n";
$str .= "    }\n";
$str .= "\n";
$str .= "    function lien() {\n";
$str .= "        date_visitor = new Date();\n";
$str .= "        var year = date_visitor.getFullYear();\n";
$str .= "        var month = 1 + date_visitor.getMonth();\n";
$str .= "        var day = date_visitor.getDate();\n";
$str .= "        var hour = date_visitor.getHours();\n";
$str .= "        var minute = date_visitor.getMinutes();\n";
$str .= "        var second = date_visitor.getSeconds();\n";
$str .= "\n";
$str .= "        var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
$str .= "\n";
$str .= "        var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
$str .= "        window.newLocation(chainurl);\n";
$str .= "    }\n";
$str .= "    </script>\n";
$str .= "    <title>Titre</title>\n";
$str .= "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=iso-8859-1\" />\n";
$str .= "</head>\n";
echo $str;
if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
{
    echo "<body onLoad=lien()>\n";
    stop_the_session(false);
}
else if (!isset($_SESSION['decalage']))
{
    if(isset($_GET['timestamp']))
    {
        $timestamp = (int)$_GET['timestamp'];
        if(IsAllowed($timestamp, $long_min, $long_max))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    stop_the_session(false);
}
?>
 
WRInaute accro
Petite variante.

Théoriquement le méridien 0 est Greenwhich.

Le problème serait les offsets > 180°.

MIN_LONG et MAX_LONG des US sont mal calculés.

Bien amicalement.


PHP:
<?php
require("/var/www/html/php/config/tmp_config.php");
start_the_session(false, false, false);
/**
 * Longitudes min et max
 * en degrés.
 **/
function ALL_offsets(float $longitude_min, float $longitude_max )
{
    $array_zones = array(
        DateTimeZone::AFRICA, 
        DateTimeZone::AMERICA, 
        DateTimeZone::ANTARCTICA, 
        DateTimeZone::ARCTIC, 
        DateTimeZone::ASIA, 
        DateTimeZone::ATLANTIC, 
        DateTimeZone::AUSTRALIA, 
        DateTimeZone::EUROPE, 
        DateTimeZone::INDIAN, 
        DateTimeZone::PACIFIC);
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 5000000.0;
    $max = -5000000.0;
    /**
    * Parcours
    * de tous les
    * offsets
    * de tous les
    * fuseaux horaires.
    **/
    foreach($array_zones as $value)
    {
        unset($timeZones);
        $timeZones = DateTimeZone::listIdentifiers($value);
        foreach ( $timeZones as $zoneName )
        {
            $tz = new DateTimeZone($zoneName);
            $transitions = $tz->getTransitions($theTime,$theTime);
            $locations = $tz->getLocation();
            foreach($transitions as $offset)
            {   
                $tmp_offset    = (int)$offset['offset'];
                if(isset($locations['longitude']))
                {
                    $longitude = (float)$locations['longitude'];
                    if(($longitude >= $longitude_min)&&
                        ($longitude <= $longitude_max))
                    {
                        $min = min($min, $tmp_offset);
                        $max = max($max, $tmp_offset);
                    }
                }
                else
                {
                    foreach($locations as $location)
                    {
                        $longitude = (float)$location['longitude'];
                        if(($longitude >= $longitude_min)&&
                            ($longitude <= $longitude_max))
                        {
                            $min = min($min, $tmp_offset);
                            $max = max($max, $tmp_offset);
                        }
                    }
                }
            }
        }
    }
    $offset_min = $min;
    $offset_max = $max;
    return array($offset_min, $offset_max);
}
function IsAllowed(int $timestamp, float $long_min, float $long_max)
{
    //    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
    $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
    $timestamp %= (12 * 50 * 24 * 60 * 60);
    $month = floor($timestamp / (50 * 24 * 60 * 60));
    $timestamp %= (50 * 24 * 60 * 60);
    $day = floor($timestamp / (24 * 60 * 60));
    $timestamp %= (24 * 60 * 60);
    $hour = floor($timestamp / (60 * 60));
    $timestamp %= (60 * 60);
    $minute = floor($timestamp / 60);
    $second = $minute % 60;
    $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
    $date_browser = new DateTime($date_locale, new DateTimeZone('UTC'));
    $array_Offset = array();
    $array_Offset = ALL_offsets($long_min, $long_max);
    $offset_min = $array_Offset[0];
    $offset_max = $array_Offset[1];
    $_SESSION['offset'] = serialize($array_Offset);
    $date_now = new DateTime("now", new DateTimeZone('UTC'));
    $decalage = $date_browser->diff($date_now);
    $offset = $decalage->format('%H:%I:%s');
    $_SESSION['decalage'] = $offset;
    $array_offset = explode(':', $offset);
    $offset = 3600 * $array_offset[0] + 60 * $array_offset[1] + $array_offset[2];
    $offsetALL_min = $offset_min + $offset;
    $offsetALL_max = $offset_max + $offset;
    if(($offsetALL_min <= $offset) && ($offsetALL_max >= $offset))
    {
        return false;
    }
    else
    {
        return true;
    }
}
function filtrage($title, $min_long, $max_long)
{
    if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
    {
        $str  = "<!DOCTYPE html>\n";
        $str .= "<html lang=\"fr\">\n";
        $str .= "<head>\n";
        $str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
        $str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
        $str .= "<script>\n";
        $str .= "\n";
        $str .= "    window.newLocation = function( location ) {\n";
        $str .= "        var newLocation = ( typeof location === 'string' ) ? location : window.location.href,\n";
        $str .= "        appendType = ( newLocation.indexOf('?') < 0 ) ? '?' : '&';\n";
        $str .= "        window.location = newLocation + appendType + '_t=' + (new Date()).getTime();\n";
        $str .= "    }\n";
        $str .= "\n";
        $str .= "function lien() {\n";
        $str .= "    date_visitor = new Date();\n";
        $str .= "    var year = date_visitor.getFullYear();\n";
        $str .= "    var month = 1 + date_visitor.getMonth();\n";
        $str .= "    var day = date_visitor.getDate();\n";
        $str .= "    var hour = date_visitor.getHours();\n";
        $str .= "    var minute = date_visitor.getMinutes();\n";
        $str .= "    var second = date_visitor.getSeconds();\n";
        $str .= "\n";
        $str .= "    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
        $str .= "\n";
        $str .= "    var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
        $str .= "    window.newLocation(chainurl);\n";
        $str .= "}\n";
        $str .= "</script>\n";
        $str .= "<title>" . $title . "</title>\n";
        $str .= "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n";
        $str .= "</head>\n";
        echo $str;
        echo "<body onLoad=lien()>\n";
        return null;
    }
    else if (!isset($_SESSION['decalage']))
    {
        if(isset($_GET['timestamp']))
        {
            $timestamp = (int)$_GET['timestamp'];
            if(IsAllowed($timestamp, $min_long, $max_long))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
/**
 * Russie => Lavry( 27° 21' E )
 **/
define('MIN_LONG', +27.0);
/**
 * Russie => Cap Dejnev ( 169° 39' 07" O )
 **/
define('MAX_LONG', +191.0);
/**
 * USA => Maine. ( 66° 55' O )
 **/
define('MAX_LONG', 113.0);
/*
 * USA => Washington. ( 124° 44' O )
 **/
define('MIN_LONG', 145.0);

$res = filtrage($title, MIN_LONG, MAX_LONG);
if($res === true)
{
    echo "You are not allowed to connect.<br >\n";
}
elseif($res === false)
{
    echo "You are allowed to connect.<br >\n";
}
stop_the_session(false);
?>
 
WRInaute accro
Voilà, voilà

Correct théoriquement.

Testez si vous voulez au : https://www.pronostics-courses.fr/Time.php

C'est réglé pour les longitudes Françaises, et il semble que les résultats varient et sont souvent faux.

Vous pouvez tester pour la Russie ou les US.

J'ai piqué les longitudes sur Wikipedia.

Amicalement.


PHP:
<?php
require("/var/www/html/php/config/tmp_config.php");
start_the_session(false, false, false);
/**
 * Longitudes min et max
 * en degrés.
 **/
function ALL_offsets(float $longitude_min, float $longitude_max )
{
    $array_zones = array(
        DateTimeZone::AFRICA, 
        DateTimeZone::AMERICA, 
        DateTimeZone::ANTARCTICA, 
        DateTimeZone::ARCTIC,
        DateTimeZone::ASIA, 
        DateTimeZone::ATLANTIC, 
        DateTimeZone::AUSTRALIA, 
        DateTimeZone::EUROPE, 
        DateTimeZone::INDIAN, 
        DateTimeZone::PACIFIC);
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 5000000.0;
    $max = -5000000.0;
    /**
    * Parcours
    * de tous les
    * offsets
    * de tous les
    * fuseaux horaires.
    **/
    foreach($array_zones as $value)
    {
        unset($timeZones);
        $timeZones = DateTimeZone::listIdentifiers($value);
        foreach ( $timeZones as $zoneName )
        {
            $tz = new DateTimeZone($zoneName);
            $transitions = $tz->getTransitions($theTime,$theTime);
            $locations = $tz->getLocation();
            foreach($transitions as $offset)
            {   
                $tmp_offset    = (int)$offset['offset'];
                if(isset($locations['longitude']))
                {
                    $longitude = (float)$locations['longitude'];
                    if(($longitude >= $longitude_min)&&
                        ($longitude <= $longitude_max))
                    {
                        $min = min($min, $tmp_offset);
                        $max = max($max, $tmp_offset);
                    }
                }
                else
                {
                    foreach($locations as $location)
                    {
                        $longitude = (float)$location['longitude'];
                        if(($longitude >= $longitude_min)&&
                            ($longitude <= $longitude_max))
                        {
                            $min = min($min, $tmp_offset);
                            $max = max($max, $tmp_offset);
                        }
                    }
                }
            }
        }
    }
    $offset_min = $min;
    $offset_max = $max;
    return array($offset_min, $offset_max);
}
function IsAllowed(int $timestamp, float $long_min, float $long_max)
{
    //    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
    $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
    $timestamp %= (12 * 50 * 24 * 60 * 60);
    $month = floor($timestamp / (50 * 24 * 60 * 60));
    $timestamp %= (50 * 24 * 60 * 60);
    $day = floor($timestamp / (24 * 60 * 60));
    $timestamp %= (24 * 60 * 60);
    $hour = floor($timestamp / (60 * 60));
    $timestamp %= (60 * 60);
    $minute = floor($timestamp / 60);
    $second = $minute % 60;
    $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
    $date_browser = new DateTime($date_locale, new DateTimeZone('UTC'));
    $array_Offset = array();
    $array_Offset = ALL_offsets($long_min, $long_max);
    $offset_min = $array_Offset[0];
    $offset_max = $array_Offset[1];
    $_SESSION['offset'] = serialize($array_Offset);
    $date_now = new DateTime("now", new DateTimeZone('UTC'));
    $decalage = $date_browser->diff($date_now);
    $offset = $decalage->format('%H:%I:%s');
    $_SESSION['decalage'] = $offset;
    echo "\t\t" . '$offset_min = ' . $offset_min . "<br />\n";
    echo "\t\t" . '$offset_max = ' . $offset_max . "<br />\n";
    echo "\t\t" . '$offset = ' . $offset . "<br />\n";
    $array_offset = explode(':', $offset);
    $offset = 3600 * $array_offset[0] + 60 * $array_offset[1] + $array_offset[2];
    echo "\t\t" . '$offset = ' . $offset . "<br />\n";
    if(($offset_min <= $offset) && ($offset_max >= $offset))
    {
        return false;
    }
    else
    {
        return true;
    }
}
function filtrage($title, $min_long, $max_long)
{
    if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
    {
        $str  = "<!DOCTYPE html>\n";
        $str .= "<html lang=\"fr\">\n";
        $str .= "<head>\n";
        $str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
        $str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
        $str .= "<script>\n";
        $str .= "\n";
        $str .= "    window.newLocation = function( location ) {\n";
        $str .= "        var newLocation = ( typeof location === 'string' ) ? location : window.location.href,\n";
        $str .= "        appendType = ( newLocation.indexOf('?') < 0 ) ? '?' : '&';\n";
        $str .= "        window.location = newLocation + appendType + '_t=' + (new Date()).getTime();\n";
        $str .= "    }\n";
        $str .= "\n";
        $str .= "function lien() {\n";
        $str .= "    date_visitor = new Date();\n";
        $str .= "    var year = date_visitor.getFullYear();\n";
        $str .= "    var month = 1 + date_visitor.getMonth();\n";
        $str .= "    var day = date_visitor.getDate();\n";
        $str .= "    var hour = date_visitor.getHours();\n";
        $str .= "    var minute = date_visitor.getMinutes();\n";
        $str .= "    var second = date_visitor.getSeconds();\n";
        $str .= "\n";
        $str .= "    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
        $str .= "\n";
        $str .= "    var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
        $str .= "    window.newLocation(chainurl);\n";
        $str .= "}\n";
        $str .= "</script>\n";
        $str .= "<title>" . $title . "</title>\n";
        $str .= "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n";
        $str .= "</head>\n";
        echo $str;
        echo "<body onLoad=lien()>\n";
        return null;
    }
    else if (!isset($_SESSION['decalage']))
    {
        if(isset($_GET['timestamp']))
        {
            $timestamp = (int)$_GET['timestamp'];
            if(IsAllowed($timestamp, $min_long, $max_long))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
/**
 * Examples
 * Décalages horaires.
 **/
/**
 * Russie => Lavry( 27° 21' E )
 **/
// define('MIN_LONG', 27.35);
/**
 * Russie => Cap Dejnev ( 169° 39' 07" O )
 **/
// define('MAX_LONG', -168.0);
/*
 * USA => Washington. ( 124° 44' O )
 **/
// define('MIN_LONG', -125.0);
/**
 * USA => Maine. ( 66° 55' O )
 **/
// define('MAX_LONG', -66.0);
/**
 * France => pointe de Corsen ( 4° 47' 44" O )
 **/
define('MIN_LONG', -4.8);
/**
 * France => Lauterbourg ( 8° 13' 50" E )
 **/
define('MAX_LONG', 8.25);
$title = "Essai";
$res = filtrage($title, MIN_LONG, MAX_LONG);
if($res === true)
{
    echo "You are allowed to connect.<br >\n";
}
elseif($res === false)
{
    echo "You are not allowed to connect.<br >\n";
}
stop_the_session(false);
?>
 
WRInaute accro
Bonjour

Voici ce que j'ai :


$offset_min = 0
$offset_max = 3600
$offset = 01:00:24
$offset = 3624
You are allowed to connect.


Compte tenu des légers retards aléatoires dans les calculs de dates, il faudrait que j'élargisse légèrement aussi, le critère arithmétique true/false de IsAllowed().

Le calcul est approximatif.

A plus tard.

Amicalement.
 
WRInaute accro
Bonjour

Avec un offset approximatif de 100 secondes.

J'obtiens :

$offset_min = -100
$offset_max = 3700
$offset = 01:00:10
$offset = 3610
You are not allowed to connect.

C'est réglé pour filtrer les accès à partir de France.

L'idéal si possible, serait de calculer toutes les dates en même temps, ainsi il n'y aurait plus de retards induisant des erreurs de calcul d'offsets.

Dans ces conditions, il serait possible de se passer d'offset approximatif.

Testez si vous voulez au : https://www.pronostics-courses.fr/Time.php

Merci de votre aide.

Amicalement.


Code:
<?php
require("/var/www/html/php/config/tmp_config.php");
start_the_session(false, false, false);
/**
 * Offset d'approximation
 * de critère
 * d'offset,
 * pour la fonction
 * IsAllowed(),
 * fixé à 100 secondes.
 **/
define('ADD_OFFSET', 100);
/**
 * Examples
 * Décalages horaires,
 * paramètres de
 * la fonction filtrage().
 **/
/**
 * Russie => Lavry( 27° 21' E )
 **/
// define('MIN_LONG', 27.35);
/**
 * Russie => Cap Dejnev ( 169° 39' 07" O )
 **/
// define('MAX_LONG', -168.0);
/*
 * USA => Washington. ( 124° 44' O )
 **/
// define('MIN_LONG', -125.0);
/**
 * USA => Maine. ( 66° 55' O )
 **/
// define('MAX_LONG', -66.0);
/**
 * France => pointe de Corsen ( 4° 47' 44" O )
 **/
define('MIN_LONG', -4.8);
/**
 * France => Lauterbourg ( 8° 13' 50" E )
 **/
define('MAX_LONG', 8.25);
/**
 * Longitudes min et max
 * en degrés.
 **/
function ALL_offsets(float $longitude_min, float $longitude_max )
{
    $array_zones = array(
        DateTimeZone::AFRICA,
        DateTimeZone::AMERICA,
        DateTimeZone::ANTARCTICA,
        DateTimeZone::ARCTIC,
        DateTimeZone::ASIA,
        DateTimeZone::ATLANTIC,
        DateTimeZone::AUSTRALIA,
        DateTimeZone::EUROPE,
        DateTimeZone::INDIAN,
        DateTimeZone::PACIFIC);
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 5000000.0;
    $max = -5000000.0;
    /**
    * Parcours
    * de tous les
    * offsets
    * de tous les
    * fuseaux horaires.
    **/
    foreach($array_zones as $value)
    {
        unset($timeZones);
        $timeZones = DateTimeZone::listIdentifiers($value);
        foreach ( $timeZones as $zoneName )
        {
            $tz = new DateTimeZone($zoneName);
            $transitions = $tz->getTransitions($theTime,$theTime);
            $locations = $tz->getLocation();
            foreach($transitions as $offset)
            {  
                $tmp_offset    = (int)$offset['offset'];
                if(isset($locations['longitude']))
                {
                    $longitude = (float)$locations['longitude'];
                    if(($longitude >= $longitude_min)&&
                        ($longitude <= $longitude_max))
                    {
                        $min = min($min, $tmp_offset);
                        $max = max($max, $tmp_offset);
                    }
                }
                else
                {
                    foreach($locations as $location)
                    {
                        $longitude = (float)$location['longitude'];
                        if(($longitude >= $longitude_min)&&
                            ($longitude <= $longitude_max))
                        {
                            $min = min($min, $tmp_offset);
                            $max = max($max, $tmp_offset);
                        }
                    }
                }
            }
        }
    }
    $offset_min = $min;
    $offset_max = $max;
    return array($offset_min, $offset_max);
}
function IsAllowed(int $timestamp, float $long_min, float $long_max)
{
    //    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
    $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
    $timestamp %= (12 * 50 * 24 * 60 * 60);
    $month = floor($timestamp / (50 * 24 * 60 * 60));
    $timestamp %= (50 * 24 * 60 * 60);
    $day = floor($timestamp / (24 * 60 * 60));
    $timestamp %= (24 * 60 * 60);
    $hour = floor($timestamp / (60 * 60));
    $timestamp %= (60 * 60);
    $minute = floor($timestamp / 60);
    $second = $minute % 60;
    $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
    $date_browser = new DateTime($date_locale, new DateTimeZone('UTC'));
    $array_Offset = array();
    $array_Offset = ALL_offsets($long_min, $long_max);
    $offset_min = (int)$array_Offset[0] - ADD_OFFSET;
    $offset_max = (int)$array_Offset[1] + ADD_OFFSET;
    $_SESSION['offset'] = serialize($array_Offset);
    $date_now = new DateTime("now", new DateTimeZone('UTC'));
    $decalage = $date_browser->diff($date_now);
    $offset = $decalage->format('%H:%I:%s');
    $_SESSION['decalage'] = $offset;
    echo "\t\t" . '$offset_min = ' . $offset_min . "<br />\n";
    echo "\t\t" . '$offset_max = ' . $offset_max . "<br />\n";
    echo "\t\t" . '$offset = ' . $offset . "<br />\n";
    $array_offset = explode(':', $offset);
    $offset = 3600 * $array_offset[0] + 60 * $array_offset[1] + $array_offset[2];
    echo "\t\t" . '$offset = ' . $offset . "<br />\n";
    if(($offset_min <= $offset) && ($offset_max >= $offset))
    {
        return false;
    }
    else
    {
        return true;
    }
}
function filtrage($title, $min_long, $max_long)
{
    if(!isset($_SESSION['decalage']) and !isset($_GET['timestamp']))
    {
        $str  = "<!DOCTYPE html>\n";
        $str .= "<html lang=\"fr\">\n";
        $str .= "<head>\n";
        $str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
        $str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
        $str .= "<script>\n";
        $str .= "\n";
        $str .= "    window.newLocation = function( location ) {\n";
        $str .= "        var newLocation = ( typeof location === 'string' ) ? location : window.location.href,\n";
        $str .= "        appendType = ( newLocation.indexOf('?') < 0 ) ? '?' : '&';\n";
        $str .= "        window.location = newLocation + appendType + '_t=' + (new Date()).getTime();\n";
        $str .= "    }\n";
        $str .= "\n";
        $str .= "function lien() {\n";
        $str .= "    date_visitor = new Date();\n";
        $str .= "    var year = date_visitor.getFullYear();\n";
        $str .= "    var month = 1 + date_visitor.getMonth();\n";
        $str .= "    var day = date_visitor.getDate();\n";
        $str .= "    var hour = date_visitor.getHours();\n";
        $str .= "    var minute = date_visitor.getMinutes();\n";
        $str .= "    var second = date_visitor.getSeconds();\n";
        $str .= "\n";
        $str .= "    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
        $str .= "\n";
        $str .= "    var  chainurl='" . $_SERVER['PHP_SELF'] . "?timestamp=' + timestamp;\n";
        $str .= "    window.newLocation(chainurl);\n";
        $str .= "}\n";
        $str .= "</script>\n";
        $str .= "<title>" . $title . "</title>\n";
        $str .= "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n";
        $str .= "</head>\n";
        echo $str;
        echo "<body onLoad=lien()>\n";
        return null;
    }
    else if (!isset($_SESSION['decalage']))
    {
        if(isset($_GET['timestamp']))
        {
            $timestamp = (int)$_GET['timestamp'];
            if(IsAllowed($timestamp, $min_long, $max_long))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
$title = "Essai";
$res = filtrage($title, MIN_LONG, MAX_LONG);
if($res === true)
{
    echo "You are allowed to connect.<br >\n";
}
elseif($res === false)
{
    echo "You are not allowed to connect.<br >\n";
}
stop_the_session(false);
?>
 
WRInaute accro
Bonjour

J'y ai mis avec la rdirection en _POST.

K0Ogar, comment tu gères les variables transmises en GET ou POST ?

Je suis en train de modifier mon site pour gérer les variables transmises par PHP.

Sinon, c'est impossible en session ou cookie ?

Ci-dessous 2 fichiers :

Time.php à inclure au début.

Filtrage.php amorçant le filtrage.

Merci beaucoup de ton aide.

Amicalement.


PHP:
<?php
/**
 * Time.php
 **/
global $web_url;
/**
 * Offset d'approximation
 * de critère
 * d'offset,
 * pour la fonction
 * IsAllowed(),
 * fixé à 100 secondes.
 **/
define('ADD_OFFSET', 100);
/**
 * Examples
 * Décalages horaires,
 * paramètres de
 * la fonction filtrage().
 **/
/**
 * Russie => Lavry( 27° 21' E )
 **/
// define('MIN_LONG', 27.35);
/**
 * Russie => Cap Dejnev ( 169° 39' 07" O )
 **/
// define('MAX_LONG', -168.0);
/*
 * USA => Washington. ( 124° 44' O )
 **/
// define('MIN_LONG', -125.0);
/**
 * USA => Maine. ( 66° 55' O )
 **/
// define('MAX_LONG', -66.0);
/**
 * France => pointe de Corsen ( 4° 47' 44" O )
 **/
// define('MIN_LONG', -4.8);
/**
 * France => Lauterbourg ( 8° 13' 50" E )
 **/
// define('MAX_LONG', 8.25);
/**
 * Longitudes min et max
 * en degrés.
 **/
function ALL_offsets(float $longitude_min, float $longitude_max )
{
    $array_zones = array(
        DateTimeZone::AFRICA, 
        DateTimeZone::AMERICA, 
        DateTimeZone::ANTARCTICA, 
        DateTimeZone::ARCTIC,
        DateTimeZone::ASIA, 
        DateTimeZone::ATLANTIC, 
        DateTimeZone::AUSTRALIA, 
        DateTimeZone::EUROPE, 
        DateTimeZone::INDIAN, 
        DateTimeZone::PACIFIC);
    $theTime = time(); # specific date/time we're checking, in epoch seconds.
    $min = 5000000.0;
    $max = -5000000.0;
    /**
    * Parcours
    * de tous les
    * offsets
    * de tous les
    * fuseaux horaires.
    **/
    foreach($array_zones as $value)
    {
        unset($timeZones);
        $timeZones = DateTimeZone::listIdentifiers($value);
        foreach ( $timeZones as $zoneName )
        {
            $tz = new DateTimeZone($zoneName);
            $transitions = $tz->getTransitions($theTime,$theTime);
            $locations = $tz->getLocation();
            foreach($transitions as $offset)
            {   
                $tmp_offset    = (int)$offset['offset'];
                if(isset($locations['longitude']))
                {
                    $longitude = (float)$locations['longitude'];
                    if(($longitude >= $longitude_min)&&
                        ($longitude <= $longitude_max))
                    {
                        $min = min($min, $tmp_offset);
                        $max = max($max, $tmp_offset);
                    }
                }
                else
                {
                    foreach($locations as $location)
                    {
                        $longitude = (float)$location['longitude'];
                        if(($longitude >= $longitude_min)&&
                            ($longitude <= $longitude_max))
                        {
                            $min = min($min, $tmp_offset);
                            $max = max($max, $tmp_offset);
                        }
                    }
                }
            }
        }
    }
    $offset_min = $min;
    $offset_max = $max;
    return array($offset_min, $offset_max);
}
function IsAllowed(int $timestamp, float $long_min, float $long_max)
{
    $date_now = new DateTime("now", new DateTimeZone('UTC'));
    //    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
//    $year = floor($timestamp / (12 * 50 * 24 * 60 * 60));
    $year = floor($timestamp / 51840000);
//    $timestamp %= (12 * 50 * 24 * 60 * 60);
    $timestamp %= 51840000;
//    $month = floor($timestamp / (50 * 24 * 60 * 60));
    $month = floor($timestamp / 4320000);
//    $timestamp %= (50 * 24 * 60 * 60);
    $timestamp %= 4320000;
//    $day = floor($timestamp / (24 * 60 * 60));
    $day = floor($timestamp / 86400);
//    $timestamp %= (24 * 60 * 60);
    $timestamp %= 86400;
//    $hour = floor($timestamp / (60 * 60));
    $hour = floor($timestamp / 3600);
//    $timestamp %= (60 * 60);
    $timestamp %= 3600;
    $minute = floor($timestamp / 60);
    $second = $minute % 60;
    $date_locale = $year . "-" . substr("00" . $month, -2) . "-" . substr("00" . $day, -2) . " " . substr("00". $hour, -2) . ":" . substr("0" . $minute, -2) . ":" . substr("00" . $second,- 2);
//    echo "\t\t" . '$date_locale = ' . $date_locale . "<br />\n";
    $date_browser = new DateTime($date_locale, new DateTimeZone('UTC'));
    $array_Offset = array();
    $array_Offset = ALL_offsets($long_min, $long_max);
    $offset_min = (int)$array_Offset[0] - ADD_OFFSET;
    $offset_max = (int)$array_Offset[1] + ADD_OFFSET;
    $_SESSION['offset'] = serialize($array_Offset);
    $decalage = $date_browser->diff($date_now);
    $offset = $decalage->format('%H:%I:%s');
    $_SESSION['decalage'] = $offset;
    /**
    echo "\t\t" . '$offset_min = ' . $offset_min . "<br />\n";
    echo "\t\t" . '$offset_max = ' . $offset_max . "<br />\n";
    echo "\t\t" . '$offset = ' . $offset . "<br />\n";
    **/
    $array_offset = explode(':', $offset);
    $offset = 3600 * $array_offset[0] + 60 * $array_offset[1] + $array_offset[2];
//    echo "\t\t" . '$offset = ' . $offset . "<br />\n";
    if(($offset_min <= $offset) && ($offset_max >= $offset))
    {
        return false;
    }
    else
    {
        return true;
    }
}
function filtrage($title, $min_long, $max_long)
{
    global $web_url;
    if(!isset($_SESSION['decalage']) and !isset($_POST['timestamp']))
    {
        $str  = "<!DOCTYPE html>\n";
        $str .= "<html lang=\"fr\">\n";
        $str .= "<head>\n";
        $str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
        $str .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
        $str .= "<script>\n";
        $str .= "\n";
        $str .= "    window.newLocation = function( location ) {\n";
        $str .= "        var newLocation = ( typeof location === 'string' ) ? location : window.location.href,\n";
        $str .= "        appendType = ( newLocation.indexOf('?') < 0 ) ? '?' : '&';\n";
        $str .= "        window.location = newLocation + appendType + '_t=' + (new Date()).getTime();\n";
        $str .= "    }\n";
        $str .= "\n";
        $str .= "function postForm(path, params, method) {\n";
        $str .= "    method = method || 'post';\n";
        $str .= "\n";
        $str .= "    var form = document.createElement('form');\n";
        $str .= "    form.setAttribute('method', method);\n";
        $str .= "    form.setAttribute('action', path);\n";
        $str .= "\n";
        $str .= "    for (var key in params) {\n";
        $str .= "        if (params.hasOwnProperty(key)) {\n";
        $str .= "            var hiddenField = document.createElement('input');\n";
        $str .= "            hiddenField.setAttribute('type', 'hidden');\n";
        $str .= "            hiddenField.setAttribute('name', key);\n";
        $str .= "            hiddenField.setAttribute('value', params[key]);\n";
        $str .= "\n";
        $str .= "            form.appendChild(hiddenField);\n";
        $str .= "        }\n";
        $str .= "    }\n";
        $str .= "\n";
        $str .= "    document.body.appendChild(form);\n";
        $str .= "    form.submit();\n";
        $str .= "}\n";
        $str .= "\n";
        /**
        * La fonction lien()
        * rend dans timestamp
        * la date locale
        * du browser.
        **/
        $str .= "function lien() {\n";
        /**
        * Date UTC browser.
        **/
        $str .= "    date_visitor = new Date();\n";
        $str .= "    var year = date_visitor.getFullYear();\n";
        $str .= "    var month = 1 + date_visitor.getMonth();\n";
        $str .= "    var day = date_visitor.getDate();\n";
        $str .= "    var hour = date_visitor.getHours();\n";
        $str .= "    var minute = date_visitor.getMinutes();\n";
        $str .= "    var second = date_visitor.getSeconds();\n";
        $str .= "\n";
        $str .= "    var timestamp = 12 * 50 * 24 * 60 * 60 * year + 50 * 24 * 60 * 60 * month + 24 * 60 * 60 * day + 60 * 60 * hour + 60 * minute + second;\n";
        $str .= "\n";
        $str .= "postForm('" . $web_url . $_SERVER['PHP_SELF'] . "', {timestamp: timestamp});\n";
        $str .= "}\n";
        $str .= "</script>\n";
        $str .= "<title>" . $title . "</title>\n";
        $str .= "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n";
        $str .= "</head>\n";
        echo $str;
        echo "<body onLoad=lien()>\n";
        return null;
    }
    else if (!isset($_SESSION['decalage']))
    {
        if(isset($_POST['timestamp']))
        {
            $timestamp = (int)$_POST['timestamp'];
            if(IsAllowed($timestamp, $min_long, $max_long))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

?>
  
<?php
/**
  * Filtrage.php
 **/
/**
 * Examples
 * Décalages horaires,
 * paramètres de
 * la fonction filtrage().
 **/
/**
 * Russie => Lavry( 27° 21' E )
 **/
// define('MIN_LONG', 27.35);
/**
 * Russie => Cap Dejnev ( 169° 39' 07" O )
 **/
// define('MAX_LONG', -168.0);
/*
 * USA => Washington. ( 124° 44' O )
 **/
define('MIN_LONG', -125.0);
/**
 * USA => Maine. ( 66° 55' O )
 **/
define('MAX_LONG', -66.0);
/**
 * France => pointe de Corsen ( 4° 47' 44" O )
 **/
// define('MIN_LONG', -4.8);
/**
 * France => Lauterbourg ( 8° 13' 50" E )
 **/
// define('MAX_LONG', 8.25);
$res = filtrage($title, MIN_LONG, MAX_LONG);
if($res === null)
{
    stop_the_session(false);
}
elseif($res === false)
{
    echo "You are not allowed to connect.<br >\n";
    stop_the_session(true);
    exit;
}
?>
 
WRInaute accro
Beurk

Ave la redirection $_POST c'est trop long à se charger.

Comment calculer le décalage horaire à partir du browser sans faire d'ajax ?

D'après l'IP ou son reverse ?

Il me suffirait d'identifier les us.

Merci beaucoup de votre aide.

Amicalement.
 
WRInaute accro
Bonjour K0ogar

Je vais suivre ton conseil avec GeoIP.

Pour les US ( ou autres lieux non filtrés ), faut-il demander la permission des visiteurs avant de calculer leur pays ?

Merci beaucoup.

Amicalement.


PHP:
<?php

 echo geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);

?>
 
WRInaute accro
Bon, bon...

C'est fait.

Les US n'accèdent plus à mon site.

Code ( facile ) :

PHP:
<?php
if(function_exists("geoip_country_code_by_name")&&
    (geoip_country_code_by_name($_SERVER['REMOTE_ADDR']) == 'US'))
{
    echo "<br />\n";
    echo "<br />\n";
    echo "You are not allowed to connect.";
    echo "<br />\n";
    echo "<br />\n";
    exit;
}
?>

Mais que dire des GoogleBots ?

Ce filtrage est-il permis par la CNIL/GDPR ou le CPPA ?

Merci beaucoup de vos réponses.

Amicalement.
 
Discussions similaires
Haut