{"id":9908,"date":"2021-05-10T11:00:09","date_gmt":"2021-05-10T02:00:09","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9908"},"modified":"2021-05-07T19:55:34","modified_gmt":"2021-05-07T10:55:34","slug":"basic-auth-by-php","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9908","title":{"rendered":"Basic Auth by PHP"},"content":{"rendered":"\n<p>There are several ways to add a basic authentication using php. Today I would like to share a snippet to add basic authentication easily.<\/p>\n\n\n\n<p>Lets get started.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/\/you can define your custom username and password here.\n$AUTH_USER = 'username';\n$AUTH_PASS = 'password';\n\nheader('Cache-Control: no-cache, must-revalidate, max-age=0');\n$credentials = !(empty($_SERVER['PHP_AUTH_USER']) &amp;&amp; empty($_SERVER['PHP_AUTH_PW']));\n\n\/\/check credentials existence or wrong credentials\n$auth_fail = (\n\t!$credentials ||\n\t$_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||\n\t$_SERVER['PHP_AUTH_PW']   != $AUTH_PASS\n);\n\n\/\/Ask for the auth. If credentials are not correct, deny the access.\nif ($auth_fail) {\n\theader('HTTP\/1.1 401 Authorization Required');\n\theader('WWW-Authenticate: Basic realm=\"Access denied\"');\n\texit;\n}<\/code><\/pre>\n\n\n\n<p>After the coding process, you will get a confirmation alert box for authentication before accessing the website as below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"918\" height=\"430\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/05\/image.png\" alt=\"\" class=\"wp-image-9911\"\/><\/figure>\n\n\n\n<p>Well, I think you can now add basic auth in any of your PHP application neat and easily.<\/p>\n\n\n\n<p>If you want to add basic authentication for contents like html and images etc , you can also do that by configuring <code>.htaccess<\/code><\/p>\n\n\n\n<p>Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9908\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9908\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Basic Auth by PHP\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9908\" data-text=\"Basic Auth by PHP\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9908\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9908\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>There are several ways to add a basic authentication using php. Today I would like to share a snippet to add b [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9908"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9908"}],"version-history":[{"count":7,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9908\/revisions"}],"predecessor-version":[{"id":10112,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9908\/revisions\/10112"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}