Get REQUEST_URI and don't overwrite

I tried to keep REQUEST_URI in session with below code. I need to keep the very first REQUEST_URI when a visitor lands on a page on our site and doesn't overwrite it when they start browsing. It doesn't work, when I browse the site I see the new URI echoed.

session_start();
if ( !isset( $_SESSION['theuri'] ) ) {
    if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
        $_SESSION['theuri'] = $_SERVER['REQUEST_URI'];
    }
}

echo $_SESSION['theuri'];

      

+3


source to share


4 answers


I found this article which said to add this code to my .php functions and now the code in my footer is working fine.

http://devondev.com/2012/02/03/using-the-php-session-in-wordpress/



add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');

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

function myEndSession() {
    session_destroy ();
}

      

0


source


Clear your cookie and try again. It was probably installed the first time you never had it! Empty () is conditional and has never changed since



0


source


You can try this hack:

if(session_id() == '') 
     session_start();

      

Should help if WP is trying to start a session for you.

0


source


You must start a session before any exit for the cookies to be set correctly. Wordpress has no status and does not use sessions at all. You mentioned that you put this code in the footer, so there is probably a way out already.

A hacky solution is to inject session_start () at the top of your index.php or in wp-config.php.

0


source







All Articles