differentiate admin and users with session php
I successfully made a PHP login system with session but I need to differentiate the users to their respective role such as admin and users and direct them to their respective page. I already made a column in my database entitled 'user_role'..
here's my code..
login_inc.php
<?php
session_start();
require('dbconnect.php');
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($conn,$_POST['username']);
$mypassword = mysqli_real_escape_string($conn,$_POST['password']);
$sql = "SELECT userID FROM users WHERE username = '$myusername' && password = '$mypassword'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// $active = $row['active'];
$count = mysqli_num_rows($result);
if($count == 1)
{
$_SESSION['login_user'] = $myusername;
// header("Location: ../index.php");
}
else
{
alert('Invalid Username or Password');
header("Refresh: 1,../index.php");
die();
}
}
?>
php
add a comment |
I successfully made a PHP login system with session but I need to differentiate the users to their respective role such as admin and users and direct them to their respective page. I already made a column in my database entitled 'user_role'..
here's my code..
login_inc.php
<?php
session_start();
require('dbconnect.php');
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($conn,$_POST['username']);
$mypassword = mysqli_real_escape_string($conn,$_POST['password']);
$sql = "SELECT userID FROM users WHERE username = '$myusername' && password = '$mypassword'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// $active = $row['active'];
$count = mysqli_num_rows($result);
if($count == 1)
{
$_SESSION['login_user'] = $myusername;
// header("Location: ../index.php");
}
else
{
alert('Invalid Username or Password');
header("Refresh: 1,../index.php");
die();
}
}
?>
php
You should have to maintain user type in database field & same type when user logged in maintain in session.
– Gajanan Kolpuke
Nov 16 '18 at 6:23
1
Just fetch data from user_role column and set that type in session same as login_user.
– Gajanan Kolpuke
Nov 16 '18 at 6:25
@GajananKolpuke.. I already done that but I've got an error that the $user_role variable for my user type is unidentified?
– Mark Dexter
Nov 16 '18 at 6:37
undefined rather
– Mark Dexter
Nov 16 '18 at 6:53
add a comment |
I successfully made a PHP login system with session but I need to differentiate the users to their respective role such as admin and users and direct them to their respective page. I already made a column in my database entitled 'user_role'..
here's my code..
login_inc.php
<?php
session_start();
require('dbconnect.php');
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($conn,$_POST['username']);
$mypassword = mysqli_real_escape_string($conn,$_POST['password']);
$sql = "SELECT userID FROM users WHERE username = '$myusername' && password = '$mypassword'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// $active = $row['active'];
$count = mysqli_num_rows($result);
if($count == 1)
{
$_SESSION['login_user'] = $myusername;
// header("Location: ../index.php");
}
else
{
alert('Invalid Username or Password');
header("Refresh: 1,../index.php");
die();
}
}
?>
php
I successfully made a PHP login system with session but I need to differentiate the users to their respective role such as admin and users and direct them to their respective page. I already made a column in my database entitled 'user_role'..
here's my code..
login_inc.php
<?php
session_start();
require('dbconnect.php');
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($conn,$_POST['username']);
$mypassword = mysqli_real_escape_string($conn,$_POST['password']);
$sql = "SELECT userID FROM users WHERE username = '$myusername' && password = '$mypassword'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// $active = $row['active'];
$count = mysqli_num_rows($result);
if($count == 1)
{
$_SESSION['login_user'] = $myusername;
// header("Location: ../index.php");
}
else
{
alert('Invalid Username or Password');
header("Refresh: 1,../index.php");
die();
}
}
?>
php
php
edited Nov 16 '18 at 7:40
Sanu0786
550715
550715
asked Nov 16 '18 at 6:16
Mark DexterMark Dexter
814
814
You should have to maintain user type in database field & same type when user logged in maintain in session.
– Gajanan Kolpuke
Nov 16 '18 at 6:23
1
Just fetch data from user_role column and set that type in session same as login_user.
– Gajanan Kolpuke
Nov 16 '18 at 6:25
@GajananKolpuke.. I already done that but I've got an error that the $user_role variable for my user type is unidentified?
– Mark Dexter
Nov 16 '18 at 6:37
undefined rather
– Mark Dexter
Nov 16 '18 at 6:53
add a comment |
You should have to maintain user type in database field & same type when user logged in maintain in session.
– Gajanan Kolpuke
Nov 16 '18 at 6:23
1
Just fetch data from user_role column and set that type in session same as login_user.
– Gajanan Kolpuke
Nov 16 '18 at 6:25
@GajananKolpuke.. I already done that but I've got an error that the $user_role variable for my user type is unidentified?
– Mark Dexter
Nov 16 '18 at 6:37
undefined rather
– Mark Dexter
Nov 16 '18 at 6:53
You should have to maintain user type in database field & same type when user logged in maintain in session.
– Gajanan Kolpuke
Nov 16 '18 at 6:23
You should have to maintain user type in database field & same type when user logged in maintain in session.
– Gajanan Kolpuke
Nov 16 '18 at 6:23
1
1
Just fetch data from user_role column and set that type in session same as login_user.
– Gajanan Kolpuke
Nov 16 '18 at 6:25
Just fetch data from user_role column and set that type in session same as login_user.
– Gajanan Kolpuke
Nov 16 '18 at 6:25
@GajananKolpuke.. I already done that but I've got an error that the $user_role variable for my user type is unidentified?
– Mark Dexter
Nov 16 '18 at 6:37
@GajananKolpuke.. I already done that but I've got an error that the $user_role variable for my user type is unidentified?
– Mark Dexter
Nov 16 '18 at 6:37
undefined rather
– Mark Dexter
Nov 16 '18 at 6:53
undefined rather
– Mark Dexter
Nov 16 '18 at 6:53
add a comment |
1 Answer
1
active
oldest
votes
You must create another variable in database that assume 0 or 1. In case 0 Is normal user, One is admin.
Add in SQL query the new field and Add another if for check the value privilege .
$privilege = mysql_result($result,"privilege")
I already got my user_role column in database. and tried this code
– Mark Dexter
Nov 16 '18 at 6:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332423%2fdifferentiate-admin-and-users-with-session-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You must create another variable in database that assume 0 or 1. In case 0 Is normal user, One is admin.
Add in SQL query the new field and Add another if for check the value privilege .
$privilege = mysql_result($result,"privilege")
I already got my user_role column in database. and tried this code
– Mark Dexter
Nov 16 '18 at 6:48
add a comment |
You must create another variable in database that assume 0 or 1. In case 0 Is normal user, One is admin.
Add in SQL query the new field and Add another if for check the value privilege .
$privilege = mysql_result($result,"privilege")
I already got my user_role column in database. and tried this code
– Mark Dexter
Nov 16 '18 at 6:48
add a comment |
You must create another variable in database that assume 0 or 1. In case 0 Is normal user, One is admin.
Add in SQL query the new field and Add another if for check the value privilege .
$privilege = mysql_result($result,"privilege")
You must create another variable in database that assume 0 or 1. In case 0 Is normal user, One is admin.
Add in SQL query the new field and Add another if for check the value privilege .
$privilege = mysql_result($result,"privilege")
edited Nov 16 '18 at 6:38
answered Nov 16 '18 at 6:33
theantomctheantomc
1299
1299
I already got my user_role column in database. and tried this code
– Mark Dexter
Nov 16 '18 at 6:48
add a comment |
I already got my user_role column in database. and tried this code
– Mark Dexter
Nov 16 '18 at 6:48
I already got my user_role column in database. and tried this code
– Mark Dexter
Nov 16 '18 at 6:48
I already got my user_role column in database. and tried this code
– Mark Dexter
Nov 16 '18 at 6:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332423%2fdifferentiate-admin-and-users-with-session-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You should have to maintain user type in database field & same type when user logged in maintain in session.
– Gajanan Kolpuke
Nov 16 '18 at 6:23
1
Just fetch data from user_role column and set that type in session same as login_user.
– Gajanan Kolpuke
Nov 16 '18 at 6:25
@GajananKolpuke.. I already done that but I've got an error that the $user_role variable for my user type is unidentified?
– Mark Dexter
Nov 16 '18 at 6:37
undefined rather
– Mark Dexter
Nov 16 '18 at 6:53