Skip to content

Instantly share code, notes, and snippets.

@vc8bp
Last active March 17, 2023 11:31
Show Gist options
  • Save vc8bp/27685ed0e2348c667e1b50c4765489cc to your computer and use it in GitHub Desktop.
Save vc8bp/27685ed0e2348c667e1b50c4765489cc to your computer and use it in GitHub Desktop.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "mydb"; //tere db a name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected to database successfully"
?>
<?php
include 'db.php';
if (isset($_POST["email"])) {
$email = $_POST["email"];
$pass = $_POST['pass'];
$query = "SELECT * from users where email='$email' AND pass='$pass' limit 1";
$ress = mysqli_query($conn, $query);
if (mysqli_num_rows($ress)) {
session_start();
$_SESSION["logedin"] = true;
while($row = mysqli_fetch_array($ress)){
$_SESSION["isAdmin"] = $row["isAdmin"];
$_SESSION["name"] = $row["name"];
$_SESSION["email"] = $row["email"];
$_SESSION["userid"] = $row["id"];
}
if($_SESSION["isAdmin"]){
header('Location: admin/adminhome.php');
} else {
header('Location: index.php');
}
exit();
} else {
$error = "Username and password dosent matched";
}
}
?>
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<style>
/* Add your own CSS to style the page */
.container {
width: 100vw;
height: 100vh;
background-color: #f0f0f0;
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
}
.form-group {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 10px;
background-color: white;
border-radius: 10px;
padding: 50px 30px;
box-shadow: 0px 0px 10px gray;
width: 500px;
max-width: 90%;
}
h3 {
margin: 0 0 10px 0;
}
input[type="text"], input[type="password"] {
box-sizing: border-box;
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid gray;
margin-top: 10px;
width: 100%;
}
input[type="submit"] {
padding: 10px 20px;
background-color: dodgerblue;
color: white;
border-radius: 5px;
border: none;
cursor: pointer;
margin-top: 20px;
width: 100%;
}
p.error {
color: red;
margin: 10px 0 0 0;
}
</style>
</head>
<body>
<div class="container">
<div class="form-group">
<h3>Login</h3>
<form action="#" method="POST">
<input type="text" name="email" placeholder="Email">
<input type="password" name="pass" placeholder="Password">
<input type="submit" value="Login">
</form>
<?php if (isset($error)) { echo "<p class='error'>".$error."</p>"; } ?>
</div>
</div>
</body>
</html>
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
CREATE TABLE `users` (
`id` int(6) UNSIGNED NOT NULL,
`name` varchar(30) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`isAdmin` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `users` (`id`, `name`, `email`, `password`, `isAdmin`) VALUES
(1, 'vivek', 'vc8bppc@gmail.com', '1234', 0);
ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `email` (`email`);
ALTER TABLE `users`
MODIFY `id` int(6) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;
<?php
include 'db.php';
if (isset($_POST["name"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$pass = $_POST['password'];
$age = $_POST['age'];
$query = "INSERT INTO users (`name`, `email`, `password`) VALUES ('$name','$email','$pass')";
if (mysqli_query($conn, $query)) {
header('Location: index.php');
exit();
} else {
$error = "Failed to register: " . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
<style>
/* Add your own CSS to style the page */
.container {
width: 100vw;
height: 100vh;
background-color: #f0f0f0;
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
}
.form-group {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 10px;
background-color: white;
border-radius: 10px;
padding: 50px 30px;
box-shadow: 0px 0px 10px gray;
width: 500px;
max-width: 90%;
}
h3 {
margin: 0 0 10px 0;
}
input[type="text"], input[type="password"] {
box-sizing: border-box;
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid gray;
margin-top: 10px;
width: 100%;
}
input[type="submit"] {
padding: 10px 20px;
background-color: dodgerblue;
color: white;
border-radius: 5px;
border: none;
cursor: pointer;
margin-top: 20px;
width: 100%;
}
p.error {
color: red;
margin: 10px 0 0 0;
}
</style>
</head>
<body>
<div class="container">
<div class="form-group">
<h3>Register</h3>
<form action="#" method="POST">
<input type="text" name="name" placeholder="name">
<input type="text" name="email" placeholder="email">
<input type="password" name="password" placeholder="Password">
<input type="text" name="age" placeholder="Age">
<input type="submit" value="Login">
</form>
<?php if (isset($error)) { echo "<p class='error'>".$error."</p>"; } ?>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment