Login/Signup script using php and mySQL
-----------------LOGIN & SIGNUP SCRIPTS---------------
<?php
session_start();
$error=""; //error message variable for login error
$error_signup="";//error message variable for signup error
// Create connection
//$connection=mysqli_connect("<host address>","<database username>","<database password>","<database name>");
//<Example database details>
$host="127.0.0.1";
$username="root";
$password="";
$db_name="motor_world";
$table_name="userinfo";
//connect to server and select database
$connection=mysqli_connect("$host","$username","$password") OR die("cannot connect");
mysqli_select_db($connection,$db_name) OR die("cannot select database");
//-----------------Start of Login Script-------------------
//if login button clicked only this login script will work.....it'll be done from the below line coding....
if(isset($_POST['login_button']) and $_POST['login_button']="login"){
//Getting details from the login form
$user_email=$_POST['u_email'];
$user_password=$_POST['u_password'];
//stripslashes and mysqli_real_escape_string is for security check
$user_email=stripslashes($user_email);
$user_password=stripcslashes($user_password);
$user_email = mysqli_real_escape_string($connection, $user_email);
$user_password = mysqli_real_escape_string($connection, $user_password);
//Retrieve user details from the data base
$sql="SELECT * FROM `$table_name` WHERE `eMail`='$user_email' and `password`='$user_password'";
$result=mysqli_query($connection,$sql) or die (mysqli_error($connection));
//check how many rows affected from select statement
$rowcount=mysqli_num_rows($result);
if($rowcount>0){
//user avilable
$_SESSION['user']=$username;
header ("Location:index1.php");
} else {
//invalid user
$error="*Invalid Username or Password";
}
}
//---------------End of Login Script-----------------
//---------------Start of SIGNUP Script--------------
//if SIGNUP button clicked only this script will work.....it will be done from the below coding.......
if( isset($_POST['signup_button']) && $_POST['signup_button']="signUp"){
//retrieving form values from signup form in the web site
$fname=mysqli_real_escape_string($connection, $_POST['fname']);
$lname=mysqli_real_escape_string($connection, $_POST['lname']);
$email=mysqli_real_escape_string($connection, $_POST['mail']);
$password=mysqli_real_escape_string($connection, $_POST['password']);
//Select statement will search for the usename and password in the database
$SQL="SELECT * FROM `userinfo` WHERE `eMail` = '$email' ";
$result2=mysqli_query($connection,$SQL);
if($result2){
$count = mysqli_num_rows($result2);
if($count>0){
$error_signup = "*Email already Exists!!!";
} else {
$SQL="INSERT INTO `userinfo`(`firstName`,`lastName`,`eMail`,`password`) VALUES('$fname','$lname','$email','$password')";
if(!mysqli_query($connection,$SQL))
die('Error:'.mysqli_error($connection));
//sessions will store your current state in the browser while you logged out or you close // your browser............
$_SESSION['user']=$email;
header ("Location:index1.php");
//---------------End of SignUp Script--------------
}
} else {
echo 'Error in the SQL statment';
}
}
//After open a database connection we have to manually close that connection
//therefore we are using mysqli_close() syntax for that purpose............
mysqli_close($connection);
?>
Keep it up genius! :)
ReplyDelete