douci6541 2015-06-19 12:43
浏览 23
已采纳

如何判断用户是否在表单条目中输入了http:// [复制]

This question already has an answer here:

I have a form allowing users to enter in their referral link (URL). I need to be sure they entered the http:// first before the url. If they entered it then it's ok, if they did not enter it then I need to add it before placing the url in the database.

How can I check this in PHP before saving the form in the database?

</div>
  • 写回答

1条回答 默认 最新

  • duanfu9523 2015-06-19 12:46
    关注

    Use substr (and don't forget "https://" and "//") :

    <?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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0