Skip to content

Instantly share code, notes, and snippets.

@arif98741
Last active June 18, 2018 11:05
Show Gist options
  • Save arif98741/57769c896bd1b3bfb695972a5ddf4a0e to your computer and use it in GitHub Desktop.
Save arif98741/57769c896bd1b3bfb695972a5ddf4a0e to your computer and use it in GitHub Desktop.
Login Registration User Existance
<?php
//phpdark.com
//[email protected]
if (isset($_POST['login'])) {
$con = new mysqli("localhost","root","","database"); //database connection
if ($con) {
$username = mysqli_real_escape_string($con, $_POST['$username']);
$password = mysqli_real_escape_string($con, $_POST['$password']);
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
if (empty($username) || empty($password) ) { //check wheather it is empty or not
echo "Field must not be empty";
} else {
$sql = "select * from table where username='$username'";
$check = $con->query($sql) or die($con->error)." error at line number ".__LINE__;
if ($check) {
$row = $check->num_rows;
if ($row > 0) {
echo "User already exist in database";
} else {
$sql = "select * from table where username='$username' and password='$password'";
$stmt = $con->query($sql) or die($con->error)." error at line number ".__LINE__;
if ($stmt) {
$row = $stmt->num_rows;
if ($row > 0) {
$database_pass = $stmt->fetch_assoc()['password'];
if (password_verify($database_pass, $hashed_password)) {
session_start();
$_SESSION['login'] = true;
$_SESSION['username'] = $username;
header("location: index.php"); //after successful login
}
}
} else {
echo "Failed to login";
}
}
}
}
} else {
echo "Database connnection failed";
}
}
@Kaperskyguru
Copy link

Please this code is very wrong.... this is a wrong method of doing this.. please use a prepared statement... if it's going on production

@arif98741
Copy link
Author

arif98741 commented Jun 18, 2018

thanks for sharing idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment