什么PHP脚本将执行此功能:

时间:2013-12-20 20:24:17

标签: php html .htaccess

我正在寻找一个工作的PHP脚本,可以记录/保存尝试的用户名和密码组合来访问我的受保护的.htpasswd文件(.htaccess文件将人们重定向到这个php脚本文件)。我找到了这个:

<?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();
?>

但是当我尝试使用它时,我收到此错误消息:

  

解析错误:语法错误,意外T_STRING,期待','或';'在第20行的/web/user/log-htpasswd.log中

任何人都知道可以使用的php脚本吗?

4 个答案:

答案 0 :(得分:1)

您有2个选项

错误:下面的代码正文导致了解析错误,这是由于doesn't

这个词中未转义的引号造成的
  • 选项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>';

对此:(在使用"-//IETF//DTD HTML 2.0//EN"

时转义引号\"

并为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>";
  • 选项2:

根据以下内容将doesn't更改为doesn\'t

Eduardo Stuart's answer 也是解决方案,如果不是最好的话。

答案 1 :(得分:0)

检查第19行的问号,你需要逃避它们:

browser doesn\'t understand how to supply

答案 2 :(得分:0)

转义"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();
?>

答案 3 :(得分:0)

执行类似

的操作
$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);