簡體   English   中英

如何使用Javascript和PHP驗證引薦來源網址

[英]How to validate referrer url using Javascript and PHP

每當引薦來源網址網址等於鏈接時,我都希望重新加載頁面,但我想使用像操作符一樣的方式,因為級別ID並不總是相同,所以我不想使用等於。 請幫助我提供正確的代碼。

<?php
    echo '<script type="text/javascript"> 
    if (document.referrer = https://www.maocular.org/membership-account/membership-confirmation/?level=10)  { 
       location.reload(forceGet);
    }
    </script>';
?>

您可以使用String(javascript)的match函數,該函數以String或Regex作為輸入,因此顧名思義,match會對傳遞給它的字符串進行檢查,如果找到匹配項,則match函數返回一個Array,如果沒有找到匹配項找到,然后匹配返回null。

您的代碼的解決方案:

if(document.referer.match('https://www.maocular.org/membership-account/membership-confirmation/?level=')) {
   location.reload(forceGet);
  }

說明:因此,當document.referrer返回一個字符串時,我們可以利用Java語言中存在的String Class的match函數,並且在match內部,我們傳遞了直接字符串而不是regex,因為僅需要匹配,以防萬一您需要多個使用正則表達式(|)進行匹配。

請參閱此文檔以獲取更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/match

<?php
   echo '<script type="text/javascript"> 
   if (document.referrer == https://www.maocular.org/membershipaccount/membership-confirmation/?level=10) 
   { 
        location.reload(forceGet);
     }
    </script>';
?>

根據您的情況,從=變為==

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM