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');
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');
After a big more digging, turns out that its caching of the pages that are preventing the sessions from working properly..
When logged into wordpress sessions work fine, but when logged out (which all the visitors will be) they don't work.
Think im gonna have to look into doing this with cookies instead.