簡體   English   中英

聯系表不再有效 - Function eregi() 已棄用

[英]Contact form no longer working - Function eregi() is deprecated

我在一個舊的 HTML 網站上有一個查詢表,它在舊版本的 PHP 上運行良好。現在的問題是 Function eregi() 其中 forms 部分代碼在 PHP 的所有新版本中都被棄用了。

我不會假裝我明白這是怎么回事::)

下面是現有的代碼——其中包含 eregi() 位:

// check for any human hacking attempts
class clean {
    function comments($message) {
        $this->naughty = false;
        $this->message = $message;
        $bad = array("content-type","bcc:","to:","cc:","href");
        $for = array( "\r", "\n", "%0a", "%0d");
        foreach($bad as $b) {
            if(eregi($b, $this->message)) {
                $this->naughty = true;
            }   
        }   
        $this->message = str_replace($bad,"#removed#", $this->message);
        $this->message = stripslashes(str_replace($for, ' ', $this->message));
        
        // check for HTML/Scripts
        $length_was = strlen($this->message);
        $this->message = strip_tags($this->message);
        if(strlen($this->message) < $length_was) {
            $this->naughty = true;
        }
   }
} // class

谷歌搜索后我猜我需要用 preg_match 替換 eregi() 位?

我不知道把它放在上面的代碼中的什么地方才能讓它工作?

有人有什么想法嗎?

提前致謝,親切的問候

布萊恩

您示例中的 eregi function 僅用於簡單的字符串比較。 您可以簡單地用 stripos 替換它:

if (stripos($this->message, $b) !== false) {
    $this->naughty = true;
}

像這樣使用

class clean {
    function comments($message) {
        $this->naughty = false;
        $this->message = $message;
        $bad = array("content-type","bcc:","to:","cc:","href");
        $for = array( "\r", "\n", "%0a", "%0d");
        foreach($bad as $b) {
            if (preg_match("/$b/i", $this->message)) {
                $this->naughty = true;
            } else {
                //comment does not contain that string.
            }
            //if(eregi($b, $this->message)) {
                //$this->naughty = true;
            //}
        }   
        $this->message = str_replace($bad,"#removed#", $this->message);
        $this->message = stripslashes(str_replace($for, ' ', $this->message));
        
        // check for HTML/Scripts
        $length_was = strlen($this->message);
        $this->message = strip_tags($this->message);
        if(strlen($this->message) < $length_was) {
            $this->naughty = true;
        }
   }
}

我只找到了eregi文檔的羅馬尼亞語頁面,它似乎說它自 PHP 5.3 以來已被棄用並在 7.0 中刪除。

由於其目的是執行不區分大小寫的正則表達式檢查,您可以將其替換為帶有i標志(代表“不區分大小寫” )的preg_match() ) :

if (preg_match(sprintf('~%s~i', $b), $this->message) === 1) {
    // ...
}

但正如@tino.codes 回答的那樣,使用 function 之類的stripos()就足夠了。

暫無
暫無

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

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