Semak sama ada nilai Sec-GPC wujud dalam pengepala permintaan HTTP dalam WordPress
P粉930534280
P粉930534280 2023-08-31 13:34:53
0
1
585
<p>如何检测 HTTP 请求标头中是否存在“Sec-GPC:1”?</p> <pre class="brush:php;toolbar:false;"><?Fungsi PHP detect_gpc_signal() { if (isset($_GET['Sec-Gpc'])) { gema 'isyarat GPC dikesan dalam data GET'; } elseif (isset($_POST['Sec-Gpc'])) { gema 'isyarat GPC dikesan dalam data POST'; } elseif (isset($_COOKIE['Sec-Gpc'])) { gema 'isyarat GPC dikesan dalam data COOKIE'; } elseif (isset($_SERVER['Sec-Gpc'])) { gema 'isyarat GPC dikesan dalam data SERVER'; } lain { echo 'Tiada isyarat GPC dikesan'; } } add_action('wp_footer', 'detect_gpc_signal'); ?></pra></p>
P粉930534280
P粉930534280

membalas semua(1)
P粉311464935

Terima kasih kerana mencadangkan getallheaders(). Dengan bantuan anda saya menemui dua cara, dengan dan tanpa Javascript.

/* Add GPC Banner to WP Header */
function display_gpc_signal_banner() {
echo '<section id="headerBannerGpc" class="bg-color"><div 
class="container detect-gpc-signal py-1 small"><span class="alert- 
circle"></span> Global Privacy Signal Detected</div></section>';
}
add_action('wp_head', 'display_gpc_signal_banner');

// Detect GPC Signal with Javascript
function detect_gpc_signal_js() {
?>
  <script>
  if(navigator.globalPrivacyControl == true ) {
    // Add a class to the body tag -- This makes the GPC banner visible
    document.body.classList.add('gpc-signal-detected');
    document.getElementById('headerBannerGpc').style.display = "block";
  } else {
    // Add a class to body tag declaring the GPC signal was not detected
    document.body.classList.add('no-gpc-signal-detected');
    document.getElementById('headerBannerGpc').style.display = "none";
  }
  </script>
  <?
}
add_action('wp_footer', 'detect_gpc_signal_js');

// Add a unique class to the body tag when GPC signal is detected
function detect_gpc_signal_body_class( $classes ) {
$headers = getallheaders();
if($headers['sec-gpc'] == 1) {
  $classes[] = 'gpc-signal-detected-no-js';
} else {
  $classes[] = 'no-gpc-signal-detected-no-js';
}
return $classes;
}
add_filter( 'body_class', 'detect_gpc_signal_body_class' );

Kemas kini

Fungsi asal akan berfungsi apabila HTTP_ ditambahkan pada nilai.

if (isset($_GET['HTTP_SEC_GPC'])) {
  echo 'GPC signal detected in GET data';
  } elseif (isset($_POST['HTTP_SEC_GPC'])) {
  echo 'GPC signal detected in POST data';
  } elseif (isset($_COOKIE['HTTP_SEC_GPC'])) {
  echo 'GPC signal detected in COOKIE data';
  } elseif (isset($_SERVER['HTTP_SEC_GPC'])) {
  echo 'GPC signal detected in SERVER data';
  } else {
   echo 'No GPC signal detected';
  }
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan