---
title: "PHP 判断请求来源"
author: "onestopweb"
date: "2018-01-28T11:01:11Z"
tags: []
categories: ["编程语言"]
slug: "php"
draft: false
lastmod: "2018-01-28T11:28:09Z"
id: 2409537
url: "https://onestopweb.iteye.com/blog/2409537"
---

```
<?php
header('Content-Type: text/html; charset=utf-8');
define("WEBSITE", "http://onestopweb.iteye.com");

if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['fdata'])){
    sourceJudgment();
    echo $_POST["website"];
    exit;
}
//来源判断
function sourceJudgment(){
    if (strpos($_SERVER['HTTP_REFERER'], WEBSITE)===false) {
        header('Location:'.WEBSITE.'/index.php?f='.$_SERVER['HTTP_REFERER']);
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>PHP 判断请求来源</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
	<input type="text" name="website" value="" placeholder="请输入网址">
	<input type="submit" name="fdata" value="提交">
</form>
</body>
```

  

效果图：  
![](http://dl2.iteye.com/upload/attachment/0128/7069/831b4e51-c2e4-3fa8-bda6-21a119308071.png)