Its probably best you drop the old DW code as that wont mix with the new msqli code.
Below (scroll down) is mysqli login code example, replace ('localhost' , 'root' , 'root' , 'users_table') with the name of your server, username, password and database name.
The login form uses - name="username" and name="password" as the 'name' attribute values.
You get those values from the submitted form and asign them to variables:
$username = $conn->real_escape_string(trim($_POST['username']));
$password = $conn->real_escape_string(trim($_POST['password']));
The mysqli query selects ALL from the 'users' table in the 'users_table' database (you need to change the table name to that of your own table)
$sql = 'SELECT * FROM users';
If the query finds a match it takes you to a page named 'secure_page.php' (you can change the name of the secure_page.php in the script to what you like)
if ($row['username'] == $username && $row['password'] == $password) {
$_SESSION['username'] = $username;
header ('Location: secure_page.php');
}
If it doesnt find a match the script asigns an error message to a variable named '$response':
$response = "Sorry you do not have permission to access this website";
If the '$response' variable is set it gets echoed to the page (see full code below)
<?php
if(isset($response)) {
echo $response;
}
?>
You will need to make sure your database name columns and those in the script are correct ie it is no good using this:
if ($row['username'] == $username && $row['password'] == $password)
if the column names in your database are 'fred' and 'flintstone' - you would use:
if ($row['fred'] == $username && $row['flintstone'] == $password)
<!------ BELOW IS THE MYSQLI LOGIN CODE -->
Copy and save as login.php
<?php session_start() ?>
<?php $conn = new mysqli('localhost' , 'root' , 'root' , 'users_table'); ?>
<?php
if (array_key_exists('submit', $_POST)) {
$username = $conn->real_escape_string(trim($_POST['username']));
$password = $conn->real_escape_string(trim($_POST['password']));
$sql = 'SELECT * FROM users';
$result = $conn->query($sql) or die($conn->error);
while ($row = $result->fetch_assoc()) {
if ($row['username'] == $username && $row['password'] == $password) {
$_SESSION['username'] = $username;
header ('Location: pass_secure.php');
}
else {
$response = "Sorry you do not have permission to access this website";
}
}
}
?>
<!DOCTYPE>
<html>
<head>
<meta ="charset=UTF-8" />
<title>Security Login</title>
</head>
<body>
<?php
if(isset($response)) {
echo $response;
}
?>
<form id="login" name="login" method="post" action="">
<label>Username</label><br />
<input type="text" name="username" value=""/><br />
<label>Password</label><br />
<input type="text" name="password" value=""/><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<!-- YOU ALSO NEED TO INCLUDE THE BELOW CODE ON ANY OF THE 'SECURE PAGES' RIGHT AT THE TOP, BEFORE ANY OTHER CODE. THIS WILL PREVENT DIRECT ACCESS TO THE PAGE. IF ANYONE ATTEMPTS DIRECT ACCESS THEY WILL BE REDIRECTED TO THE LOGIN PAGE -->
<?php session_start() ?>
<?php
if (!isset($_SESSION['username'])) {
header("Location: login.php");
}
?>