ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

PHP parsing order issue?

April 25, 2012 12:01pm

Subscribe [2]
  • #1 / Apr 25, 2012 12:01pm

    TheBlackHole

    72 posts

    Hi all,

    I am using the following PHP to store and output a URL variable…

    <?php
     if(isset($_GET['media'])){
      $_SESSION['media']=strip_tags($_GET['media']);
     }else{
      $_SESSION['media']="DEFAULT";
    
     }
    ?>
    
    Media code: <?php echo strip_tags($_SESSION['media']); ?>

    It works fine unless I put the first set of php tags in another template in which case I get this error…

    A PHP Error was encountered
    
    Severity: Notice
    
    Message: Undefined variable: _SESSION
    
    Filename: libraries/Functions.php(659) : eval()'d code
    
    Line Number: 19

    Any ideas?

  • #2 / Apr 25, 2012 12:08pm

    Carl W Crawley

    136 posts

    The issue (as I understand it) is that you can’t use the $_SESSION inside of a template (for whatever reason, it’s never worked for me) - instead store it in the session class instead:

    http://ellislab.com/expressionengine/user-guide/development/usage/session.html

    Thats what I’ve used in the past anyway and it’s worked.

    One question though - why are you setting a session variable which you then use further on in the page? why not set it as a variable $media = strip_tags($_GET[‘media’]); and then you can simply “echo $media;”? or have I missed something?

    Cheers,

    Carl.

  • #3 / Apr 25, 2012 2:41pm

    TheBlackHole

    72 posts

    Ah, I didn’t know that. This is unchartered territory for me so I asked someone with PHP knowledge to write it.

    I need to check if there is a media code in the url in case someone has clicked a promo link and then set the variable accordingly (or set it to a default value) for the purposes of linking off site for another domain with a similar system in place.

    Could you show me a better way to do this? Using native EE code if its possible?

    Many thanks
    Mike

  • #4 / Apr 26, 2012 12:29pm

    Dan Decker

    7338 posts

    Hi Mike,

    Thank for posting to the forums!

    You might have some luck with Session Variables, a plugin that lets you access native PHP sessions without using PHP in your templates.

    Thank you Carl for chiming in!

    This might be better served in the Development and Programming forum, would you like me to move it there for you?

    Cheers,

  • #5 / Apr 26, 2012 12:33pm

    TheBlackHole

    72 posts

    That looks great Dan, I’m still going to need some assistance though : )

    Move the thread if you think it would be better suited elsewhere.

    Cheers

  • #6 / Apr 27, 2012 4:41am

    TheBlackHole

    72 posts

    Will I be able to get something out of the URL with the session variables plugin? Bearing in mind I’ll never know which segment it will be?

    Cheers

  • #7 / Apr 27, 2012 4:59am

    TheBlackHole

    72 posts

    This is working using the Session variables plugin, but is there a better way to do it?

    In head of document

    <?php
     if(isset($_GET['media'])){
      $_SESSION['media']=strip_tags($_GET['media']);
     }else{
      $_SESSION['media']="DEFAULT";
    
     }
    ?>
    
    {exp:session_variables:set name='media_code' value='<?php echo strip_tags($_SESSION['media']); ?>'}


    In body of the document

    Media code: {exp:session_variables:get name='media_code'}
  • #8 / Apr 27, 2012 5:05am

    TheBlackHole

    72 posts

    Actually, this doesn’t work as it needs to. As soon as the media code isn’t present in the URL (which will happen as soon as the user navigates away from the first page) the media code goes back to DEFAULT.

  • #9 / Apr 30, 2012 11:47am

    TheBlackHole

    72 posts

    Hi again,

    I have this most of the way there. The PHP needed to be altered…

    In the head of the document

    <?php
       if (isset($_GET['media']))
       {
        $_SESSION['media'] = strip_tags($_GET['media']);
       }
       else if ( ! isset($_SESSION['media']))
       {
        $_SESSION['media'] = "DEFAULT";
      
       }
      ?>
      
      {exp:session_variables:set name='media_code' value='<?php echo strip_tags($_SESSION['media']); ?>'}

    Then this outputs the code

    Media code: {exp:session_variables:get name='media_code'}

    e.g. http://somesite.com?media=123

    Would produce “Media code: 123”

    But as soon as you navigate away from a page without this output on, the output defaults until the page is refreshed. I clearly don’t understand how php sessions work : (

    I really hope someone how understands this stuff will be able to help.

    Many thanks

  • #10 / Apr 30, 2012 2:59pm

    Dan Decker

    7338 posts

    Hi Mike,

    Indeed, this is out of our general wheelhouse for technical support.

    However, looking over your code and the way the plugin works, you are adding an unneeded step:

    <?php
       if (isset($_GET['media']))
       { ?>
        {exp:session_variables:set name='media_code' value='<?php echo strip_tags($_GET['media']); ?>'}
       <? }
       else
       { ?>
        {exp:session_variables:set name='media_code' value='DEFAULT'}
       <? }
      ?>

    That might get you closer to your goal.

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases