Skip to content

Instantly share code, notes, and snippets.

@nisargjhaveri
Created February 20, 2016 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nisargjhaveri/789fb0f76ccb0962399f to your computer and use it in GitHub Desktop.
Save nisargjhaveri/789fb0f76ccb0962399f to your computer and use it in GitHub Desktop.
Webdev Workshop - Day 2 - Codes for login and register
<?php
ini_set('display_error', 1);
error_reporting(E_ALL);
$db_config = [
'host' => 'localhost',
'username' => 'root',
'password' => '',
'db_name' => 'test'
];
$db_link = mysqli_connect(
$db_config['host'],
$db_config['username'],
$db_config['password'],
$db_config['db_name']
);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Login</h1>
<form action="login.php" method="post">
<div>
Name:
<input type="text" name="username" placeholder="Name">
</div>
<div>
Password:
<input type="password" name="password" placeholder="Password">
</div>
<input type="submit" value="Login">
</form>
<h1>Register</h1>
<form action="register.php" method="post">
<div>
Name:
<input type="text" name="username" placeholder="Name">
</div>
<div>
Password:
<input type="password" name="password" placeholder="Password">
</div>
<input type="submit" value="Register">
</form>
</body>
</html>
<?php
require "common.php";
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$result = mysqli_query($db_link,
"SELECT * from user WHERE username='$username'");
$row = mysqli_fetch_assoc($result);
if ($row) {
if (password_verify($_POST['password'], $row['password'])) {
echo "Login success";
}
else {
echo "Incorrect password";
}
}
else {
echo 'Incorrect username';
}
}
else {
echo "Please provide username and password both";
}
<?php
require "common.php";
// Here we have a variable $db_link now
if (isset($_POST['username'])
&& isset($_POST['password'])) {
$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$result = mysqli_query($db_link,
"INSERT INTO user (username, password) VALUES ('$username', '$password')");
if ($result) {
echo "Registered";
}
else {
echo "DB query failed";
}
}
else {
echo "Please provide username and password both";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment