Skip to content

Instantly share code, notes, and snippets.

@yama
Created April 10, 2022 11:46
Show Gist options
  • Save yama/9a4a3a4be15aaabc163eda46c46b915b to your computer and use it in GitHub Desktop.
Save yama/9a4a3a4be15aaabc163eda46c46b915b to your computer and use it in GitHub Desktop.
ベーシック認証生成
<?php
// ini_set('display_errors', "On");
echo parse_text(
form_text(),
[
'user' => getv('user'),
'pwd' => getv('pwd')
]
);
if (!getv('user') || !getv('pwd')) {
return;
}
echo parse_text(
auth_text(),
[
'dir' => __DIR__
]
);
echo parse_text(
htpasswd_text(),
[
'user' => getv('user'),
'pwd' => password_hash(getv('pwd'), PASSWORD_BCRYPT)
]
);
function getv($key)
{
if (!isset($_GET[$key])) {
return null;
}
return $_GET[$key];
}
function form_text()
{
ob_start();
?>
<form>
username : <input name="user" type="text" value="[+user+]"><br>
password : <input name="pwd" type="text" value="[+pwd+]"><br>
<input type="submit">
</form>
<?php
return ob_get_clean();
}
function auth_text()
{
ob_start();
?>
.htaccess<br>
<textarea rows="5" style="display:block;width:640px;">
AuthType Basic
AuthName "Please enter your ID and password."
AuthUserFile [+dir+]/.htpasswd
require valid-user
</textarea>
<?php
return ob_get_clean();
}
function htpasswd_text()
{
ob_start();
?>
.htpasswd<br>
<textarea rows="5" style="display:block;width:640px;">[+user+]:[+pwd+]</textarea>
<?php
return ob_get_clean();
}
function parse_text($tpl, $ph)
{
foreach ($ph as $k => $v) {
$tpl = str_replace('[+' . $k . '+]', $v, $tpl);
}
return $tpl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment