Here’s something that might help that I threw together. It obviously does NOT work, but there shouldn’t be any syntax errors. I can’t say there isn’t logical errors for there might be and the scripts need to be put in appropriate pages (files) with modifications.
[php]<?php
/*
- This is just defining constants and this portion would normally go in a separate file off the root directory of
- you project. I use an if statement to determine local and remote server that way I don’t have to keep changing
- the constants when I make modifications to the file and I finally move it to the remote server (production server).
/
if (filter_input(INPUT_SERVER, ‘SERVER_NAME’, FILTER_SANITIZE_URL) == “localhost”) {
define(‘DATABASE_HOST’, ‘localhost’); // usually localhost
define(‘DATABASE_NAME’, ‘cms’);
define(‘DATABASE_USERNAME’, ‘username’);
define(‘DATABASE_PASSWORD’, ‘password’);
define(‘DATABASE_TABLE’, ‘users’);
} else {
define(‘DATABASE_HOST’, ‘remote_database_host’);
define(‘DATABASE_NAME’, ‘remote_db_name’);
define(‘DATABASE_USERNAME’, ‘remote_username’);
define(‘DATABASE_PASSWORD’, ‘remote_password’);
define(‘DATABASE_TABLE’, ‘users’);
}
/
- End of Defining Constants
*/
function read($username, $password) {
/*
* I use PDO and I recommend it over mysqli for I find it easier to work with, gives you more database options
* other than MySQL and you cand use named prepared statements.
/
$db_options = array(
/ important! use actual prepared statements (default: emulate prepared statements) /
PDO::ATTR_EMULATE_PREPARES => false
/ throw exceptions on errors (default: stay silent) /
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/ fetch associative arrays (default: mixed arrays) /
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO(‘mysql:host=’ . DATABASE_HOST . ‘;dbname=’ . DATABASE_NAME . ‘;charset=utf8’, DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
/ Setup the Query for reading in login data from database table */
$query = ‘SELECT id, username, password, full_name, email, security_level, private FROM users WHERE username=:username’;
$stmt = $pdo->prepare($query); // Prepare the query:
$stmt->execute([':username' => $username]); // Execute the query with the supplied user's parameter(s):
$stmt->setFetchMode(PDO::FETCH_OBJ); // If you want to use an array then use FETCH_ASSOC:
$user = $stmt->fetch(); // Fetch the appropiate record:
/*
* If username isn't in the datebase table return false.
*/
if (!$user) {
return FALSE;
}
/*
* If password matches database table match send back true otherwise send back false.
* Check out http://php.net/manual/en/function.password-hash.php
* and
* http://php.net/manual/en/function.password-verify.php for better password management.
*/
if (password_verify($password, $user->password)) {
return \TRUE;
} else {
return \FALSE;
}
}
/*
-
This is just an example of how you might grab the user’s input.
*/
if (isset($submit) && $submit === ‘login’) {
$username = filter_input(INPUT_POST, ‘username’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$password = filter_input(INPUT_POST, ‘password’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$result = read($username, $password);
if ($result) {
after_successful_login();
}
}[/php]
You also need to relax for coding can be challenging and I have spent long hours of debugging a script to get it to work. I found myself doing more earlier on when I was just learning PHP, but I occasionally hit a snag now. I even have written a fairly lengthy script only to be told by someone with more programming experience to scrap it for it wasn’t secure. Talk about frustration! Anyways we all have been there when it comes to coding. Syntax errors are easy to debug, it’s the logical errors that are the tough ones.
HTH John
P.S. One good way to debug is to have errors to on (local server only) ->
[php]/* Turn on error reporting */
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
if (filter_input(INPUT_SERVER, ‘SERVER_NAME’, FILTER_SANITIZE_URL) == “localhost”) {
error_reporting(-1); // -1 = on || 0 = off
} else {
error_reporting(0); // -1 = on || 0 = off
}[/php]