获取REQUEST_URI并且不要覆盖

I tried storing the REQUEST_URI in a session with the code below. I need to store the very first REQUEST_URI when a visitor lands on a page on our site and not have it overwritten when they begin browsing pages. This isn't working as I browse the site i'm seeing a new URI displayed by the echo.

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

echo $_SESSION['theuri'];

I found this article which said to add this code to my functions.php 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 ();
}

Clear your cookie and try again. It was probably set the first time when you never had the !empty() conditional and never changed since

You might try this hack:

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

Should help if WP tries to start session for you.

You must start the session before any output so the cookies can be set correctly. Wordpress is stateless and uses no sessions at all. You mention you put this code in the footer, so probably there is output already.

A hacky solution would be to enter session_start() on the top of your index.php, or in wp-config.php.