...

View Full Version : php Login redirect based on certain criteria



nevblanc79
01-16-2012, 07:48 PM
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.


<?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

BluePanther
01-16-2012, 10:04 PM
Location headers are what you want. Specifically:


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");
}

nevblanc79
01-17-2012, 01:48 PM
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

BluePanther
01-17-2012, 01:52 PM
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_Reference/wp_redirect

mlseim
01-17-2012, 02:04 PM
And here is how you can check for the actual username:
http://codex.wordpress.org/Function_Reference/get_currentuserinfo

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


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


.

nevblanc79
01-17-2012, 02:31 PM
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


<?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

mlseim
01-17-2012, 05:15 PM
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.


.

nevblanc79
01-17-2012, 08:15 PM
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

mlseim
01-17-2012, 08:25 PM
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/21/how-to-using-wp_redirect-without-getting-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?

nevblanc79
01-17-2012, 08:54 PM
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

mlseim
01-17-2012, 10:38 PM
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?

nevblanc79
01-18-2012, 09:43 AM
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

mlseim
01-18-2012, 12:13 PM
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".

nevblanc79
01-18-2012, 12:39 PM
The page is one I created and the id is 353.

It is a page not a post.

nevblanc79

mlseim
01-18-2012, 02:25 PM
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/uploads/2008/08/wplogin_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_Capabilities#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.


.

nevblanc79
01-18-2012, 03:11 PM
Hi there,

Based on your instructions my file looks like this. i understand i need to alter the url and the specific access level.

Just want to make sure its right prior to uploading.

Thanks,

nevblanc79


<?php
/*
Plugin Name: Redirect users to different locations after logging in
Plugin URI: http://www.theblog.ca/wplogin-front-page
Description: Redirect users to different locations after logging in. Define a set of rules for specific users, user with specific roles, users with specific capabilities, and a blanket rule for all other users.
Author: Peter
Version: 1.1
Author URI: http://www.theblog.ca
*/

/*
--------------
Config section
--------------
*/


/*
--------------
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_Capabilities#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/';

/*
------------------------
Code begins, config over
------------------------
*/


// Thanks to http://wordpress.org/support/topic/97314 for this function

function redirect_current_user_can($capability, $current_user) {
global $wpdb;

$roles = get_option($wpdb->prefix . 'user_roles');
$user_roles = $current_user->{$wpdb->prefix . 'capabilities'};
$user_roles = array_keys($user_roles, true);
$role = $user_roles[0];
$capabilities = $roles[$role]['capabilities'];

if ( in_array( $capability, array_keys( $capabilities, true) ) ) {
// check array keys of capabilities for match against requested capability
return true;
}
return false;
}

function redirect_to_front_page( $user_login ) {
global $wpdb, $redirect_to, $redirect_per_user, $redirect_by_role, $redirect_by_level, $redirect_all_users;

if ( isset( $redirect_per_user[$user_login] ) ) {
$redirect_to = $redirect_per_user[$user_login];
}
else {
$user = get_userdatabylogin($user_login);

if ($redirect_by_role) {
foreach ( $redirect_by_role as $role => $redirect_url ) {
if ( isset ( $user->{$wpdb->prefix . 'capabilities'}[$role] ) ) {
$redirect_to = $redirect_url;
return false;
}
}
}

if ($redirect_by_level) {
foreach ( $redirect_by_level as $level => $redirect_url ) {
if ( redirect_current_user_can ( $level, $user ) ) {
$redirect_to = $redirect_url;
return false;
}
}
}

if ($redirect_all_users) {
$redirect_to = $redirect_all_users;
}
}
}

add_action('wp_login', 'redirect_to_front_page');
?>

nevblanc79
01-18-2012, 03:17 PM
Based on your instructions, this is the file i've prepared. Is this right. I understand I need to correct the access level code and add the correct url.

Just want to check its right prior to uploading


<?php
/*
Plugin Name: Redirect users to different locations after logging in
Plugin URI: http://www.theblog.ca/wplogin-front-page
Description: Redirect users to different locations after logging in. Define a set of rules for specific users, user with specific roles, users with specific capabilities, and a blanket rule for all other users.
Author: Peter
Version: 1.1
Author URI: http://www.theblog.ca
*/

