Issue 1 I have this code below, and I keep hearing people talking about using loads of classes. Is there any merit to me putting the code below as a class (I'm already turning the data into objects) and how would I go about doing it? (I'm new to OOP PHP).
try {
$connection = new PDO(DATA, USER, PASSWORD);
$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $error) {
PDOcrash($error);
}
// Scrubbing remember data
$_POST['remember'] = (int)$_POST['remember'];
// Query the database for the unique salt
$query = $connection->prepare("SELECT id, salt FROM {$_SESSION['environment']->database}.system_user WHERE username = :username LIMIT 1");
$query->execute(array(':username' => $_POST['username']));
$security = $query->fetch(PDO::FETCH_OBJ);
$query->closeCursor();
// Form the hash using sha1 alrorithm
$_POST['password'] = sha1(sha1($security->salt) . sha1($_POST['password']));
$query = $connection->prepare("SELECT id, centre, reference, first_name, last_name FROM {$_SESSION['environment']->database}.system_user WHERE username = :username AND password = :password");
$query->execute(array(':username' => $_POST['username'], ':password' => $_POST['password']));
$_SESSION['user'] = $query->fetch(PDO::FETCH_OBJ);
$query->closeCursor();
Issue 2
For this code block:
try {
$connection = new PDO(DATA, USER, PASSWORD);
} catch (PDOException $error) {
PDOcrash($error);
}
I am using constants to define my connection details, but they only seem to work if I define them directly on the page. I thought a constant would stay a constant through the system. If I move my define()
functions to an earlier encountered page, they don't work. Does this mean the scope of a constant is a single page? I also heard that you can define connection info in some separate apache document and this is a more secure method. How do I go about doing this?