create a sign-in page with the MySQL Database. Before entering the code section, you will need to create MySQL databases. So considering you have access to the root user, you can create any database using mysql mysqladmin binary.
CREATE TABLE `register` (
`id` int(11) NOT NULL,
`email` varchar(64) NOT NULL,
`username` varchar(250) NOT NULL,
`password` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Then place the example code one by one, see the following example codeĀ
#config.php
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'tutorial');
$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
?>
#login.php
<?php
include("config.php");
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($db, $_POST['username']);
$mypassword = mysqli_real_escape_string($db, $_POST['password']);
$sql = "SELECT id FROM register WHERE username = '$myusername' and password = '$mypassword'";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$active = $row['active'];
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if ($count == 1) {
session_register("myusername");
$_SESSION['login_user'] = $myusername;
header("location: welcome.php");
} else {
$error = "Your Login Name or Password is invalid";
}
}
?>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<div>
<div>
<div><h4>Login</h4></div>
<div>
<form action = "" method = "post">
<label>UserName :</label><input type = "text" name = "username" class = "box"/><br /><br />
<label>Password :</label><input type = "password" name = "password" class = "box" /><br/><br />
<input type = "submit" value = " Submit "/><br />
</form>
<div style = "font-size:11px; color:#cc0000; margin-top:10px">
<?php if (isset($error)): ?>
<?php echo $error; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
</body>
</html>
#welcome.php
<?php include('session.php');?>
<h1>Welcome <?php echo $login_session; ?></h1>
<h2><a href = "logout.php">Sign Out</a></h2>
#logout.php
<?php
session_start();
if (session_destroy()) {
header("Location: login.php");
}
?>
#session.php
<?php
include('config.php');
session_start();
$user_check = $_SESSION['login_user'];
$ses_sql = mysqli_query($db, "select username from register where username = '$user_check' ");
$row = mysqli_fetch_array($ses_sql, MYSQLI_ASSOC);
$login_session = $row['username'];
if (!isset($_SESSION['login_user'])) {
header("location:login.php");
die();
}
?>
Session.php will confirm a session, if there are no sessions to redirect to the sign-in page.