duadlkc5762218 2014-07-16 23:48
浏览 64

无法让http_referer留在会话中

I have been searching for hours and cant find the answer to this one.

I am trying to add the referring url to an email message (form sent by visitor on the website) so I can know what website the visitor was referred from. (part of ongoing analytic).

I am trying to set the SERVER["HTTP_REFERER"] into a session like so..

if(!isset($_SESSION["inbound"])) { 
    $_SESSION["inbound"] = $_SERVER["HTTP_REFERER"];
 }

but the session keeps changing every time another page is loaded. I presumed putting the ! before isset would tell it that there is already a session and not to try adding it again.

I have also tried it this way (and a combinations of other ways):

if(isset($_SESSION["inbound"])) { 
    // do nothing 
} else { 
    $_SESSION["inbound"] = $_SERVER["HTTP_REFERER"]; 
}

I am doing this in WordPress, but I dont think that should be an issue. I have used sessions in Wordpress many times before without any problems.

Any advice or help is greatly appreciated! Thanks Eoin

UPDATE: Have tried it like this:

function get_ref_session() {
    if(!isset($_SESSION["inbound"])) { 
        $the_referer = $_SERVER["HTTP_REFERER"]; 
        $_SESSION["inbound"] = $the_referer; 
    }
}
add_action( 'wp_head', 'get_ref_session' );

No joy this way either :(

Tried this in the plugin and in functions.php, no joy. (at the top)

function register_session(){
    if( !session_id() )
        session_start();
}
add_action('init','register_session');
  • 写回答

2条回答 默认 最新

  • dongshuxi3105 2014-07-16 23:51
    关注

    Wordpress does not use PHP sessions by default. It directly sets cookies to manage its own sessions. You need to include session_start(); before any header information is sent. Otherwise no session data will be saved.

    How to use session in wordpress in plugin development

    function register_session(){
        if( !session_id() )
            session_start();
    }
    add_action('init','register_session');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题