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 Redirect in EE Template

February 25, 2011 2:19pm

Subscribe [3]
  • #1 / Feb 25, 2011 2:19pm

    Jav Vilchis

    8 posts

    Scenario: I’m building a site in EE where all pages are viewable only after a user logs in. After logging in, api key and other variables are maintained in php session variables. So, if a user enters a URL directly, I need to check to see if they’ve already logged in and if the session exists.

    I’ve tried putting a simple php script at the beginning of a EE template but I guess it won’t work properly because the page is rendered and then loaded?

    Here’s what I’m currently trying…

    <?php
      if(true){
        // not logged in
        header('Location: http://...login address…');
      } else {
        // logged in already, continue
      }
    ?>
    .
    .
    rest of EE template
    .
    .

    I could use a javascript redirect, but that would be after the page is loaded and all. Any thoughts?

    Forgive me if I didn’t post this in the right category.

  • #2 / Feb 25, 2011 3:38pm

    Jav Vilchis

    8 posts

    I guess a more direct question is why won’t this work? A template containing nothing but this does nothing.

    <?php
      header(‘Location: http://www.johnkeese.com/’);
    ?>

    I’ve toggled PHP parsing stage to input and output, no change.

  • #3 / Feb 25, 2011 3:57pm

    Mark Bowen

    12637 posts

    I’m probably totally missing what you’re trying to do here but can you not use the

    {if logged_in}Perform items here if they are logged in{/if}
    
    {if logged_out}Perform items here if user is logged out{/if}

    tags to get what you need here?

    I’m not brilliant with header redirects but whenever I’ve seen them in the past people have always mentioned that you need to have an exit; statement after the header location part.

    Don’t know if any of that helps at all though?

    Best wishes,

    Mark

  • #4 / Feb 25, 2011 4:06pm

    Jav Vilchis

    8 posts

    Mark, thanks for the help. the ‘exit;’ is what I needed. A search from that jump off point led me to this:

    <?php
      if($_SERVER['SCRIPT_FILENAME'] == __FILE__ )
      header('Location: /');
    ?>

    After sending the `Location:’ header PHP _will_ continue parsing, and all code below the header() call will still be executed.  So instead use:

    <?php
      if($_SERVER['SCRIPT_FILENAME'] == __FILE__)
    {
      header('Location: /');
      exit;
    }
    ?>

    more here >

    Thanks again for the help. Just the bump I needed

    btw, I can’t use the {logged_in} variables because I’m not using the EE member groups. For the login, I’m sending api calls to an external database.

  • #5 / Feb 25, 2011 4:15pm

    Mark Bowen

    12637 posts

    Thanks again for the help. Just the bump I needed

    No problem at all. Glad that got it working for you.

    btw, I can’t use the {logged_in} variables because I’m not using the EE member groups. For the login, I’m sending api calls to an external database.

    Aha I see now. Hope it all goes well for you.

    Best wishes,

    Mark

  • #6 / Feb 26, 2011 4:31pm

    Greg Salt

    3988 posts

    Hi John,

    Glad that you got this figured out.

    Cheers

    Greg

    @Mark, thanks very much for the help.

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

ExpressionEngine News!

#eecms, #events, #releases