84669 人學習
152542 人學習
20005 人學習
5487 人學習
7821 人學習
359900 人學習
3350 人學習
180660 人學習
48569 人學習
18603 人學習
40936 人學習
1549 人學習
1183 人學習
32909 人學習
如何偵測HTTP請求標頭中是否存在「Sec-GPC:1」?
1 0 0 P粉930534280 全部回覆(1) 我來回復 關閉 P粉3114649352023-09-01 11:03:53 1樓 感謝您建議 getallheaders()。在你的幫助下,我找到了兩種方法,使用和不使用 Javascript。 /* Add GPC Banner to WP Header */ function display_gpc_signal_banner() { echo ' Global Privacy Signal Detected'; } add_action('wp_head', 'display_gpc_signal_banner'); // Detect GPC Signal with Javascript function detect_gpc_signal_js() { ?> } 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' ); 更新 當 HTTP_ 新增至值時,原始函數將會起作用。 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'; } 點贊 +0 新增回覆 关闭回复 P粉930534280 回覆
感謝您建議 getallheaders()。在你的幫助下,我找到了兩種方法,使用和不使用 Javascript。
/* Add GPC Banner to WP Header */ function display_gpc_signal_banner() { echo ' Global Privacy Signal Detected'; } add_action('wp_head', 'display_gpc_signal_banner'); // Detect GPC Signal with Javascript function detect_gpc_signal_js() { ?> } 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' );
更新
當 HTTP_ 新增至值時,原始函數將會起作用。
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'; }
感謝您建議 getallheaders()。在你的幫助下,我找到了兩種方法,使用和不使用 Javascript。
更新
當 HTTP_ 新增至值時,原始函數將會起作用。