0

I use redirect template variable to redirect user conditionally:

{if logged_in AND (segment_3 ~ "/[^\d+]/" OR segment_3 == "")}
    {!-- Redirect to own profile if logged in--}
    {redirect="account/profile/{member_id}"}
{/if}

But instead of redirect to it's profile, I got url like: http://domain.com/account/profile/%7Bmember_id%7D

{member_id} prints correctly in template fine, but not in {redirect}. Also i tried use other variables in redirect, like {site_name}, etc., result the same they doesn't works too...

Have any ideas?

1 Answer 1

0

This will be a parse order issue, your if statement is an Advanced Conditional (Step 7 in Low's EE parse order) whereas member_id is a User Defined Global Variable (Step 9 in Low's EE parse order) so is parsed later. You'll notice the redirect works because it is on Step 8 (in Low's EE parse order) so the ordrr your template is parsed is as so :

  1. Advanced Conditional
  2. Redirect
  3. User Defined Global Variable

You see, when the redirect happens (and it happens straight away on the tag) it's too early for the member_id.

You can use a little PHP snippet or pop a script based redirect in there, js won't be executed until it gets to the browser but relies on JS. The PHP solution will work with either PHp on Input or Output.

{if logged_in AND (segment_3 ~ "/[^\d+]/" OR segment_3 == "")}
    {!-- Redirect to own profile if logged in--}
    {redirect="account/profile/<?php echo ee()->session->userdata('member_id'); ?>"}
{/if}

That should get you cookin, interestingly you can do the entire logged in check with PHP if you desire, if used with PHP on Early parsing you can acheive some efficiency as the redirect will occur before the tags are all parsed on the template...

summin like (untested)...

<?php
    if ( 
      ee()->session->userdata('group_id') != 0 &&
      preg_match( '/[^\d+]/', ee()->uri->segment(3,"X") ) 
    ) {
        ee()->functions->redirect('account/profile/'.ee()->session->userdata('member_id'));
    }
?>

Check out the EE User Data in the Session Class, Segements in the URI class and Redirect in Functions Class documentation.

3
  • your answer is almost fine. There isn't advanced conditionals anymore. Take a look
    – Sobral
    Jun 9, 2015 at 12:51
  • Thanks for your input Robson, it's good to see EE have documented the parse order now (remember when we had to work this out ourselves?), however while they may have done away with the term "Advanced Conditionals" the rules still apply, if you look to the doc you linked, theres a "Parse Conditionals" stage at step 14 AND step 19, code analysis reveals that these 2 conditional parses are basically synonymous with simple and advanced conditionals. Thanks for the link though, Low's parse order is simplified and a little out of date so this is a useful link.
    – Blatant
    Jun 9, 2015 at 13:05
  • @Robson, if you wanted to check the code base yourself (/system/expressionengine/libraries/Template/php EE2.10.1), Step 13 (PHP on Input) fires at line 435, Step 14 (first Conditionals) is at line 442, you'll see here it calls $this->simple_conditionals, then onwards (skipping steps 15, 16, 17, 18) the second conditionals starts line 498, calling $this->advanced_conditionals :)
    – Blatant
    Jun 9, 2015 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.