Created
February 20, 2016 13:30
-
-
Save nisargjhaveri/789fb0f76ccb0962399f to your computer and use it in GitHub Desktop.
Webdev Workshop - Day 2 - Codes for login and register
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'] | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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