• I need to check which section of a site a user has come from and then display accordingly a menu from a selection of 3 types for a single page.

    In php CMS I built myself I did this – used Session to define a stream within the site i.e., if you are in this section of the site, session variable = 1, if you go to that section of the site session variable now = 2. Depending on what the session variable equals, display menu 1 or menu 2 on a particular page.

    Does WordPress allow for this? I’ve had a bit of a search but most of the info I’ve found so far talks about logins or using plugins that involve user logins. I don’t want it to have anything to do with user logins etc, just a simple ‘I am on this page’, session variable = 1 or 2.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter Grant-Senior

    (@grant-senior)

    I’ve still not found a solution to this. Can anybody tell me if it’s doable? Or point me in the direction where I may find an answer?

    Thread Starter Grant-Senior

    (@grant-senior)

    I have 3 pages public, media, profession. Each page states an appropriate $_SESSION as being either
    $_SESSION[‘siteStream’] = ‘public’;
    $_SESSION[‘siteStream’] = ‘profession’; or
    $_SESSION[‘siteStream’] = ‘media’;

    On a fourth page I have the following:

    session_start();

    //define variable for detecting which stream user is in
    if(isset($_SESSION[‘siteStream’]) ) {
    $siteStream = $_SESSION[‘siteStream’];
    }

    And in the page <?php echo $siteStream; ?>

    But for some reason it is always giving me ‘profession’ as the output. Can anybody advise as to what I could be doing wrong?

    Thanks in advance.
    Grant

    Thread Starter Grant-Senior

    (@grant-senior)

    Here’s where I have got with this so far – I have 3 pages public, media, profession. Each page states an appropriate $_SESSION as being either

    $_SESSION[‘siteStream’] = ‘public’;
    $_SESSION[‘siteStream’] = ‘profession’;
    $_SESSION[‘siteStream’] = ‘media’;

    On a fourth page I have the following:

    <?php

    session_start();

    //define variable for detecting which stream user is in
    if(isset($_SESSION[‘siteStream’]) ) {
    $siteStream = $_SESSION[‘siteStream’];
    }?>

    And in the page

    <?php echo $siteStream; ?>

    But for some reason it is always giving me ‘profession’ as the output. Can anybody advise as to what I could be doing wrong?

    Thanks in advance.

    So, e.g., when you leave a public page and arrive at a media page, the menu is supposed to output where you’ve been (public), but instead you’re getting (profession)?

    Thread Starter Grant-Senior

    (@grant-senior)

    Nearly. When I leave a public page and go to the fourth page (not media or profession) the fourth page should know where I have been. But it’s always displaying profession. $_SESSION is being set on the other 3 pages, public, media, profession.

    Thanks for your attention to this by the way.

    How are you passing the variables between pages?

    Did you remember to include

    <?php
    
    session_start();

    on all pages?

    Thread Starter Grant-Senior

    (@grant-senior)

    Yes.

    Grant-Senior,

    Because WordPress is completely stateless, it takes some work to make sessions available. Fortunately, there is a plugin that does the work for you: Simple Session Support.

    The missing puzzle piece is:

    ‘<?php
    if ( !session_id() )
    session_start();’

    Eric Mann has authored an alternative solution, the WP Session Manager.

    I do hope this information helps. I’ll check back in the morning to hear your (and hopefully others’) thoughts.

    Thread Starter Grant-Senior

    (@grant-senior)

    Hi there, I’ve just got back to this problem. I’ve tried the Simple Session Support plugin but it didn’t work for me. Here’s how I’ve been doing it just using PHP.

    On the public page header (
    session_start();

    //define variable for detecting which stream user is in
    $siteStream = ‘public’;
    $_SESSION[‘siteStream’] = $siteStream;

    On the generic multi stream page header,
    session_start();
    /* define variable for detecting which stream user has come from which is set in the Public, Media or Profession header */

    if(isset($_SESSION[‘siteStream’]) ) {

    $theStream = $_SESSION[‘siteStream’];

    if ($theStream == ‘public’){
    $theStream = ‘PUBLIC’;
    }
    elseif ($theStream == ‘media’){
    $theStream = ‘Media!’;
    }
    elseif ($theStream == ‘profession’){
    $theStream = ‘professional’;
    }
    else {$theStream = ‘where have I been?’;}

    }

    else {
    $theStream = ‘I don\’t know where I have been.’;
    }

    This works out of the WordPress environment but I can’t get it to work in WordPress.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Session to check which section of site user has come from’ is closed to new replies.