hotlink protection

2020-04-18 05:49发布

i made this simple code to prevent hotlinking my files from my php download file :

if ((strpos($_SERVER['HTTP_REFERER'],'www.domain.com')!==0)) {
    $redirect='index.php';
    header("Location: $redirect");
    exit;
}

it's not working , it always redirect me to index.php even if i clicked the link inside my wbesite. i tried to change the domain to many types like :

http://www.domain.com
www.domain.com
domain.com
domain

but still the same problem

2条回答
男人必须洒脱
2楼-- · 2020-04-18 06:37

You actually want to use !== FALSE instead. The string could be at position 0. Also include zerkms' suggestion:

if (!empty($_SERVER['HTTP_REFERER']) && 
    (strpos($_SERVER['HTTP_REFERER'],'www.domain.com') !== FALSE)) {

Documentation: http://php.net/manual/en/function.strpos.php

查看更多
唯我独甜
3楼-- · 2020-04-18 06:50

i found the solution, i just made a compare between HTTP_REFERER and the HTTP_HOST using strpos, if they match that mean there is no hotlinking. the code :

if($_SERVER['HTTP_REFERER'])
   {
      if(!strpos($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))
         {
            $redirect='index.php'; 
            header("Location: $redirect");
         }
   }
查看更多
登录 后发表回答