MyBB Community Forums

Full Version: Redirecting the user to a particular page after login
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear friends,

I have searched a lot and I didn't find anything related to this. Can someone suggest me starting from the scratch (if possible), how to redirect a user to a particular page after he/she logs in to the forum rather than to the index page. I want to add a rules and disclaimer page and the user has to read agree for the first login and from the second login user can be directly sent to index page.

In case of any change in the rules and the user has to be redirected to disclaimer page.

I am using the latest myBB software for my forum i.e. 1.6.x. Kindly explain based on the latest version.

Thanks in advance
Leader
Oh really..

http://www.google.com/search?sourceid=ch...fter+login

I see plenty. Just look for one it may not be mybb but it is redirecting. Sometimes you have to put it in a more general statment.
(2011-09-10, 04:22 AM)Tyler K Wrote: [ -> ]Oh really..

http://www.google.com/search?sourceid=ch...fter+login

I see plenty. Just look for one it may not be mybb but it is redirecting. Sometimes you have to put it in a more general statment.

/facepalm
They're not for MyBB, are they?
(2011-09-10, 04:50 AM)Jordan L. Wrote: [ -> ]
(2011-09-10, 04:22 AM)Tyler K Wrote: [ -> ]Oh really..

http://www.google.com/search?sourceid=ch...fter+login

I see plenty. Just look for one it may not be mybb but it is redirecting. Sometimes you have to put it in a more general statment.

/facepalm
They're not for MyBB, are they?

Does not matter. You can take that general idea.

Considering the top results (which are what people look at / for) are:

Wordpress:
<?php
add_action('login_form', 'redirect_after_login');

function redirect_after_login() {
	global $redirect_to;
	if (!isset($_GET['redirect_to'])) {
		$redirect_to = get_option('siteurl');
	}
}
?>

Drupal:
/**
 * Implementation of hook_form_alter().
 */
function mymodulename_form_alter($form_id, &$form) {
  if ($form_id == 'user_login_block' || $form_id == 'user_login') {
    $form['#action'] = mymodulename_get_destination();
  }
}

/** 
 * Change the $path to the node location you want the user to
 * go to after logging in.
 */
function mymodulename_get_destination() {
  global $user;
  if (in_array('admin user',$user->roles)) {
     $path = 'node/11';  # admin area
  }
  else {
     $path = 'node/12';  # user area
  }
  return url('', "destination=$path");
}

and ASP.Net:
   protected void Login1_LoginError(object sender, EventArgs e)
    {
        //There was a problem logging in the user

        //See if this user exists in the database 
        MembershipUser userInfo = Membership.GetUser(Login1.UserName);

        if (userInfo == null)
        {
            //The user entered an invalid username...

            Login1.FailureText = "There is no user in the database with the username " + Login1.UserName;
        }
        else
        {
            //See if the user is locked out or not approved 
            if (!userInfo.IsApproved)
            {

                Login1.FailureText = "Your account has not yet been approved by the site's administrators. Please try again later...";
            }
            else if (userInfo.IsLockedOut)
            {
                Login1.FailureText = "Your account has been locked out because of a maximum number of incorrect login attempts. You will NOT be able to login until you contact a site administrator and have your account unlocked.";
            }
            else
            {
                //The password was incorrect (don't show anything, the Login control already describes the problem) 
                Login1.FailureText = string.Empty;
            }
        }

    }


    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        TextBox TextBox1 = (TextBox)Login1.FindControl("UserName");
        //MembershipUser user = Membership.GetUser(TextBox1.Text);
        MembershipUser user = Membership.GetUser(Login1.UserName);

        if (Request.QueryString["ReturnUrl"] != null)
        {
            Login1.DestinationPageUrl=Request.QueryString["ReturnUrl"].ToString();
        }
        else
        {

            //-- check if login user in Admin role
            if (Roles.IsUserInRole(TextBox1.Text, "Admin"))
            {
                Login1.DestinationPageUrl="~/Admin/Default.aspx";

            }
            //-- check if login user in User role
            else if (Roles.IsUserInRole(TextBox1.Text, "User"))
            {
                Login1.DestinationPageUrl="~/User/Default.aspx";
            }
        }
        
    }

Yeah, I'd say it does matter.
(2011-09-10, 04:22 AM)Tyler K Wrote: [ -> ]Oh really..

http://www.google.com/search?sourceid=ch...fter+login

I see plenty. Just look for one it may not be mybb but it is redirecting. Sometimes you have to put it in a more general statment.

I am sorry if I post anything in my previous thread. But the only thing is when I searched for myBB forum I have found a plugin link somewhere. Unfortunately its a dead link.

And I must say I am a new bie for the programming, that is why I have requested you start from scratch.

Thanks for replying and request you to help in creating that redirect link if possible.
(2011-09-10, 01:38 PM)Jordan L. Wrote: [ -> ]Considering the top results (which are what people look at / for) are:

Wordpress:
<?php
add_action('login_form', 'redirect_after_login');

function redirect_after_login() {
	global $redirect_to;
	if (!isset($_GET['redirect_to'])) {
		$redirect_to = get_option('siteurl');
	}
}
?>

Drupal:
/**
 * Implementation of hook_form_alter().
 */
function mymodulename_form_alter($form_id, &$form) {
  if ($form_id == 'user_login_block' || $form_id == 'user_login') {
    $form['#action'] = mymodulename_get_destination();
  }
}

/** 
 * Change the $path to the node location you want the user to
 * go to after logging in.
 */
function mymodulename_get_destination() {
  global $user;
  if (in_array('admin user',$user->roles)) {
     $path = 'node/11';  # admin area
  }
  else {
     $path = 'node/12';  # user area
  }
  return url('', "destination=$path");
}

and ASP.Net:
   protected void Login1_LoginError(object sender, EventArgs e)
    {
        //There was a problem logging in the user

        //See if this user exists in the database 
        MembershipUser userInfo = Membership.GetUser(Login1.UserName);

        if (userInfo == null)
        {
            //The user entered an invalid username...

            Login1.FailureText = "There is no user in the database with the username " + Login1.UserName;
        }
        else
        {
            //See if the user is locked out or not approved 
            if (!userInfo.IsApproved)
            {

                Login1.FailureText = "Your account has not yet been approved by the site's administrators. Please try again later...";
            }
            else if (userInfo.IsLockedOut)
            {
                Login1.FailureText = "Your account has been locked out because of a maximum number of incorrect login attempts. You will NOT be able to login until you contact a site administrator and have your account unlocked.";
            }
            else
            {
                //The password was incorrect (don't show anything, the Login control already describes the problem) 
                Login1.FailureText = string.Empty;
            }
        }

    }


    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        TextBox TextBox1 = (TextBox)Login1.FindControl("UserName");
        //MembershipUser user = Membership.GetUser(TextBox1.Text);
        MembershipUser user = Membership.GetUser(Login1.UserName);

        if (Request.QueryString["ReturnUrl"] != null)
        {
            Login1.DestinationPageUrl=Request.QueryString["ReturnUrl"].ToString();
        }
        else
        {

            //-- check if login user in Admin role
            if (Roles.IsUserInRole(TextBox1.Text, "Admin"))
            {
                Login1.DestinationPageUrl="~/Admin/Default.aspx";

            }
            //-- check if login user in User role
            else if (Roles.IsUserInRole(TextBox1.Text, "User"))
            {
                Login1.DestinationPageUrl="~/User/Default.aspx";
            }
        }
        
    }

Yeah, I'd say it does matter.

Thanks for sparing your valuable time.

Can you help me by providing code for myBB forum.

Thanks in advance.
Can you guys please help on this.