search
HomeBackend DevelopmentPHP TutorialHow to use PHP to obtain referer and determine the source to prevent illegal access

This article will introduce to you how to use PHP to obtain the referer to determine the source to prevent illegal access? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use PHP to obtain referer and determine the source to prevent illegal access

The php code of download page down.php Now I found that if I open it directly with Thunder or Google Chrome, the downloaded file can be output, and it has no anti-leeching effect at all. Now I want to allow only those connected to my own site to use it directly. Those connected to other sites and those who directly enter this address will jump to the copy.htm page.

The $_SERVER["HTTP_REFERER"] predefined server variable in PHP can determine the source.

$_SESSION['HTTP_REFERER'] can obtain the source address of the previous connection of the current link, that is, the URL address of the previous page linked to the current page.
It is generally used to determine where the viewer clicked the link to jump to this page, that is, the so-called origin. It can also be used to prevent hotlinking by determining the origin.
For example:

<?php
 $url_array = parse_url($_SESSION[&#39;HTTP_REFERER&#39;]); 
 //如果页面的域名不是服务器域名,就连接到登陆窗口 
 if($_SERVER[&#39;HTTP_HOST&#39;] != $url_array["host"]) { 
 header("location: login.php"); 
 exit; 
 } 
 ?>

Recently there is a project that needs to prevent users from illegally accessing a json page. The basic solution is to determine the source to restrict non-call access:

$_SERVER[‘HTTP_REFERER’]:来路链接,可能带尾巴(如: 
可以通过php内置函数parse_url()来获取到当前网址(www.httple.net),即:
$refererUrl = parse_url($_SERVER[‘HTTP_REFERER’]);
$host = $refererUrl[‘host’];
$host的值即为来路的网址(www.httple.net)。
获取到了来路的网址之后,我们就可以通过这个网址来限制访问该页面的权限了。代码如下:
if(!isset($_SERVER[‘HTTP_REFERER’]) || $referurl[‘host’] !=”www.httple.net”) { header(“location: /”); 
//如果没有来路,或者来路不是本站,跳转到首页。 exit; }

Putting this line of code at the top of the json data page can easily solve this problem.

Defects of this processing method: the normal data of the page can be obtained through forged sources.

Related code

The method of getting the source Url mainly uses the HTTP_REFERER function in the server variable. The code is pasted:

function get_referer(){   
$url = $_SERVER["HTTP_REFERER"]; //获取完整的来路URL   
$str = str_replace("http://","",$url); //去掉http://   
$strdomain = explode("/",$str); // 以“/”分开成数组   
$domain = $strdomain[0]; //取第一个“/”以前的字符  
return $domain;  
}   

//对于百度、谷歌搜索引擎来路判断   
function get_seo(){  
$s = 0;   
if(strstr(get_referer(),&#39;baidu.com&#39;)){   
$s = 1;   
}   
else if(strstr(get_referer(),&#39;google.com.hk&#39;)){   
$s = 1;   
}   
return $se;    
}

php website Get the source Url The method mainly uses the HTTP_REFERER function in the server variable. The code is pasted:

function get_referer(){
$url = $_SERVER["HTTP_REFERER"]; //获取完整的来路URL
$str = str_replace(“http://”,””,$url); //去掉http://
$strdomain = explode(“/”,$str); // 以“/”分开成数组
$domain = $strdomain[0]; //取第一个“/”以前的字符
return $domain;
}

//对于百度、谷歌搜索引擎来路判断
function get_seo(){
$s = 0;
if(strstr(get_referer(),’baidu.com’)){
$s = 1;
}
else if(strstr(get_referer(),’google.com.hk’)){
$s = 1;
}
return $se;
}

When processing a form, you have to consider Discuz has already judged the possibility of static submission by users based on formhash

Here I use another way to determine the origin of the page. Of course, this method can also be used to forge the origin of HTTP_REFERER

The second part is to solve the problem that header('location: in PHP cannot get HTTP_REFERER for the next page after jumping to the page. Here we can only add a link to the page and then use js to simulate clicking the link, so that the next page will definitely Received HTTP_REFERER. Keyword: document.getElementById('gourl').click();

Recommended learning: php video tutorial

The above is the detailed content of How to use PHP to obtain referer and determine the source to prevent illegal access. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)