tags:

views:

44

answers:

4

I have a php login page with session control. It routes a normal user to info.php and an admin user to info_admin.php. If I login with a "normal" user it goes to info.php, however, in the address bar, I can go to info_admin.php and it doesn't kick me out, gives me access. How can I control this or prevent the user from doing this manually?

For info, I'm using this script: http://php-login-script.com/

Thanks very much!

+2  A: 

I quickly scanned through the login code, it seems to be setting a variable $_SESSION['user_level'] when the user first logs in.

To allow only user with level 2, for example, put this at the top of your page. It should redirect anyone who is not a level 2 user back to the login page.

<?php

if (isset($_SESSION['user_level'] && $_SESSION['user_level'] == 2) {

?>


<!-- Your page HTML here -->


<?php

} else {
    header('Location: login.php');
}

?>
Lotus Notes
Lotus, I have that at the top of EVERY page, but it is only checking that the user is logged in. It's not checking user level
BigMike
Ok, edited. Hopefully this should work.
Lotus Notes
This is cool, but why put all of your HTML code between an if...else when you can just change the header if they fail authentication at the top of the file? This will make for much easier to read code down the line.
Liam
@Liam Yes, thanks. I also forgot to call exit() or die() after header() which is good practice.
Lotus Notes
+1  A: 

The high level approach is that upon login, store the user's access level in the session or in a database. At each page call, check against that value.

Andrew Sledge
+1  A: 

Just add an extra function that checks user level--if it's at or above the desired level, return true. If not, return false and failover. Then in your page you want to protect, fire the function with the desired level of protection.

For instance:

function checkPerms($level) {
  if ($level >= number) {
   return true
  } else {
    failover here
  }
}

then call in your pages

checkperms(5);

EDIT: If you were really slick, you could just add an extra param to your original function call with the user level, defaulted to the lowest value. Then, if the user validates as registered, it could check the user level at the same time.

bpeterson76
+3  A: 

Just to make Lotus Notes' code a bit more compact:

<?php
if (!isset($_SESSION['user_level']) || ($_SESSION['user_level'] != 2)) {
    header('Location: login.php');
    exit;
}
?>

<!-- Your page HTML here -->
Mike C
I'm using this code, but it's not showing the page. I if (!isset($_SESSION['user_level'] || $_SESSION['user_level'] != admin)
BigMike
Sorry there was an error in the code. I've fixed it.
Mike C
Mike, i just did a echo $_SESSION['user_level']; and it's not showing. But I know its working because the protect_page() function is using Session control. Any reason this coule be failing?
BigMike
Are you calling session_start(); beforehand? You should.
Mike C
Yes, I figured it out. Thanks very much, your solution is perfect.
BigMike