如何判断用户是否在表单条目

时间:2015-06-19 12:43:32

标签: php

我有一个表单,允许用户输入他们的推荐链接(URL)。我需要确保他们先在网址前输入http://。如果他们输入了它就没关系,如果他们没有输入它,那么我需要在将url放入数据库之前添加它。

在将表单保存到数据库之前,如何在PHP中检查?

1 个答案:

答案 0 :(得分:1)

使用substr(不要忘记“https://”和“//”):

<?php
$url = 'example.com'; // Here is the URL you got in input
if (substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://')
    $url = 'http://'.$url;
else if (substr($url, 0, 2) == '//') // "//" is valid, it just means that the browser needs to continue using http or https depending on what it is currently using
    $url = 'http:'.$url;