PHP Sessions lost between pages

Discussion in 'PHP' started by cubicaaron, Sep 7, 2010.

  1. #1
    Hi all,

    I have a site that is split into domestic and commercial tabs, much like whats over at British Gas' website.

    Once a user selects one or the other, a different stylesheet is loaded on the page to give a diffferent user experience.

    I'm using PHP Sessions to differentiate customers once they have selected Commercial. I also have a few fail safes - some pages, dependant up on their page parents will have commercial or domestic in the URL.

    If they are present, the Session is set again.

    Some pages are not specific to either of the two sections, i.e. FAQ's and contact us, but I still want it to reflect the stylesheets of the customer type who is logged in. If I click onto one of these pages which doesnt reinforce the PHP SESSION, then click onto another, the Session data is lost.

    Any thoughts?
    
    session_start();
    	if (is_page_or_ancestor(882)):
    		$_SESSION["UserType"] = 2;
    	elseif (is_page_or_ancestor(916)):
    		$_SESSION["UserType"] = 1;
    	endif;
    	if (inStr("commercial", get_permalink())){
    		$_SESSION["UserType"] = 2;
    	} elseif (inStr("domestic", get_permalink())){
    		$_SESSION["UserType"] = 1;
    	}
    PHP:
    (I've got an InStr function defined in the form of InStr(needle, haystack)

    Any help or thoughts would be appreciated!!!
     
    cubicaaron, Sep 7, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    is the session_start right at the top of every script? No output before not even a space?
     
    stephan2307, Sep 7, 2010 IP
  3. cubicaaron

    cubicaaron Guest

    Messages:
    104
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Its at the top of the wordpress file, after the comments, but there is no space between the comment block and the start of the session_start.
     
    cubicaaron, Sep 7, 2010 IP
  4. cubicaaron

    cubicaaron Guest

    Messages:
    104
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Should this need to go above the comment block?
     
    cubicaaron, Sep 7, 2010 IP
  5. TheWiseGuy

    TheWiseGuy Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #5
    do you use header: location redirects in this site?

    in case you do you might try to replace your single line redirect with this:
    
    session_regenerate_id(true);
    header("Location: ".$dest);
    die();
    
    Code (markup):
    let me know if this fixes it
     
    TheWiseGuy, Sep 7, 2010 IP
  6. cubicaaron

    cubicaaron Guest

    Messages:
    104
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I dont use header redirects, the Session variable simply lets me choose to laod two different stylesheets
     
    cubicaaron, Sep 7, 2010 IP