Referer.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace common\lib;
class Referer{
private static $_referer = array();
public static function cleanall(){
session_id() or session_start();
unset($_SESSION['HTTP_REFERER']);
}
public static function clean($key = null){
if (!$key)
$key = $_SERVER['REQUEST_URI'];
self::session($key,'');
}
public static function get(){
if (!isset($_SERVER['REQUEST_URI']))
return '';
$key = self::__handle_url($_SERVER['REQUEST_URI']);
if (self::__request_uri() != self::__http_referer()){
self::set($key);
}
$value = self::session($key);
if (!$value){
self::set($key);
$value = self::session($key);
}
return $value;
}
public static function set($key){
if (!isset($_SERVER['REQUEST_URI']))
return '';
self::$_referer[$key] = $value = self::__referer();
self::session(0,$value);
self::session($key,$value);
//var_dump($_SESSION,self::$_referer,$key,$value);
return $value;
}
private static function __handle_url($url){
$parse = parse_url($url);
if (!empty($parse['query'])){
parse_str($parse['query'],$query);
unset($query['v']);
unset($query['spm']);
ksort($query);
$parse['query'] = http_build_query($query);
}
$url = (!empty($parse['scheme']) ? $parse['scheme'].'://' : '')
.(!empty($parse['user']) ? $parse['user']. (!empty($parse['pass']) ? ':'.$parse['pass'] : '') .'@' : '')
.(!empty($parse['host']) ? $parse['host'] : '')
.(!empty($parse['port']) ? ':'.$parse['port'] : '')
.(!empty($parse['path']) ? '/'.$parse['path'] : '')
.(!empty($parse['query']) ? '?'.$parse['query'] : '')
.(!empty($parse['fragment']) ? '#'.$parse['fragment'] : '');
return $url;
}
private static function __request_uri(){
return self::__handle_url(self::__protocol().$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}
private static function __php_self(){
return self::__protocol().$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
}
private static function __protocol(){
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'])
return 'https://';
else
return 'http://';
}
private static function __http_referer(){
if (isset($_SERVER['HTTP_REFERER']))
return self::__handle_url($_SERVER['HTTP_REFERER']);
return false;
}
private static function __referer(){
if ((isset($_SERVER['HTTP_REFERER']) && (basename($_SERVER['HTTP_REFERER']) != 'index.php')))
return $_SERVER['HTTP_REFERER'];
if (isset($_SERVER['REQUEST_URI']))
return self::__request_uri();
return self::__php_self();
}
private static function session($key, $value = null) {
if ((!$session_Id = session_id()) && !headers_sent()) {
session_start();
$session_Id = session_id();
}
if (!isset($_SESSION['HTTP_REFERER']) || !is_array($_SESSION['HTTP_REFERER']) || (count($_SESSION['HTTP_REFERER']) > 30))
$_SESSION['HTTP_REFERER'] = array();
$session = &$_SESSION['HTTP_REFERER'];
if ($value === '') {
unset($session[$key]);
return !isset($session[$key]);
}
if ($value === null)
if (isset($session[$key]))
return $session[$key];
else
return false;
$session[$key] = $value;
return isset($session[$key]);
}
}