How to Submit Registration and Login Form in PHP?
Hi friends, today we will learn How to Submit Registration and Login Form in PHP
so let's start
Note - follow the steps.
Step 1 - First we will create a Registration form and write code for this.
create a PHP file in your folder registartion.php. I am not using CSS here. just use simple HTML you can use accordingly to you.
create a PHP file in your folder registartion.php. I am not using CSS here. just use simple HTML you can use accordingly to you.
<?php
require('config.php');
if(isset($_POST['submit'])){
if($_POST['username'] !== "" && $_POST['email'] !== "" && $_POST['password'] !=="" && $_POST['cPassword'] !== "" ){
// if you want to check all fields are required.
if($_POST['password'] !== $_POST['cPassword']){ //check password and confirm password
$pwd_nomatch = "password not match";
}else{
// we don't need to store confirm password in DB
// make sure you should include connection file on the top ex -> require('config.php');
$query = "INSERT INTO `register` (username, password,email) VALUES ('".$_POST['username']."', '".$_POST['cPassword']."', '".$_POST['email']."')";
$result = mysql_query($query);
if($result){
$success_msg = "User Created Successfully.";
}
}
}else{
// Error
$allerror = "All field are requried";
}
// or if you want to do separate condition
if($_POST['username'] !== ""){
$username = mysql_real_escape_string(trim($_POST['username']));
}else{
$user_error = "User Name is required";
}
if($_POST['email'] !== ""){
$email = mysql_real_escape_string(trim($_POST['email']));
}else{
$email_error = "Email Id is required";
}
if($_POST['password'] !== ""){
$pwd = mysql_real_escape_string($_POST['password']);
}else{
$pwd_error = "Password required";
}
if($_POST['cPassword'] !== ""){
$cPassword = mysql_real_escape_string($_POST['cPassword']);
}else{
$cPassword_error = "Confirm Password required";
}
if($_POST['cPassword'] == $_POST['userpassword']){
// make sure you should include connection file on the top ex -> require('config.php');
$check_user_exists = "select * from `user` where email ='".$_POST['email']."' ";
$result = mysql_query($connection,$check_user_exists);
$num_rows = mysql_num_rows($result);
if($num_rows > 0){ // check if user exists already or not
// we don't need to store confirm password in DB
$query = "INSERT INTO `register` (username, password, email) VALUES ('$username', '$pwd', '$email')";
$result = mysql_query($query);
if($result){
$success_msg = "User Created Successfully.";
}
}else{
$user_exists = "User already exists";
}
}else{
$pwd_nomatch = "password not match";
}
}
?>
<center><h1>Regisartion Form</h1></center>
<form action="registarion.php" method="post">
<?php if(isset($success_msg)){?>
<b> <?php echo $success_msg ;?> </b>
<?php } ?>
<input type = "text" name="username" value="">
<?php if(isset($user_error)){?>
<span> <?php echo $user_error ;?> </span>
<?php } ?>
<input type = "email" name="email" value="">
<?php if(isset($email_error)){?>
<span> <?php echo $email_error ;?> </span>
<?php } ?>
<input type = "password" name="password" value="">
<?php if(isset($pwd_error)){?>
<span> <?php echo $pwd_error ;?> </span>
<?php } ?>
<input type = "password" name="cpassword" value="">
<?php if(isset($cPassword_error)){?>
<span> <?php echo $cPassword_error ;?> </span>
<?php } ?>
<?php if(isset($pwd_nomatch)){?>
<span> <?php echo $pwd_nomatch ;?> </span>
<?php } ?>
<input type = "submit" name="submit" value="submit">
<?php if(isset($allerror)){?>
<span> <?php echo $allerror ;?> </span>
<?php } ?>
<?php if(isset($user_exists)){?>
<span> <?php echo $user_exists ;?> </span>
<?php } ?>
</form>
- Read also: How to pass a variable from the command line using PHP?
- String Functions in PHP
- Read : How to pass variable from command line using PHP?
- How to Get All Google play store apps List using PHP Scraper?
Step 2 - Now you have completed Successfully Registration form and wrote PHP code with validations.
Now we will create login Form using simple HTML and learn How we will match the credentials for login using PHP
create login.php file in your folder and include your connection file.
<?php// login.php
require('config.php');
if(isset($_POST['submit'])){
if($_POST['email'] !== "" && $_POST['password'] !==""){
// make sure you should include connection file on the top ex -> require('config.php');
$query = "select * from `user` where `email` = '".mysql_real_escape_string($_POST['email'])."' and `password` = '".mysql_real_escape_string($_POST['password'])."' ";
$result = mysql_query($connection,$query);
if(mysqli_num_rows($result) > 0){
$success_msg = "You are Successfully login.";
header('Location: index.php');
}else{
$error_msg = "email id or password not match";
}
}else{
// Error
$allerror = "All field are requried";
}
}
?>
File name - login.php
<center><h1>Login Form</h1></center>
<form action="login.php" method="post">
<?php if(isset($success_msg)){?>
<b> <?php echo $success_msg ;?> </b>
<?php } ?>
<input type = "email" name="email" value="">
<?php if(isset($email_error)){?>
<span> <?php echo $email_error ;?> </span>
<?php } ?>
<input type = "password" name="password" value="">
<?php if(isset($pwd_error)){?>
<span> <?php echo $pwd_error ;?> </span>
<?php } ?>
<input type = "submit" name="submit" value="Login">
<?php if(isset($allerror)){?>
<span> <?php echo $allerror ;?> </span>
<?php } ?>
<?php if(isset($error_msg)){?>
<span> <?php echo $error_msg ;?> </span>
<?php } ?>
</form>
Yeah! You have learned ЁЯШК
I hope you understood this and like this article. if you like it so, please give feedback in the comment section.If any mistakes I made here, so please let me know and improve me.If you have any query, feel free to ask me ЁЯШКRecommended post
- How to Get All Google play store apps List using PHP Scraper?
- How to pass a variable from the command line using PHP?
- How to Submit Registration and Login Form in PHP?
- Introduction PHP
- how to declare variables in PHP?
- What things you need to install PHP?
- How to get meta tags in 2 easy way in PHP
- How to Encode and Decode Strings with Base64 in JavaScript examples
- How to Reset DropDownList Selection Using jQuery?
- Create dynamic drop-down list option with JavaScript and jQuery
- Onclick show and hide div using JQuery with examples
Post a Comment
Please do not enter any spam link in the comment section.