{"id":1656,"date":"2016-10-28T23:02:42","date_gmt":"2016-10-28T23:02:42","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1656"},"modified":"2016-11-07T19:45:15","modified_gmt":"2016-11-07T19:45:15","slug":"how-protect-files-using-php-and-htaccess","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2016\/10\/how-protect-files-using-php-and-htaccess\/","title":{"rendered":"How protect files using php and .htaccess ?"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/10\/phpmind-password-dialog.gif\" alt=\"phpmind-password-dialog\" width=\"400\" height=\"216\" class=\"aligncenter size-full wp-image-1657\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/10\/htaccess-logo.png\" alt=\"htaccess-logo\" width=\"600\" height=\"200\" class=\"aligncenter size-full wp-image-1658\" srcset=\"https:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/10\/htaccess-logo.png 600w, https:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/10\/htaccess-logo-300x100.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><br \/>\nYou can secure any file using this script.<br \/>\nIt will not allow you to access without username and password. <\/p>\n<p>1st step is to create .htaccess file in a folder which you want to protected.<br \/>\n2nd step is add index.php as shown in example. This will not work as it is but you can modify. <\/p>\n<pre lang=\"php\">\r\n\/*******************************************************\/\r\n\r\n\r\n.htaccess file\r\n\r\n\/********************************************************\/\r\n\r\nOptions -Indexes \r\n\r\nRewriteEngine On\r\n\r\nRewriteCond $0 !^(favicon\\.ico|favicon\\.png|media|robots\\.txt|crossdomain\\.xml|css|js)\r\n\r\nRewriteRule .* index.php?file=$0 [QSA,L] # pass everything thru php\r\n \r\n\r\n \r\n<\/pre>\n<pre>\r\n<?php\r\nsession_start();\r\ninclude_once 'Sessionconfig.php';\r\n\r\n\r\n$file = isset($_GET['file']) ? $_GET['file'] : null;\r\n$file = $file === 'index.php' ? null : $file;\r\n$file = $file ? realpath($file) : null;\r\n\r\nif (!$file || strpos($file, Config::ABS_PATH . 'protected\/') === false) {\r\n\texit();\r\n}\r\n\r\nif(!classCheck::isInSession())\r\n{\r\n\theader(\"location: \".Config::ABS_URL . \"login.php\");\r\n\texit;\r\n}\r\nelse {\r\n\t$suffix = substr($file, strlen($file) - 4, strlen($file) - 1);\r\n\tif ($suffix == \".css\") {\r\n\t\theader(\"Content-type: text\/css\", true);\r\n\t\t$mime_t = \"text\/css\";\r\n\t}\r\n\treadfile($file);\r\n\texit;\r\n}\r\n?>\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can secure any file using this script. It will not allow you to access without username and password. 1st step is to create .htaccess file in a folder which you want to protected. 2nd step is add index.php as shown in example. This will not work as it is but you can modify. \/*******************************************************\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[33,3],"tags":[],"class_list":["post-1656","post","type-post","status-publish","format-standard","hentry","category-htaccess","category-php"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1656"}],"version-history":[{"count":6,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1656\/revisions"}],"predecessor-version":[{"id":1677,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1656\/revisions\/1677"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}