Please can you check my code and tell about any smokestack
I create login form with session with four files:
- connection.php
- login.php
- plan.php
- logout.php
Four pages
connection.php, login.php,plan.php,logout
Please check my code and tell about any smokestack.phpDatabase educate(Create table users):Database educate (create table users)
CREATE TABLE users(
id INT AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(100) NOT NULL,
PRIMARY KEY(id) );
Connection.phpConnection.php
<?php
$dsn = 'mysql:host=localhost;dbname=educate'; //Data source Name
$username = 'root';
$password = '';
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND=> 'SET NAMES utf8');
$conn = new PDO($dsn, $username, $password,$options);
?>
login.phplogin.php
<?php
session_start();
if(!empty($_SESSION['username'])) {
header('location:plan.php');
}
require 'connection.php';
if(isset($_POST['login'])) {
$user = $_POST['username'];
$pass = $_POST['password'];
if(empty($user) || empty($pass)) {
$message = 'All field are required';
} else {
$query = $conn->prepare("SELECT username, password FROM users WHERE
username=? AND password=? ");
$query->execute(array($user,$pass));
$row = $query->fetch(PDO::FETCH_BOTH);
if($query->rowCount() > 0) {
$_SESSION['username'] = $user;
header('location:plan.php');
} else {
$message = "Username/Password is wrong";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
if(isset($message)) {
echo $message;
}
?>
<form action="#" method="post">
Username: <input type="text" name="username" placeholder="username">
<br/><br/>
Password: <input type="password" name="password" placeholder="password">
<br/><br/>
<input type="submit" name="login" value="Login">
</form>
</html>
logout.phplogout.php
<?php
session_start();
session_destroy();
header('location: login.php');
?>
plan.phpplan.php
<?php
session_start();
if(isset($_SESSION['username'])) {
echo "Welcome <strong>".$_SESSION['username']."</strong><br/>";
} else {
header('location: login.php');
}
?>
<a href="logout.php">Logout</a>