<p>I have a PHP script that runs as a CGI program and the HTTP <code>Authenticate</code> header gets eaten and spit out. So I would like to implement some kind of FORM based authentication. As an added constraint, there is no database so no session data can be stored.</p>

<p>I am very open to having a master username and password. I just need to protect the application from an intruder who doesn't know these credentials.</p>

<p>So how would you implement this?</p>

<p>Cookies?</p>

<p>I could present the form and if it validates, I can send back a cookie that is a hash of the IP address come secret code. Then I can prevent pages from rendering unless the thing decrypts correctly. But I have no idea how to implement that in PHP.</p>

[{'answer_id': 17414, 'author': 'Justin Standard', 'author_id': 92, 'author_profile': 'https://Stackoverflow.com/users/92', 'pm_score': 4, 'selected': True, 'text': '<p>A few ways you could do this.</p>\n\n<ol>\n<li><a href="http://fragments.turtlemeat.com/htaccess.php" rel="nofollow noreferrer">htaccess</a> -- have your webserver handle securing the pages in question (not exactly cgi form based though).</li>\n<li>Use cookies and some sort of hashing algorithm (md5 is good enough) to store the passwords in a flat file where each line in the file is username:passwordhash.  Make sure to <a href="http://www.codinghorror.com/blog/archives/000949.html" rel="nofollow noreferrer">salt</a> your hashes for extra security vs rainbow tables. (This method is a bit naive... be <em>very</em> careful with security if you go this route)</li>\n<li>use something like a <a href="http://www.sqlite.org/" rel="nofollow noreferrer">sqlite</a> database just to handle authentication.  Sqlite is compact and simple enough that it may still meet your needs even if you don\'t want a big db backend.</li>\n</ol>\n\n<p>Theoretically, you could also store session data in a flat file, even if you can\'t have a database.</p>\n'}, {'answer_id': 17419, 'author': 'fulmicoton', 'author_id': 446497, 'author_profile': 'https://Stackoverflow.com/users/446497', 'pm_score': -1, 'selected': False, 'text': "<p>... About salt, add the username in your hash salt will prevent someone who knows your salt and have access to your password file to write a rainbow table and crack number of your users's password.</p>\n"}, {'answer_id': 31869, 'author': 'John Douthat', 'author_id': 2774, 'author_profile': 'https://Stackoverflow.com/users/2774', 'pm_score': 1, 'selected': False, 'text': '<p>If you\'re currently using Authenticate, then you may already have an <em>htpasswd</em> file. If you would like to continue using that file, but switch to using FORM based authentication rather than via the Authenticate header, you can use a PHP script to use the same htpasswd file and use sessions to maintain the authentication status. </p>\n\n<p>A quick Google search for php htpasswd reveals <a href="https://web.archive.org/web/20120510143809/http://koivi.com/php-http-auth/" rel="nofollow noreferrer">this page</a> with a PHP function to check credentials against an htpasswd. You could integrate it (assuming you have sessions set to autostart) with some code like this:</p>\n\n<pre><code>// At the top of your \'private\' page(s):\nif($_SESSION[\'authenticated\'] !== TRUE) {\n    header(\'Location: /login.php\');\n    die();\n}\n\n// the target of the POST form from login.php\nif(http_authenticate($_POST[\'username\'], $_POST[\'password\']))\n    $_SESSION[\'authenticated\'] = TRUE;\n</code></pre>\n'}, {'answer_id': 109524, 'author': 'brian d foy', 'author_id': 2766176, 'author_profile': 'https://Stackoverflow.com/users/2766176', 'pm_score': 1, 'selected': False, 'text': "<p>Do you really need a form? No matter what you do, you're limited by the username and password being known. If they know that, they get your magic cookie that lets them. You want to prevent them seeing the pages if they don't know the secret, and basic authorization does that, is easy to set up, and doesn't require a lot of work on your part. </p>\n\n<p>Do you really need to see the Authorization header if the web server takes care of the access control for you?</p>\n\n<p>Also, if you're providing the application to a known list of people (rather than the public), you can provide web-server-based access on other factors, such as incoming IP address, client certificates, and many other things that are a matter of configuration rather than programming. If you explained your security constraints, we might be able to offer a better solution.</p>\n\n<p>Good luck, :)</p>\n"}]
['https://Stackoverflow.com/questions/17376', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/338/']