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.

Global variables and Superglobals in Templates? - Implementation problem

January 08, 2008 11:35pm

Subscribe [3]
  • #1 / Jan 08, 2008 11:35pm

    radar77

    28 posts

    Hello All,

    A newbie to EE, trying to implement some global and superglobal variables into templates in my EE site. Basic scenario is this:

    I am trying to integrate an EE site located at subdomain.mydomain.com. The main site, mydomain.com, is an ASP.NET content-managed site. There are some variables that I need to carry over to the EE site to have links transition back out of the EE site smoothly.

    Below is some PHP code (I am not a programmer, so please excuse the poor code!) that I am trying to include in my EE templates to set the variables that I need.

    <?php
    
    global $IN;
    
    //Find out where the user came from
    $fs_refer = $IN->GBL('HTTP_REFERER', 'SERVER');
    $host_refer = parse_url($fs_refer);
    $host_refer = $host_refer['host'];
    
    //Extract the WebAlias variable  for use with outbound links
    $fs_refer = explode("=", $fs_refer);
    
    //Grab First Name if it exists
    $FirstName = $IN->GBL('FirstName', 'GET');
    
    //Set the WebAlis for outbound links back to mydomain.com
    if (($host_refer == "www.mydomain.com") OR ($host_refer == "mydomain.com")) {
        
        $web_alias = $fs_refer[1];
    
    }    else {
        
        $web_alias = "grace";
        
    }
    ?>


    A brief explanation of why I need these variables:

    I am trying to capture a variable from the referring URL, which is generally in the following format. I will also need to be able to use this variable as a conditional variable within EE:

    <a href="http://www.mydomain.com/page.aspx?ID=web_alias">http://www.mydomain.com/page.aspx?ID=web_alias</a>

    And here is the format (most of the time) that links are coming into the EE site from:

    <a href="http://www.mydomain.com/page.aspx?ID=&lt?=$web_alias?&gt%22&gtLink">Link back to domain.com</a>

    In attempting to implement this as a template include which allows PHP, I get an error saying that the variables are undefined. I tried including them in the path.php global array, but as I understand it these cannot include and PHP or other code.

    At this point, any advice on how to make this work would be greatly appreciated. Or, is there an entirely different and better way to do it, I’m all ears! I love EE thus far and am really looking forward to learning more and more as I go.

    Cheers,
    Michael R.

  • #2 / Jan 09, 2008 10:23am

    Robin Sowell

    13255 posts

    Hm- I’m going to shift this over to ‘How to’ as it isn’t really a stock EE issue. 

    I am thinking it could be done in the path.php file with path globals.  But if you can get that working, you lose the variable as soon as the click on another page- since you’re defining it based on the referrer.  Seems like you’d want to use cookies or sessions- or am I misunderstanding? 

    But yea- could set a cookie- might be easiest thing.  Of course- you’d lose those folks who don’t allow them, but I’m not sure how mission critical it all is.

  • #3 / Jan 09, 2008 10:26am

    Derek Jones

    7561 posts

    Hm, I don’t see how path.php enters into here.  Can you copy and paste the exact errors please?  Then I can offer some suggestions on troubleshooting.

  • #4 / Jan 09, 2008 12:52pm

    radar77

    28 posts

    Hi Guys,

    Thanks for looking into this for me.

    @Robin
    You are correct, I do need to at least be able to store the $web_alias variable somehow as it will be reused for the static header links back to the main site. However, I only need to grab the referrer one time.

    @Derek
    Please see below the error messages that I am getting.

    Notice: Undefined index: host in /home/xxxxx/domains/subdomain.mydomain.com/html/_sys/core/core.functions.php(637) : eval()'d code on line 6
    Notice: Undefined variable: FirstName in /home/xxxxx/domains/subdomain.mydomain.com/html/_sys/core/core.functions.php(637) : eval()'d code on line 10
    Notice:  Undefined variable:  web_alias in /home/xxxxx/domains/subdomain.mydomain.com/html/_sys/core/core.functions.php(637) : eval()'d code on line 34

    Basically what I am trying to do is check to see if the user came from the main site (mydomain.com) by using HTTP_REFERER. If they did, I want to set the $web_alias variable by parsing the referral URL. If they didn’t come from the main site, I want to set the $web_alias to a static value equal to “grace.” I hope that makes sense. It seems like it should be possible.

    Thanks again for your help.

  • #5 / Jan 09, 2008 1:22pm

    Derek Jones

    7561 posts

    I don’t think I’m seeing the full code in its entirety, radar77, as the line numbers for the two latter errors don’t match up, and the statements where they are used above in the initial post would not generate those areas since you are only assigning and not referencing.  Is some of the code in another template or script?  The first error sounds like you don’t have referrer data.  You’ll want to check that it has data and is what you expect before you start referencing array keys.  To debug it, try this:

    $fs_refer = $IN->GBL('HTTP_REFERER', 'SERVER');
    var_dump($fs_refer);
    exit;
  • #6 / Jan 09, 2008 2:11pm

    radar77

    28 posts

    Hi Derek,

    Yes, the script I included in my original post is a separate include (in the includes template group) from where I am referencing the variables, in the index template of my EE main site template group.

    A quick question, in this case since I am just testing a single template right now, there is no referrer data. So, if that’s the case, I want to set the $web_alias variable to a static variable. Is my logic incorrect for doing that?

    Here is a snippet of the template structure:

    {embed="includes/webalias"}
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>
    <body>
        <!--Associate Info-->
        <div class="associateInfo">
              <span><b><?=$FirstName;?></b></span>
              <span><a href="http://www.mydomain.com/Contact.aspx?ID=&lt?=$web_alias?&gt">Contact Me</a></span>
        </div>
        <!--End Associate Info-->
    <!--End Header-->
    
      <!--NavBar-->
          <ul id="nav">
          <!-- Home -->
            <li><a href="http://www.mydomain.com/default.aspx?ID=&lt?=$web_alias?&gt">Home</a></li>

    I will try dumping the referrer date as you mentioned.

    Thanks!

  • #7 / Jan 09, 2008 2:21pm

    Derek Jones

    7561 posts

    If you’re testing, then you’ll need to provide a URL that it can parse, or $host_refer[‘host’] will never be set.  The issue you are having with trying to use these other variables in other templates is explained here, though instead of that approach, I’d recommend using $SESS->cache to keep your code portable throughout EE.  And since you have more than a couple lines of code, you’d benefit from making this into a plugin as well.

  • #8 / Jan 09, 2008 3:24pm

    radar77

    28 posts

    @Derek

    Thanks, this is very useful information. Looks like I have a lot of reading to do!!

    I understand what you mean about the host and have solved that for now. Also, I realized that the problem is exactly as you state, that I was declaring variables in other templates that weren’t being referenced globally. It looks like $SESS->cache is exactly what I’ll need, I’ll just need to wrap my head around it!

    Can you provide a link to more info on creating a plugin? I assume that would provide the capability for the code to be accessed by any template?

    Thanks again for all your help!

    Here is a working version of the code:

    <?php
    
    global $IN;
    
    $host_refer="";
    
    //Grab First Name if it exists
    $FirstName = $IN->GBL('FirstName', 'GET');
    if ($FirstName=false) {
        $FirstName = "My Name";
    }
    
    //Find out where the user came from
    $fs_refer = $IN->GBL('HTTP_REFERER', 'SERVER');
    
    if ($fs_refer == false) {
    
        $web_alias = "grace";
        
    } else {
    
        $host_refer = parse_url($fs_refer);
        $host_refer = $host_refer['host'];
        $fs_refer = explode("=", $fs_refer);
    }
    
    //Set the WebAlis for outbound links back to mydomain.com
    if (($host_refer == "www.mydomain.com") OR ($fs_refer == "mydomain.com")) {
    
        $web_alias = $fs_refer[1];
    
    } else {
    
        $web_alias = "grace";
    
    } 
    
    
    ?>
  • #9 / Jan 09, 2008 3:28pm

    radar77

    28 posts

    One last thing…

    Once I establish these variables globally, can they then be used as a condition variable?

    For example:

    {if web_alias == grace}
    <div>Special content for this web alias.</div>
    {/if}
  • #10 / Jan 09, 2008 3:31pm

    Derek Jones

    7561 posts

    $FirstName = $IN->GBL('FirstName', 'GET');
    if ($FirstName=false) {
        $FirstName = "My Name";
    }

    In your conditional, you’re setting $FirstName to FALSE, which will never fail, so $FirstName will always be set to “My Name”.  For comparisons you want == not =.

    Also if $fs_refer is FALSE, then $host_refer will never be set, leading to another PHP error when it comes up in the conditional.  That last conditional should probably be inside of the ‘else’ portion of the $fs_refer conditional.

    For EE development, start with this page which can direct you to wherever you need to go (or use the User Guide search.  The Plugin API docs are among that list. :-D

  • #11 / Jan 09, 2008 3:32pm

    Derek Jones

    7561 posts

    One last thing…

    Once I establish these variables globally, can they then be used as a condition variable?

    For example:

    {if web_alias == grace}
    <div>Special content for this web alias.</div>
    {/if}

    No EE conditional variables are not automatically created from PHP variables.  In fact, we strongly discourage the use of global variables apart from the global class objects.

  • #12 / Jan 09, 2008 4:14pm

    radar77

    28 posts

    Thanks Derek, those corrections make sense.

    So, is there any way to convert a global variable to a conditional variable?

    Also, out of curiosity, why is it discouraged to use custom global variables? Is it a security concern? I’m still new to EE and PHP even, so I’m always looking to do things the “right way.” Is there a better approach to take than what I am currently doing?

    Cheers,
    Michael

  • #13 / Jan 09, 2008 4:31pm

    Derek Jones

    7561 posts

    Global variables can lead to sloppy programming habits, and on some servers, yes, there can be security concerns as well.  EE protects you from those, but it’s again a practice that is frowned upon (using this type of code in an external script, for instance, wouldn’t benefit from EE’s security protection in this regard).  It also doesn’t fit well with the practice of writing object-oriented code.  Again, I’d just use $SESS->cache as outlined by the link above in place of your globals.  Parsing conditionals in a plugin or module is also covered in the developer guide, under the Template class docs.

  • #14 / Jan 09, 2008 5:50pm

    radar77

    28 posts

    Thanks for the info Derek.

    Can you provide a quick example of how to use $SESS->cache in this case?
    After reading through the documentation, it is not entirely clear to me.

    Thanks again.

  • #15 / Jan 09, 2008 6:14pm

    Derek Jones

    7561 posts

    First call the global class object:

    global $SESS;

    Then set the values as needed.

    $SESS->cache['radars_referers']['web_alias'] = 'grace';
       ...
    $SESS->cache['radars_referers']['fs_refer'] = $IN->GBL('HTTP_REFERER', 'SERVER');

    And in your other templates or plugin methods, just access the value per usual:

    echo $SESS->cache['radars_referers']['web_alias'];
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases