doujing2017 2016-03-06 23:45
浏览 186
已采纳

php Mac地址检查格式

I am getting the mac of the user from the url. What I'm trying to do is match to see if the address is genuine. Cause I'll be running it against a database and I don't want to run it if it's not the right format.

This is what I have so far:

if (isset($_GET['mac'])) { 
    if (strlen($_GET['mac']) == 18) { 
        $get_mac_filtered = preg_replace('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', '',$_GET['mac']);
            if (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', $_GET['mac']) == $get_mac_filtered) {
                echo 'Got a mac match! '.$get_mac_filtered;
            }else {
                echo 'Sorry !=';
            }
}else {die();} }

I'm guessing the problem is somewhere in here (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', $_GET['mac']) == $get_mac_filtered) because it returns else

  • 写回答

2条回答 默认 最新

  • dplbf4340 2016-03-06 23:57
    关注

    Remove this line. It does nothing useful:

    $get_mac_filtered = preg_replace('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', '',$_GET['mac']);
    

    As written, this replaces anything resembling a valid MAC address with… nothing. This is almost certainly not what you want to do here.

    Instead, on the next line, compare the results of preg_match() to 1. (It returns 1 if the regular expression matches, and 0 if it does not.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题