Support » Developing with WordPress » Session variable not passing through to registration

  • Resolved TIEro

    (@tiero)


    I’m adding a bit of simple code to record a user’s referrer on a site. Unfortunately, the session variable is not passing through to registration.

    The user goes to site.com?ref=999 (where 999 is the referring member’s user ID).

    In my included php file, I have the following:

    // Add action to kick off a session and detect ref id
    add_action('init','was_start_session');
    
    // Add action to store ref id after registration
    add_action('user_register', 'was_record_referrer', 10, 1);
    
    // Function to record referrer ID in session
    function was_start_session() {
    
    	// Check for an active session: if there is none, start one. 
    	if (session_status() == PHP_SESSION_NONE) {
    		session_start();
    	}
    
    	// Check for referrer and store in session.
    	// If there is no referrer, assign to "user zero".
    	$_SESSION['ref'] = (isset($_GET['ref'])) ? $_GET['ref'] : 0;
    }
    
    // Record referrer's user id in new user meta
    function was_record_referrer($user_id) {
    	update_user_meta($user_id, '_user_referrer', $_SESSION['ref']);
    }

    I added an echo command after setting the session ‘ref’ variable and it shows the right value (999). I added the same echo to the bottom function and it invariably shows zero. WordPress is dropping the variable between their arrival on the site and registering.

    Any ideas why?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Update: tested on a clean install with only that plugin running and the basic Twenty[x]teen theme. Nope, still not working. 🙁

    Moderator bcworkz

    (@bcworkz)

    What’s happening is the URL parameter gets dropped when the user navigates away from the initial page. If the referring link were to go directly to the registration page and the user registered without going elsewhere, I think your code will work.

    Once the URL parameter gets dropped, your ternary code assigns 0 to the session variable regardless of there being a useful value or not. The fix is once the session is started, check if the session variable is already set. If it is, skip the $_GET[‘ref’] stuff. I suppose your code should reverse that logic — if not set try $_GET[‘ref’], then either assign the value or 0

    Aha! Of course… because it runs the session bit at the start of each page load, so it checks the URL *again* and finds no “?ref=”, so assigns zero. It’s all so obvious once someone smart points it out. Thank you VERY much.

    For reference, added a “!isset” check to the session function as follows:

    function was_start_session() {
    
    	// Check for an active session: if there is none, start one. 
    	if (session_status() == PHP_SESSION_NONE) {
    		session_start();
    	}
    
    	// Check for referrer and store in session.
    	// If there is no referrer, assign to user zero.
    	if (!isset($_SESSION['ref'])) {
    		$_SESSION['ref'] = (isset($_GET['ref'])) ? $_GET['ref'] : 0;
    	}
    }

    Tested and working. Resolved. Thanks again!

    • This reply was modified 3 years, 4 months ago by TIEro.
    Moderator bcworkz

    (@bcworkz)

    You’re welcome. It’s nice you think I’m smart 🙂 It’s really more of just getting some fresh eyes on the problem.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Session variable not passing through to registration’ is closed to new replies.