My website has a login form and registration form in one page.
However, the system gets confused and uses both specifically $username and $password for login in and registering when filling up. Wanting to know how to separate and let the system differentiate these inputs. New to developing these
the PHP:
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$user_name = $_POST['user_name1'];
$password = $_POST['password1'];
if(!empty($user_name) && !empty($password) && !is_numeric($user_name))
{
$query = "select * from users where user_name = '$user_name' limit 1";
$result = mysqli_query($con, $query);
if($result)
{
if($result && mysqli_num_rows($result) > 0)
{
$user_data = mysqli_fetch_assoc($result);
if($user_data['password'] === $password)
{
$_SESSION['user_id'] = $user_data['user_id'];
header("Location: index.php");
die;
}
}
}
echo "wrong username or password!";
}else
{
header("Location: login.php");
echo "wrong username or password!";
}
}
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$user_name = $_POST['user_name'];
$password = $_POST['password'];
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$home_add = $_POST['home_add'];
$email = $_POST['email'];
$mob_no = $_POST['mob_no'];
if(!empty($user_name) && !empty($password) && !is_numeric($user_name))
{
$user_id = random_num(20);
$query = "insert into users
(user_id,user_name,password,f_name,l_name,email,home_add,mob_no)
values
('$user_id','$user_name','$password','$f_name','$l_name','$email','$home_add','$mob_no')";
mysqli_query($con, $query);
header("Location: login.php");
die;
}else
{
echo "Please enter some valid information!";
}
}
?>
The Login and Registration form:
enter code here<div class="c2">
<div class="Form C">
<div class="form-btn">
<span>Login</span>
<span>Register</span>
</div>
<form method="post" id="Login">
<input type="username" name="user_name1" placeholder="Username" >
<input type="password" name="password1" placeholder="Password" >
<button type="submit" class="btn">Login</button>
</form>
<form method="post" id="Reg" >
<input type="username" name="user_name" placeholder="Username" >
<input type="password" name="password" placeholder="Password" >
<input type="firstname" name="f_name" placeholder="Firstname">
<input type="lastname" name="l_name" placeholder="Lastname">
<input type="email" name="email" placeholder="Email">
<input type="Homeaddress" name="home_add" placeholder="Home Address">
<input type="Mobileno" name="mob_no" placeholder="Mobile No.">
<button type="submit" class="btn">Register</button>
</form>
</div>
</div>
2
Answers
If you want separate the file of login and registration make another file example is login.php & registration.php and add action to your HTML form action="login.php"
Or you don’t want to separate their file make a condition that will identify them if they’re login or registration something like this
Or a hidden input so you can identify the login and registration
Note: Learn the how to prevent sql injection
We have 2 problems to solve here:
For the first one you can add
name
attribute in your html form:And now for the second problem, you need to consider the following:
session_regeneration_id()
before$_SESSION['user_id'] = $user_data['user_id'];
, google about Session Hijacking!Good Luck!