PHP Session variable not always updating in WordPress

I’m trying to pass the URL of a page to a session variable, so i can use it in a login plugin.
The login plugin only refers to the profile page, but it won’t refer to the previous page the user was on. I’ve tried using wp_get_referer(); in the login plugin itself, but because the login form sends it to a different page, the previous page according to wordpress is the login page.

In order to do this i first of all put the following in my functions.php:

Read More
add_action('init', 'myStartSession', 1);

function myStartSession() {
    if(!session_id()) {
        session_start();
    }
}

In my header.php, I check if the user is on the login (profiel) page or not. If he isn’t, the current URL should be placed in a session variable. If he is, the session variable shouldn’t change. To do this, i use the following code, right below the start of <body>:

<?php
    if (!is_page('profiel')) {
           $_SESSION["refurl"] = $_SERVER['HTTP_REFERER'];
       }
?>

However, this only seems to work sometimes. On the ‘profiel’ page, where the login form is, I added <?php echo 'session' .$_SESSION["refurl"]; ?> to check wether or not the previous URL has been saved to the session variable. The problem is that a lot of the times it refers to my home url, instead of the page previously visited. What am i doing wrong?

Related posts

Leave a Reply