Referer.php 3.7 KB
<?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]);
    }
}