Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts

    php Login redirect based on certain criteria

    Hi.

    I am in the process of creating a membership site and am having difficulty refining the login process.

    Visitors to the site have two different levels of access and I want the user to be directed to the relevant page based on their assigned user level.

    Code:
    <?php if (current_user_can("access_level1")){ ?>
        redirect to relevant page
    <?php } else { ?>
        send to another page.
    <?php } ?>
    I am unsure about the correct code for the redirect to the right url. I'm assuming it needs to be html??

    Could anyone help?

    Thanks,

    nevblanc79

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Location headers are what you want. Specifically:
    PHP Code:
    if (current_user_can("access_level1")){
        
    header("Location: http://www.domain.com/some_page.php");
    } else {
        
    header("Location: http://www.domain.com/another_page.php");

    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    New Coder
    Join Date
    Jun 2011
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi there,

    thanks for getting back on this.

    Unfortunately, I'm getting an error saying something along the lines of the header already being passed. I have seen this elsewhere.

    It is a wordpress site and I have installed the exec-php plugin to allow php in pages, however nothing seems to be working.

    do you have any other ideas?

    thanks,

    nevblanc79

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by nevblanc79 View Post
    Hi there,

    thanks for getting back on this.

    Unfortunately, I'm getting an error saying something along the lines of the header already being passed. I have seen this elsewhere.

    It is a wordpress site and I have installed the exec-php plugin to allow php in pages, however nothing seems to be working.

    do you have any other ideas?

    thanks,

    nevblanc79
    I don't get this. If you're making a wordpress plugin, you'll be using PHP to build it?

    For wordpress, you'll probably want this function:
    http://codex.wordpress.org/Function_...ce/wp_redirect
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    And here is how you can check for the actual username:
    http://codex.wordpress.org/Function_...urrentuserinfo

    This is the CODEX you are currently utilizing ... and you are correct in using it:
    http://codex.wordpress.org/Function_...rrent_user_can


    I agree the WP CODEX is the way you should be customizing your theme.


    .

  • #6
    New Coder
    Join Date
    Jun 2011
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you both.

    I am not developing a plugin, I am trying to use conditional statements within a theme's page to redirect based on a user level. It is a membership plugin and I have had difficulty getting help through their forum hence the reason I am here. I am using a lightweight plugin to allow php code to be inserted into pages

    If I was to put the wp_redirect function in amongst the conditional statement like this, do you think it would work? Should I also be adding the php before the html starts at the top of the page or does that not make a difference?

    I am new hence the potentially stupid questions

    Code:
    <?php if (current_user_can("access_s2member_level1")){ ?>
        php wp_redirect( home_url() ); exit;
    <?php } else { ?>
        <?php wp_redirect( another_url() ); exit; ?>
    <?php } ?>
    thanks in anticipation.

    nevblanc79

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I think you're on the right track ... but I didn't test it.
    You do have some syntax issues ... maybe more like this?


    <?php if (current_user_can("access_s2member_level1")){
    wp_redirect( home_url() ); exit;
    }else{
    wp_redirect( another_url() ); exit;
    } ?>

    It's all PHP, so you don't need to open <? and close ?> it with each line.


    .

  • #8
    New Coder
    Join Date
    Jun 2011
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi again,

    Thought I was onto something there, however I am getting an error saying Cannot modify header information headers have already been sent in wp-includes/pluggable.php
    Completely lost by this.

    Can you shed any light?

    thanks,

    nevblanc79

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    That happens when some HTML has been outputted before the redirect.

    This is the best explanation I've found:
    http://www.sanisoft.com/blog/2011/02...etting-errors/

    But how you implement that, or what your particular case is, I don't know.
    Can you list pluggable.php for us if you can't get the suggestions above to work?

  • #10
    New Coder
    Join Date
    Jun 2011
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I tried to post pluggable.php but it exceeds the character limit.

    Is there anything specific within it that you need to see?

    I have no idea where to start with the example you provided.

    Sorry,

    nevblanc79

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Let's go back to post 1.
    When is the user supposed to be redirected ... with one particular page,
    or all pages? When should it occur? Maybe you're not even doing it in
    the correct place?

  • #12
    New Coder
    Join Date
    Jun 2011
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Right.

    I am developing a membership site that involves manual verification by admin. When the user registers, they are assigned level 0 access. It is up to the admin team to alter the access rights to level 1.

    So. They login for the first time from the home page and are redirected to a login welcome page. This page explains that their account is awaiting approval (to member level 1). This page is set as the default login welcome once anyone signs in. What I want to avoid is users who have had their account approved to level 1 seeing this page. I want them to be redirected straight to the members area page.

    I thought by, incorporating this if else statement into the login welcome page, the user would be directed away if they had level 1 access.

    Am I doing the right thing?

    Thanks for getting back to me on this.

    nevblanc79

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    What is the welcome page id number ... is it a page you created?
    When you go to admin and view all pages and scroll over it, the bottom left
    of your browser will show the URL .. it has an ID number in that.

    Let me know if that welcome page is a "page" and not a "post".

  • #14
    New Coder
    Join Date
    Jun 2011
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    The page is one I created and the id is 353.

    It is a page not a post.

    nevblanc79

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I found a small simple plug-in that can redirect a user after they login.
    You pick which event to use ... in your case, you pick a redirect based on level.

    This is the script itself:
    http://www.theblog.ca/wp-content/upl...n_redirect.txt

    They commented-out all events, so you have to find the "redirect based on level"
    and copy that line to where it says ...

    Create a new folder called "loginmove" or something like that, and save the
    script as "index.php" in that folder.

    Copy line 47 to line 55 and put in your proper URL to direct to.
    So it looks something like this ... see the two red lines ...

    --------------
    Redirect specific user levels to different URLs

    Example:
    $redirect_by_level['level_8'] = 'http://yoursite.com/specialadminpage.php';

    Note that the first rule that matches is applied
    See http://codex.wordpress.org/Roles_and...s#Capabilities for a full list of capabilities
    --------------
    */

    $redirect_by_level = array(); // Do not touch this line. Enter rules below this line
    $redirect_by_level['level_1'] = 'http://yoursite.com/';



    Save it and upload your folder into the plug-ins directory.
    In WP admin, look at your plug-ins and you'll see this one:
    Redirect users to different locations after logging in

    Activate it and see what happens.

    I didn't try this myself, so I'm hoping it works.


    .


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •