Jump to content

Recommended Posts

Hey guys, hope you can help me.

 

I want to make a access level in my cms that I have made , its pretty basic, its my testing platfrom to develop my php skills.

 

Its got 3 levels Admin , Client  and Guest.

 

I have look around google, but didn't find anything that will help me a lot.

 

So far i know , i need to use "if/else"  with redirect.

 

so if client log in, it will redirect, to client.php

same for quest, but not for admin.

 

Well I hope some one can give me some directions.

Thank you

 

Link to comment
https://forums.phpfreaks.com/topic/145918-user-access-level/
Share on other sites

I have session and also i got function for check if logged in.

so now i need is the "if/else" to redirect.

Would that change anything on the php code you inlcude in?

 

 

if($user == admin)  header('Location: http://www.whatever1.com/');

exit;

 

if($user == client)  header('Location: http://www.whatever2.com/');

exit;

 

if($user == guest)  header('Location: http://www.whatever3.com/');

exit;

 

Link to comment
https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766167
Share on other sites

if($user == admin)  header('Location: http://www.whatever1.com/');

exit;

 

if($user == client)  header('Location: http://www.whatever2.com/');

exit;

 

if($user == guest)  header('Location: http://www.whatever3.com/');

exit;

 

 

That is very redundant.

 

$levelRedirect = array("admin" => "http://yoursite.com/admin", "user" => "http://yoursite.com/user", "guest" => "http://yoursite.com/guest");

if (!in_array($userLevel, $levelRedirect)) 
    $userLevel = "guest";

header ('Location: ' . $levelRedirect[$userLevel]);

 

A bit cleaner with less code :)

Link to comment
https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766237
Share on other sites

if($user == admin)  header('Location: http://www.whatever1.com/');

exit;

 

if($user == client)  header('Location: http://www.whatever2.com/');

exit;

 

if($user == guest)  header('Location: http://www.whatever3.com/');

exit;

 

 

That is very redundant.

 

$levelRedirect = array("admin" => "http://yoursite.com/admin", "user" => "http://yoursite.com/user", "guest" => "http://yoursite.com/guest");

if (!in_array($userLevel, $levelRedirect)) 
    $userLevel = "guest";

header ('Location: ' . $levelRedirect[$userLevel]);

 

A bit cleaner with less code :)

 

Redundant? yes....cleaner? that's perspective....

 

For a newbie, it's easier for them to understand the code I wrote as it's function is in your face...

 

Your code is good and I like it, but could confuse newbies..and requires knowledge of arrays...

 

 

Link to comment
https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766244
Share on other sites

I am not trying to get into a pissing match at all. But in all honesty, the code you gave him is just poor code. Why do 3 ifs like that, when you can do if/elseif/else ?

 

Even to try and "show", you still should show them the proper way, not some hodged podged way that only a newbie would use (sorry no offense intended).

 

