How to check if page url has www in it or not with PHP?

前端 未结 6 801
悲&欢浪女
悲&欢浪女 2021-01-18 06:15

In PHP, how can I check if the current page has www in the url or not?

E.g if the user is on http://www.example.com/something , how can I find that there\'s www in t

6条回答
  •  独厮守ぢ
    2021-01-18 06:37

    you could do a preg_match this would be:

    if(preg_match('/www/', $_SERVER['HTTP_HOST']))
    {
      echo 'you got www in your adress';
    }
    else
    {
      echo 'you don not have www';
    }
    

    you could even do it with .htaccess depending on what you are building the above option would do fine for a simpel detection.

    if you pick the option Ferdia gave do the check as following:

    $domain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
    if(in_array('www', $domain))
    {
        echo 'JUP!';
    }
    

提交回复
热议问题