Buat modul dan rekatkan kode berikut dalam file modul Anda:
<?php
/**
* Implementation of hook_boot().
*
* Ask for user credentials and try to authenticate.
*/
function foo_boot() {
require_once DRUPAL_ROOT . '/includes/password.inc';
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
$query = "SELECT pass FROM {users} WHERE name = :name";
$result = db_query($query, array(':name' => $_SERVER['PHP_AUTH_USER']));
$account = new stdClass();
foreach ($result as $row) {
$account->pass = $row->pass;
}
if (isset($account->pass)) {
if (user_check_password($_SERVER['PHP_AUTH_PW'], $account)) {
return;
}
}
}
header('WWW-Authenticate: Basic realm="Development"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
Ini menggunakan Otentikasi HTTP dan memeriksa Database Drupal untuk nama pengguna dan kata sandi yang valid.
Jika Anda memiliki masalah dengan PHP CLI, Drush atau cron, Anda dapat menambahkan kode berikut di hook:
// Allow cron through
if (basename($_SERVER['PHP_SELF']) == 'cron.php') {
return;
}
// Allow PHP CLI/Drush through
if (isset($_SERVER['argc'])) {
if (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)) {
return;
}
}