Route Users based on Login

Discussion in 'Customization & add-ons' started by eqewebmaster, Aug 5, 2009.

  1. eqewebmaster

    eqewebmaster New Member

    Joined:
    Jun 29, 2009
    Messages:
    21
    Basically, I need to route users to different homepages depending if they're logged in or not.

    Guests/Non-logged in members would see a default homepage, while logged-in members will see a homepage with News.

    I'm aware I could just set my Product URL to the homepage, however that wouldn't help me in links that go back to "Home" (which home, would be determined by whether or not they're logged in.)

    I was thinking I could do this with PHP code, but I don't know how to code something like this from scratch. Is anyone out there savvy enough with PHP code, or is there an alternative way of doing this within aMember Pro?

    Update: Somebody suggested this script, however it doesn't appear to be working properly. When I run it, it only takes me to "public-home.php," even if I am logged in (it should be taking me to the member's home, "home.php").

    PHP:
    <?php
    //start the session
    session_start();

    // do the login function
    if ($username == "true")

    // above and below means if username and password is correct
    if ($password == "true")

    //..and if all is correct, set the 'is_login' to true and immediately redirect to the member page.
    $_SESSION['is_login'] = "true";
    header('Location: http://www.equipmentemporium.com/home.php' );


    //if the session called 'is_login' is true
    if($_SESSION['is_login'] == "true")

    //...then go to member page.
    header('Location: http://www.MYSITE.com/home.php' );
    else{
    //...else go to guest page
    header('Location: http://www.MYSITE.com/public-home.php' );
    }
    ?>
  2. skippybosco

    skippybosco CGI-Central Partner Staff Member

    Joined:
    Aug 22, 2006
    Messages:
    2,526
    haven't tested it, but this seems more straightforward:

    Code:
    <?php
    
    if (!isset($_SESSION)) session_start(); 
    
    if ($au=$_SESSION['_amember_user']){
         header('Location: http://www.equipmentemporium.com/home.php' );
    else{
         header('Location: http://www.MYSITE.com/public-home.php' );
    }
    
    ?>
  3. eqewebmaster

    eqewebmaster New Member

    Joined:
    Jun 29, 2009
    Messages:
    21
    Tested it, ran into this message:

    Update:

    Tried modifying the code to this:
    PHP:
    <?php

    if ($au=$_SESSION['_amember_user'])
         
    header('Location: http://www.MYSITE.com/home.php' );
    else
         
    header('Location: http://www.MYSITE.com/public-home.php' );
    ?>
    I no longer receive the Parse Error, but it doesn't properly re-direct me, logged in or out. It just goes straight to public-home.php regardless.
  4. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    PHP:
    <?php

    if (!isset($_SESSION)) session_start(); 

    if (
    $au=$_SESSION['_amember_user']){
         
    header('Location: http://www.equipmentemporium.com/home.php' );
    else{
         
    header('Location: http://www.MYSITE.com/public-home.php' );
    }

    ?>
    There should be a '}' before the 'else' to close the '{' following the 'if' statement otherwise there is no closing bracket to end the instructions contained in first part of 'if' statement.

    Since both the if and else contain only one statement I don't think the curly brackets are required.
  5. skippybosco

    skippybosco CGI-Central Partner Staff Member

    Joined:
    Aug 22, 2006
    Messages:
    2,526
    Sorry about that cut and paste error, serves me right trying to post code snippets using a mobile phone :)
  6. eqewebmaster

    eqewebmaster New Member

    Joined:
    Jun 29, 2009
    Messages:
    21
    Update: Nevermind anything I said before in this post. I figured it out, and it works fine.

    Thanks alot! This is why I love the aMember forums. People help you!
  7. skippybosco

    skippybosco CGI-Central Partner Staff Member

    Joined:
    Aug 22, 2006
    Messages:
    2,526
    Glad you got sorted..

    If different than above, would be great if you could share you final code snip for others that have the same need.
  8. eqewebmaster

    eqewebmaster New Member

    Joined:
    Jun 29, 2009
    Messages:
    21
    Gladly.

    To re-summarize, if somebody clicks on my "Home" link they are in actuality clicking on "gohome.php" which is simply the following code:

    PHP:
    <?php

    if (!isset($_SESSION)) session_start(); 

    if (
    $au=$_SESSION['_amember_user']){
         
    header('Location: http://www.MYSITE/home.php' );
    } else {
         
    header('Location: http://www.MYSITE/public-home.php' );
         }
    ?> 
    If the user is logged in, they will be taken to "home.php" which has news articles.

    If they are not logged in, then they will be taken to "public-home.php" which has a general statement and sign-up.
  9. glrfcentral

    glrfcentral New Member

    Joined:
    Mar 19, 2007
    Messages:
    27
    Hoping you can refresh my memory on this. I think I had this in place but then I changed the header and all was lost.

    I have two pages that are basically structure as such:
    1. home - guest landing page
    2. home - member page

    Should I put the above code between head tags of the (2) home - member page or at the start of the page?

    I get a infinite loop error with that because I also have this in the header:

    <?php
    session_start();
    if ($au=$_SESSION['_amember_user']){ // user is logged-in
    print "<font color=#ffffff>Hello $au[login] !</font><br>";
    print "<a href='/amember/logout.php'><font color=#ffff99>Logout</a>";
    } else { // user is not logged-in
    print "<form method=post action='/amember/login.php'>
    <font color=#ffffff>Username: </font><input type=text name=amember_login size=9><br>
    <font color=#ffffff>Password: </font><input type=password name=amember_pass size=9><br>
    <input type=submit value='Login'>
    </form>";
    print "<a href='/amember/signup.php'><font color=#ffffff>Register</font></a></br><a href='/amember/member.php'><font color=#ffffff>Forgot password?</font></a>
    ";
    }
    ?>

    Someone helped me with this before and I just don't remember how we sorted this out.

    Thanks,

    Brian
  10. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Brian,
    Code should be placed at the top of your home page.
    Create home-redirect.php script just with above code.
    also On your site point all "home" links to home-redirect.php

Share This Page