-7

I'm looking for a working php script that will work to log/save attempted username and password combinations to access my protected .htpasswd file (the .htaccess file is redirecting people to this php scripted file). I found this one:

<?php
define('LOGINS_LOG','/web/user/log-htpasswd.log');

if(isset($_ENV['REDIRECT_REMOTE_USER']) && !empty($_ENV['REDIRECT_REMOTE_USER'])){
 $fp = fopen(LOGINS_LOG, 'a+');
 fwrite($fp, $_ENV['REDIRECT_REMOTE_USER']);
 fclose($fp);
}

ob_start();
header("HTTP/1.1 401 Authorization Required",1);
header("Status: 401 Authorization Required",1);
echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head><title>401 Authorization Required</title></head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>';
exit;
exit();
?>

But when i attempt to use it i get this error message:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /web/user/log-htpasswd.log on line 20

Anyone know a php script that will work?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user3112748
  • 15
  • 1
  • 4

4 Answers4

1

You have 2 options

Fault: Your body of code below, caused a parse error and this is due to an unescaped quote in the word doesn't

  • Option 1:

Change this body of text/code:

echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head><title>401 Authorization Required</title></head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>';

to this: (while escaping the quotes "-//IETF//DTD HTML 2.0//EN") with \"

and using double quotes for your echo.

echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<html><head><title>401 Authorization Required</title></head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>";
  • Option 2:

Change doesn't to doesn\'t as per:

Eduardo Stuart's answer which is also a solution, if not the best actually.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Check your question marks on line 19, you need to escape them:

browser doesn\'t understand how to supply
esqew
  • 42,425
  • 27
  • 92
  • 132
0

escape "doesn\'t"

<?php
define('LOGINS_LOG','/web/user/log-htpasswd.log');

if(isset($_ENV['REDIRECT_REMOTE_USER']) && !empty($_ENV['REDIRECT_REMOTE_USER'])){
 $fp = fopen(LOGINS_LOG, 'a+');
 fwrite($fp, $_ENV['REDIRECT_REMOTE_USER']);
 fclose($fp);
}

ob_start();
header("HTTP/1.1 401 Authorization Required",1);
header("Status: 401 Authorization Required",1);
echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head><title>401 Authorization Required</title></head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn\'t understand how to supply
the credentials required.</p>';
exit;
exit();
?>
Eduardo Stuart
  • 2,869
  • 18
  • 22
0

Do something like

$content = date('m-d-Y g:i:sa')." | ".$_SERVER['REMOTE_ADDR']." has visited
";
$fp = fopen("C:\\logs\\accesslog.txt","a");
fwrite($fp,$content);
fclose($fp);
RugerSR9
  • 448
  • 1
  • 4
  • 17
  • This script only results in the accesslog.txt saying: 12-20-2013 2:56:28pm | IP address has visited. How do i make it tell user/pass entered to access as well? – user3112748 Dec 20 '13 at 20:57
  • Well of course you have to edit the $content variable to equal whatever information that you want it to log. – RugerSR9 Dec 20 '13 at 21:02
  • Unfortunately I'm really novice...what exactly would the script then look like? I tried combing the two scripts that way i THOUGHT it would work and got errors. Thanks in advance. – user3112748 Dec 20 '13 at 21:08