if ($user == 'admin') {
    header('Location: http://www.whatever1.com/');
elseif ($user == 'client') {
    header('Location: http://www.whatever2.com/');
}else {
    header('Location: http://www.whatever3.com/');
}
exit;

 

You also left off quotes around the variable checking, which does throw a NOTICE undefined constant error (not major but it is better practice to encapsulate the non-constant string in quotes).

 

Like I said, my intentions were not to put you down.

 

Another variation, just because I feel like it, is they could also use a switch:

switch($user) {
    case 'admin':
        header('Location: http://www.whatever1.com/');
    break;
    case 'client':
        header('Location: http://www.whatever2.com/');
    break;
    case 'guest':
    default:
        header('Location: http://www.whatever3.com/');
    break;
}

 

Now granted my initial way, I prefer to do cause it is easier for me, and I do understand that it can be confusing for a new user and yes I probably should of just written an else/if or a case/switch instead.

 

It seems like you were more worried about being #1 to post than posting the proper method. I do that too. Just kinda making you aware of it. Showing them improper coding does not help them, just like me posting way advanced stuff that they might not understand.

 

 

Link to comment
https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766286
Share on other sites

I am not trying to get into a pissing match at all. But in all honesty, the code you gave him is just poor code. Why do 3 ifs like that, when you can do if/elseif/else ?

 

Even to try and "show", you still should show them the proper way, not some hodged podged way that only a newbie would use (sorry no offense intended).

 

if ($user == 'admin') {
    header('Location: http://www.whatever1.com/');
elseif ($user == 'client') {
    header('Location: http://www.whatever2.com/');
}else {
    header('Location: http://www.whatever3.com/');
}
exit;

 

You also left off quotes around the variable checking, which does throw a NOTICE undefined constant error (not major but it is better practice to encapsulate the non-constant string in quotes).

 

Like I said, my intentions were not to put you down.

 

Another variation, just because I feel like it, is they could also use a switch:

switch($user) {
    case 'admin':
        header('Location: http://www.whatever1.com/');
    break;
    case 'client':
        header('Location: http://www.whatever2.com/');
    break;
    case 'guest':
    default:
        header('Location: http://www.whatever3.com/');
    break;
}

 

Now granted my initial way, I prefer to do cause it is easier for me, and I do understand that it can be confusing for a new user and yes I probably should of just written an else/if or a case/switch instead.

 

It seems like you were more worried about being #1 to post than posting the proper method. I do that too. Just kinda making you aware of it. Showing them improper coding does not help them, just like me posting way advanced stuff that they might not understand.

 

 

 

 

Sure no problem. I am always for learning better techniques. Yes I wanted to get him the answer as soon as possible.

 

The if/elseif/else would of course work which utilizes just 1 "exit" function...like I said, there's a lot of ways to do it...

 

The way I wrote it is just one method. The extra "exit"'s that I put in of course was to make sure that none of the

other header redirects would run....

 

 

Link to comment
https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766308
Share on other sites

if($user == admin)  header('Location: http://www.whatever1.com/');

exit;

 

if($user == client)  header('Location: http://www.whatever2.com/');

exit;

 

if($user == guest)  header('Location: http://www.whatever3.com/');

exit;

 

 

That is very redundant.

 

$levelRedirect = array("admin" => "http://yoursite.com/admin", "user" => "http://yoursite.com/user", "guest" => "http://yoursite.com/guest");

if (!in_array($userLevel, $levelRedirect)) 
    $userLevel = "guest";

header ('Location: ' . $levelRedirect[$userLevel]);

 

A bit cleaner with less code :)

 

 

$levelRedirect = array('admin' => 'http://yoursite.com/admin', 'user' => 'http://yoursite.com/user', 'guest' => 'http://yoursite.com/guest');

if (!in_array($userLevel, $levelRedirect)) 
    $userLevel = 'guest';

header ('Location: '.$levelRedirect[$userLevel]);

*cough*

Single quotes are (yes marginally) faster than double quotes due to php not having to see if it needs to replace a variable (almost other things).

Link to comment
https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766331
Share on other sites

*cough*

Single quotes are (yes marginally) faster than double quotes due to php not having to see if it needs to replace a variable (almost other things).

 

*ehrmmmemmmrmmm clears throat erhmhmmmm*

Wow you must really be hurting to post something huh?

 

lmao. That is just too funny. Yes, I know they are faster. In order to see a difference, however, you have to loop through about 50,000 rows to get a benchmark that you would even be able to notice a difference.

 

But notice, I did not say "more efficient" only "A bit cleaner with less code".

Link to comment
https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766338
Share on other sites

*cough*

Single quotes are (yes marginally) faster than double quotes due to php not having to see if it needs to replace a variable (almost other things).

 

*ehrmmmemmmrmmm clears throat erhmhmmmm*

Wow you must really be hurting to post something huh?

 

lmao. That is just too funny. Yes, I know they are faster. In order to see a difference, however, you have to loop through about 50,000 rows to get a benchmark that you would even be able to notice a difference.

 

But notice, I did not say "more efficient" only "A bit cleaner with less code".

 

Well I need some kind of distraction from work ;)

Link to comment
https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766343
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.