簡體   English   中英

如何使用html或php使用“ http://”預填充type =“ url”表單字段?

[英]How to pre-fill a type=“url” form field with “http://” using html or php?

問題 :在要求用戶訪問網站的表單上,除非域名前面有“ http://”,否則type =“ url”表單字段將返回錯誤(甚至“ www。”也有錯誤)。

我正在尋找的結果 :一種PHP / HTML *方法,部分用“ http://”預填充type =“ url”字段,因此用戶在該字段中鍵入“ companywebsite.com”並提交更方便沒有錯誤。

換句話說,用戶只填寫了companywebsite.com,而我想用http://預先填寫,因此最終結果是http://companywebsite.com

<h5>Website</h5>
<input type="url" name="company_website" id="company_website" value="" />


下圖是返回錯誤的表單條目的屏幕截圖(因為“ http://”不在stackoverflow.com域之前)。

形式錯誤的圖像,因為“ http://”不在鏈接前面

有兩種方法可以解決此問題:

<input type="url" name="company_website" id="company_website" value="http://" />

但是,我建議您不要這樣做,因為您永遠不會信任用戶正確輸入。 相反,假設您要通過POST方法在php代碼中提交表單,

<?php

    if(isset($_POST['company_website'])){
        $pos = strpos($_POST['company_website'], "http://");
        if($pos != 0){
            $website = "http://".$_POST['company_website'];
        }
        // do the rest
    }
?>

好吧。 為了學習有關安全性的知識,我將用一些示例代碼來編寫此答案,以便您知道將來如何安全地進行此類操作。

首先,讓我們寫一個小表格:

<form method="POST" action="your_php_file.php" accept-charset="utf-8">
    <input type="url" name="company_website" id="company_website" value="" />
    <input type="submit" value="Update website" />
</form>

現在開始基於此編寫我們的PHP文件。 首先,我們需要確保用戶提交的是新數據,而不是空字符串:

if(isset($_POST['company_website']) && !empty($_POST['company_website'])){

接下來,我們將編寫一個函數來檢查字符串是否是網站:

function isWebsite($url){

要查看字符串是否與網站匹配,我們將使用正則表達式。 正則表達式可能非常復雜,因此在此答案的結尾,我將發布一個鏈接以了解有關它們的更多信息。

    $pattern = '#((https?|ftp)://(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i';

現在讓我們編寫一個簡單的if語句,該語句返回true或false。 為簡單起見,此語句將變量$url (包含用戶數據)與$pattern的上述正則表達式模式進行比較。 如果$url包含有效的網站,它將返回true;否則,將返回false。

    if(preg_match($pattern, $url)){
        return true;
    } else {
        return false;
    }
}

接下來,我們將編寫一個函數,在用戶發送的數據之前添加“ http://”:

function addHttp($url){
    $url = "http://". $url;
    return $url;
}

現在我們的功能已經完成,我們所要做的就是使用它們。 首先,我們將檢查用戶是否已經發送了有效的網站:

if(isWebsite($_POST['company_website'])){
    //The website is valid, so you can safely add it your database here
    } else {
        //The website is not valid, but the user might simply have forgotten
        //to add http:// in front of it. So lets do it for him:
        $website = addHttp($_POST['company_website']);

        //Still we can't be sure if it's a valid website. It might be a string
        //that will try to mess with your database. So lets verify it again:
        if(isWebsite($website)){
            //The website is now valid, so you can safely add it your database here
        } else {
            //The website is still not valid, so we need to return an error
            //to your user:
            echo "It seems you didn't enter a valid website. Please try again!";
            exit;
        }
    }
}

您可能已經注意到,我們的代碼中有2個地方需要編寫相同的代碼才能將所有內容插入數據庫。 您也可以使用一個函數:

function addToDatabase($url){
    //Write your code to add everything to database here. $url will contain the website
}

因此完整的代碼將如下所示:

<?php

if(isset($_POST['company_website']) && !empty($_POST['company_website'])){

    function isWebsite($url){
        $pattern = '#((https?|ftp)://(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i';

        if(preg_match($pattern, $url)){
            return true;
        } else {
            return false;
        }
    }

    function addHttp($url){
        $url = "http://". $url;
        return $url;
    }

    function addToDatabase($url){
        //Write your code to add everything to database here. $url will contain the website
    }

    if(isWebsite($_POST['company_website'])){
        addToDatabase($_POST['company_website']);
    } else {
        //The website is not valid, try adding 'http://'
        $website = addHttp($_POST['company_website']);

        if(isWebsite($website)){
            addToDatabase($website);
        } else {
            //The website is still not valid, return error
            echo "It seems you didn't enter a valid website. Please try again!";
            exit;
        }
    }
}

?>

我應該補充一點,只是盲目地將網站添加到您的數據庫中可能仍然不安全。 為了使其絕對安全,您需要結合使用PDO()MySQLi()和Prepared Statements。 但是,如果在此答案中加以說明,則會將其變成一本書。 因此,請務必了解它!

最后,我不允許提供資源來學習更多有關正則表達式的信息。 最好和最安全的資源將來自PHP官方網站本身。 您可以在這里找到它: PHP:模式語法

暫無
暫無

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

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