Add Username and Password Authentication to h5ai for Viewing

Publish: 2018-09-04 | Modify: 2018-09-06

<a href="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://www.cenbor.cn/html/13.html&amp;title=给h5ai增加查看需要输入用户名和密码&amp;site=https://www.cenbor.cn/" title="" target="_blank"><i></i></a>
<a href="http://service.weibo.com/share/share.php?url=https://www.cenbor.cn/html/13.html&amp;title=给h5ai增加查看需要输入用户名和密码" title="" target="_blank"><i></i></a></div></ol>
<div id="postpage">
<article>
<div><div></div>
<div id="post-content">
<div>
<p>Explanation: Usually, after installing h5ai, you can directly view the directory without entering a username and password, which is not secure. Therefore, we need to manually add some code to require a username and password to view.</p>
<p>Use WinSCP software to log in to VPS and navigate to the h5ai directory and find _h5ai/public/index.php and edit it.</p>
<p>1. Add the following line right after the PHP opening tag `<?php`:</p>
<pre><code>auth();
</code></pre>
<p>2. Add the following code at the bottom:</p>
<pre><code>function auth ()
{
        $valid_passwords = array ("username" => "password");
        $valid_users = array_keys($valid_passwords);

        $user = $_SERVER['PHP_AUTH_USER'];
        $pass = $_SERVER['PHP_AUTH_PW'];

        $validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

        if (!$validated) {
          header('WWW-Authenticate: Basic realm="My Realm"');
          header('HTTP/1.0 401 Unauthorized');
          die ("Not authorized");
        }
}
</code></pre>
<p>Note: You can fill in any username and password.</p>
<hr/>
<blockquote>
<p>Copyright belongs to: Rat's Blog</p>
<p>Original article link: [https://www.cenbor.cn/go/aHR0cHM6Ly93d3cubW9lcmF0cy5jb20vYXJjaGl2ZXMvMTI1Lw==](https://www.moerats.com/archives/125/)</p>
<p>Reproduced with proper credit and this statement</p>
</blockquote>
</div>
<div>
<div>
<i></i>
<span>Last modified: September 4, 2018, 03:14 PM</span>
</div>
<div><span>© All rights reserved by the author</span>
</div>
</article>
<nav>
</nav>
<div class='statement'><p>Original article from: [给h5ai增加查看需要输入用户名和密码](https://www.cenbor.cn/html/13.html), the final interpretation right belongs to the original author. If there is any infringement, please contact QQ: 337003006 for deletion.</p></div>

Comments