Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. Im making multiuser login system and it works fine except for one thing. You can enter/log in even when username/password is wrong. What is the code for that? Your help is greatly appreciated. I am new to this.

What I have tried:

PHP
alert("you are logged in successfully and you are logged in as ' .$row['usertype'].'")';

	}
if($usertype=="SS"){
?>

window.location.href="SS.php"


window.location.href="CC.php"



	<title>Murder Mystery
	


	<h2>LOGIN</h2>
	
		<table><tbody><tr>			<td>Username: </td>		</tr>		<tr>			<td>Password: </td>		</tr>		<tr>			<td>				Select user type: 
					SS
					CC
					TT
					WW
					BB
					PP
				
			</td>		</tr>		<tr>			<td></td>		</tr>	</tbody>
</table>



<!DOCTYPE html>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="multiuserlogin";
$conn=mysqli_connect($servername, $username, $password, $dbname);

if(isset($_POST['Login'])){
	$user=$_POST['user'];
	$pass=$_POST['pass'];
	$usertype=$_POST['usertype'];
	$query="SELECT * FROM `multiuserlogin` WHERE username='".$user."'and password= '".$pass."' and usertype= '".$usertype."'";
	$result = mysqli_query($conn, $query);
	if($result){
		while($row=mysqli_fetch_array($result)){
echo'<script type="text/javascript">alert("you are logged in successfully and you are logged in as ' .$row['usertype'].'")</script>';

	}
if($usertype=="SS"){
?>
<script type="text/javascript">
window.location.href="SS.php"</script>
<?php

}else if($usertype="CC"){
?>
<script type="text/javascript">
window.location.href="CC.php"</script>
<?php

}
}else{
	echo'no result';
}
}


?>
<html>
<head>
	<title>Murder Mystery</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form method="post">
	<h2>LOGIN</h2>
	<table>
		<tr>
			<td>Username: <input type="text" name="user" placeholder="enter your username" required></td>
		</tr>
		<tr>
			<td>Password: <input type="text" name="pass" placeholder="enter your password" required></td>
		</tr>
		<tr>
			<td>
				Select user type: <select name="usertype">
					<option value="SS">SS</option>
					<option value="CC">CC</option>
					<option value="TT">TT</option>
					<option value="WW">WW</option>
					<option value="BB">BB</option>
					<option value="PP">PP</option>
				</select>
			</td>
		</tr>
		<tr>
			<td><input type="submit" name="Login" value="Login"></td>
		</tr>
	</table>
	
</form>

</body>
</html>
Posted
Updated 8-Jun-21 14:29pm
v2

<!DOCTYPE html>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="multiuserlogin";
$conn=mysqli_connect($servername, $username, $password, $dbname);

if(isset($_POST['Login'])){
	$user=$_POST['user'];
	$pass=$_POST['pass'];
	$usertype=$_POST['usertype'];
	$query="SELECT * FROM `multiuserlogin` WHERE username='".$user."'and password= '".$pass."' and usertype= '".$usertype."'";
	$result = mysqli_query($conn, $query);
	if($result){
		while($row=mysqli_fetch_array($result)){
echo'<script type="text/javascript">alert("you are logged in successfully and you are logged in as ' .$row['usertype'].'")</script>';

	}
if($usertype=="SS"){
?>
<script type="text/javascript">
window.location.href="SS.php"</script>
<?php

}else if($usertype="CC"){
?>
<script type="text/javascript">
window.location.href="CC.php"</script>
<?php

}
}else{
	echo'no result';
}
}


?>
<html>
<head>
	<title>Murder Mystery</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form method="post">
	<h2>LOGIN</h2>
	<table>
		<tr>
			<td>Username: <input type="text" name="user" placeholder="enter your username" required></td>
		</tr>
		<tr>
			<td>Password: <input type="text" name="pass" placeholder="enter your password" required></td>
		</tr>
		<tr>
			<td>
				Select user type: <select name="usertype">
					<option value="SS">SS</option>
					<option value="CC">CC</option>
					<option value="TT">TT</option>
					<option value="WW">WW</option>
					<option value="BB">BB</option>
					<option value="PP">PP</option>
				</select>
			</td>
		</tr>
		<tr>
			<td><input type="submit" name="Login" value="Login"></td>
		</tr>
	</table>
	
</form>

</body>
</html>
 
Share this answer
 
The major thing for proper authentication is how you handle usernames and passwords. In your code excerpt both have serious problems.

You store password and username as plain text in the database. This should never be done. You don't need to know the password, you just need to know if the one originally stored matches the one entered.

Never concatenate text from user interface to a SQL statement. This leaves you open to SQL injection - Wikipedia[^] and other potential problems.

I suggest going through Password Storage: How to do it.[^] . It gives you a good idea of the basic principles that should be met.
 
Share this answer
 
PHP
$query="SELECT * FROM `multiuserlogin` WHERE username='".$user."'and password= '".$pass."' and usertype= '".$usertype."'";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

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