Jump to content

User Levels


metalhead41

Recommended Posts

Hey Guys,

 

I have a small problem I can't figure out.

 

I have a login page, user enters username and password, which logs in fine and goes to another page.

Users can have a couple of different account types e.g. admin, standard. If the user is an admin the next page will show certain extra bits that a standard user can't see.

How do I get either the login page, or the next page to get the user level that is stored in a mysql database?

 

Hope that makes sense.

Link to comment
Share on other sites

<?php
session_start();

$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
include 'library/config.php';
include 'library/opendb.php';

$userId   = $_POST['txtUserId'];
$password = $_POST['txtPassword'];

$sql = "SELECT user_name FROM users WHERE user_name = '$userId' AND user_password = md5('$password')";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());

$_SESSION['username'] = $_POST['txtUserId'];

if (mysql_num_rows($result) == 1) {
	// the user id and password match, 
	// set the session
	$_SESSION['db_is_logged_in'] = true;

	// after login we move to the main page
	header('Location: main.php');
	exit;
} else {
	$errorMessage = 'Sorry, wrong user id / password';
}

include 'library/closedb.php';
}

?>

Link to comment
Share on other sites

You might need to adjust the field name accordingly.

 

<?php
session_start();

$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
include 'library/config.php';
include 'library/opendb.php';

$userId   = $_POST['txtUserId'];
$password = $_POST['txtPassword'];

$sql = "SELECT user_name,level  FROM users WHERE user_name = '$userId' AND user_password = md5('$password')";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());

$_SESSION['username'] = $_POST['txtUserId'];

if (mysql_num_rows($result) == 1) {
                         $row = mysql_fetch_assoc($result);
	// the user id and password match, 
	// set the session
                          $_SESSION['level'] = $row['level'];
	$_SESSION['db_is_logged_in'] = true;

	// after login we move to the main page
	header('Location: main.php');
	exit;
} else {
	$errorMessage = 'Sorry, wrong user id / password';
}

include 'library/closedb.php';
}

?>

Link to comment
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.