/*
--------------
Config section
--------------
*/


/*
--------------
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_Capabilities#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/';

/*
------------------------
Code begins, config over
------------------------
*/


// Thanks to http://wordpress.org/support/topic/97314 for this function

function redirect_current_user_can($capability, $current_user) {
global $wpdb;

$roles = get_option($wpdb->prefix . 'user_roles');
$user_roles = $current_user->{$wpdb->prefix . 'capabilities'};
$user_roles = array_keys($user_roles, true);
$role = $user_roles[0];
$capabilities = $roles[$role]['capabilities'];

if ( in_array( $capability, array_keys( $capabilities, true) ) ) {
// check array keys of capabilities for match against requested capability
return true;
}
return false;
}

function redirect_to_front_page( $user_login ) {
global $wpdb, $redirect_to, $redirect_per_user, $redirect_by_role, $redirect_by_level, $redirect_all_users;

if ( isset( $redirect_per_user[$user_login] ) ) {
$redirect_to = $redirect_per_user[$user_login];
}
else {
$user = get_userdatabylogin($user_login);

if ($redirect_by_role) {
foreach ( $redirect_by_role as $role => $redirect_url ) {
if ( isset ( $user->{$wpdb->prefix . 'capabilities'}[$role] ) ) {
$redirect_to = $redirect_url;
return false;
}
}
}

if ($redirect_by_level) {
foreach ( $redirect_by_level as $level => $redirect_url ) {
if ( redirect_current_user_can ( $level, $user ) ) {
$redirect_to = $redirect_url;
return false;
}
}
}

if ($redirect_all_users) {
$redirect_to = $redirect_all_users;
}
}
}

add_action('wp_login', 'redirect_to_front_page');
?>

mlseim
01-18-2012, 03:30 PM
Yes, just alter this line (as you stated):
$redirect_by_level['level_1'] = 'http://yoursite.com/';

Make the script "index.php" and save into a new directory you made.
That directory (with it's one file) gets uploaded into the plug-ins directory on WP.
When you view the plug-ins using WP admin, you should see the plug in there.
Then you have to activate it.

If things so south, you can always delete the plug-in.


.

nevblanc79
01-18-2012, 07:46 PM
Hi there

Nothing happened at all.

Any other ideas? Really appreciate your help on this.

This is really frustrating!!!

nevblanc79

karlos81
01-21-2012, 08:44 AM
Here's how i did it, but it's not a wp site, its for a web app...

First, add a field in your database table 'users', titiled 'type'
Lets assume you have 3 users and they are in your 'users' table with their respectable username, password and type. Now, you set a type for each user, lets say user: ivan type: administrator When you have that in your db, check user types from your login form input.
In my database, user type field is called 'tip' and user types are 'administrator', 'ucenik', 'profesor'.
I do it like this:



if ($dbfound){
$SQL="SELECT * FROM korisnici WHERE user='$uname' AND pass='$pword'";
$result=mysql_query($SQL);

if ($result){
$num_rows=mysql_num_rows($result);

if ($num_rows==1){
$usertype=mysql_fetch_assoc($result);

if($usertype['tip']=='administrator'){
session_start();
$_SESSION['login']='1';
$_SESSION['user']=$_POST['username'];
header("Location: ../view/profesorr.php");
}
else if($usertype['tip']!=='administrator'&&$usertype['tip']!=='ucenik'){
session_start();
$_SESSION['login']='2';
$_SESSION['user']=$_POST['username'];
header("Location: ../view/profesor.php");
}
else if($usertype['tip']!=='administrator'&&$usertype['tip']!=='profesor'){
session_start();
$_SESSION['login']='3';
$_SESSION['user']=$_POST['username'];
header("Location: ../view/ucenik.php");
}
}

else{
session_start();
$_SESSION['login']='';
print('Neregistrovan korisnik');
}

So, what u do is check for a user type and if true, assign a session id to it, which you then include in your loginCheck script for the pages you need that user to be able to access.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum