Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I made a sliding sign-in and sign-up form and I don't know what is wrong with my code. Whenever I click the sign in button the url does not change into its designated link and it just turns white.

here is my php code of the form for the sign in only

<form action = "" method = "POST" class = "sign-in-form" >
			
				<div id = "logo"></div>
				<h2 class = "title">Sign In</h2>
					<div class = "input-field">
						class="fas fa-user">
						<input type = "text" placeholder="Username" name = "username"  value="<?php echo $_POST['username']; ?>">
					</div>

					<div class = "input-field">
						class="fas fa-lock">
						<input type = "password" placeholder="Password" name = "password" value="<?php echo $_POST['password']; ?>">
					</div>
			
					<button class="btn" name = "signin">Log In</button>


Here is php code for the process:
<?php

include 'config.php';

session_start();
error_reporting(0);

if(isset($_SESSION['user_id'])){
	header("../../Homepage/Home.php");
}

	if(isset($_POST['signup'])){
		$username = mysqli_real_escape_string($db, $_POST['signup_username']);
		$email = mysqli_real_escape_string($db,$_POST['signup_email']);
		$password = mysqli_real_escape_string($db, md5($_POST['signup_password']));
		$cpassword = mysqli_real_escape_string($db, md5($_POST['signup_cpassword']));

		$check_email = mysqli_num_rows(mysqli_query($db, "SELECT email FROM users WHERE email = '$email'"));
		$check_user = mysqli_num_rows(mysqli_query($db, "SELECT username FROM users WHERE username = '$username'"));

		if($password !== $cpassword){
			echo "<script>alert('Password Not Matched!');</script>";
		}else if($check_email>0){
			echo "<script>alert('Email Already exists');</script>";
		}else if($check_user>0){
			echo "<script>alert('Username Already exists');</script>";
		}else{
			$sql = "INSERT INTO users (username, email, password) VALUES ('$username','$email','.md5($password)')";
			$result = mysqli_query($db, $sql);

				if($result){
					$_POST['signup_username'] = "";
					$_POST['signup_email'] = "";
					$_POST['password'] = "";
					$_POST['cpassword'] = "";

					echo "<script>alert('User registration successfully!');</script>";
				}else{
					echo "<script>alert('Oops..User registration failed!');</script>";
				}
		}
	}


	if (isset($_POST["signin"])) {
  		$username = mysqli_real_escape_string($db, $_POST["username"]);
  		$password = mysqli_real_escape_string($db, md5($_POST["password"]));

  		$check_user = mysqli_query($db, "SELECT id FROM users WHERE username='$username' AND password='$password' AND status='1'");

  	if (mysqli_num_rows($check_user) > 0) {
   		 $row = mysqli_fetch_assoc($check_user);
   		 $_SESSION["user_id"] = $row['id'];
				header("../../Homepage/Home.php");
			} else {
   				 echo "<script>alert('Login details is incorrect. Please try again.');</script>";
  		}
	}



?>


What I have tried:

I have tried putting the php code inside the same file as the form. I have tried changing the name of the username and password. I have tried using username to validate the log-in instead of user id. I have tried putting the Homepage in the same folder as the SignIn form. But none seems to be working. I also doubled check the database but nothing seems to be wrong.
Posted
Updated 12-Dec-21 4:22am
Comments
Richard Deeming 13-Dec-21 4:50am    
An unsalted MD5 hash of the user's password is no better than storing the password in plain text. MD5 has not been "secure" for over two decades now.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

PHP provides built-in functions to help you do the right thing:
PHP: password_hash[^]
PHP: password_verify[^]
mystL01 13-Dec-21 6:40am    
Ok thank you. I'am editing my code right now and I'am using password_hash

1 solution

You have bigger problems than that, just you haven't met them yet...

1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

And to do all that on a web-based registration or sign in screen? That goes beyond "oops" and well into actual incompetence for which the fines would be truly ludicrous...
 
Share this answer
 
Comments
Richard Deeming 13-Dec-21 4:48am    
Technically, the password isn't stored in plain text; it's stored as an unsalted MD5 hash.

Realistically, that's no better than storing it in plain text. :)
mystL01 13-Dec-21 6:44am    
Thank you for your advice.

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