Click here to Skip to main content
14,732,983 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi....
i want a small help...
am new to php coding...
i have been desiging a project for my college..
i made a login and registration app... bt i am unable to mix the user and admin code..
can i hav any help..
user table consists of username,email,password
and admin the same things...
please...
and thanks in advance..
the below code is for login of user..
for login of admin i created a new table and inserted he data..
how to create both of the using a same table a one php file..
please help....

What I have tried:


$email = $_POST["email"];
$pass = $_POST["password"];
require "inti.php";


$query = "select * from userinfo where email like '".$email."' and password like '".$pass."';";
$result = mysqli_query($con,$query);

if(mysqli_num_rows($result)>0)
{
$response = array();
$code = "login_true";
$row = mysqli_fetch_array($result);
$name = $row[0];
$message = "login sucess...".$name;
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode(array("server_response"=>$response));

}

else
{
$response = array();
$code = "login_false";
$message = "login failed.....try again..";
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode(array("server_response"=>$response));
}
mysqli_close($con);
?>
Posted
Updated 3-May-16 5:50am
Comments
   
If you are going to differentiate some, you have use authentication — I can see some log-on code. If you have that, probably you have some persistent data layer where you user information is stored, some kind of database. Your code shows that you really use MySQL. And if you have that database, it's up to you what user data you put in user records and how you classify the users. So, what's the problem?
—SA
Dheeraj Cidda 3-May-16 12:15pm
   
actually sir,i am creating a project for my college. i have created a data base and tables for both user and admin and accessing them by using different login pages,like for user login i used one page and for admin login i used another.the code i posted is just simply for login for user in his login page and admin in his login page.now i want to access both of them from a common login page.both user and admin have same attributes for logging in. email and password. please help me.yes i used mysql for writing queries,using them i retrieved records of user
   
Right, you should not have separate login. You identify the user, and then you know what kind of the user is that, from your database. What's the problem?
—SA
Dheeraj Cidda 3-May-16 14:37pm
   
yes sir,exactly.I want the code how to differentiate whether it is a user or admin.can u please help..
can we talk privately sir if possible please.
   
What do you mean by "want code". You are the one who is supposed to develop code, otherwise this is not the site for you. You can contact me via my Web site, but I cannot guarantee anything.
—SA
Dheeraj Cidda 3-May-16 15:27pm
   
immean i have one.but i am getting an error.please see the code
   
Please don't put code in comments; it makes it unreadable. Use "Improve question".
—SA
Dheeraj Cidda 3-May-16 15:28pm
   
query("select * from userinfo WHERE email like '".$email."' AND password like '".$pass."';");

// Mysql_num_row is counting table row
$count = $result->num_row;

// Determine if user is "user" or "admin"
$usertype = $dbh->query("select type from userinfo where email like'".$email."';");
$row = $usertype->fetch_row();

// If result matched $myusername and $mypassword, table row must be 1 row
if ($count == 1) {

// Register $myusername, $mypassword and redirect to file
session_start();
$_SESSION['email'] = '$email';
$_SESSION['password'] = '$pass';
$_SESSION['type'] = $row;

// Redirect to appropriate page depending on user rights. Indicator 0 for user, 1 for admin.
if ($_SESSION['type'] == 1) {
header("location:adminlogin.php");
} else {
header("location:login.php");
}
$dbh = null;
} else {
echo "";
}
?>

<html>
<body>
<head>
<title>Login</title>
<link rel="stylesheet" href="style.css" />
<link rel='stylesheet' type='text/css'>
</head>
<div class="lg-container">
Wrong Username or Password

</body>
</html>
   
Oh gosh, it looks like you store passwords. Never ever do it. You can store cryptographic hash and compare hash to hash. Never store passwords anywhere, never pass them through the network when you do authentication.
—SA
Dheeraj Cidda 3-May-16 15:29pm
   
every time i open it, i am getting wrong username or password

1 solution

You will have to assign different roles that come with different permissions for different users.
Get the idea from this introductory tutorial: How to make Role-Based Access Control in PHP | Sevvlor[^]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900