什么 PHP 脚本将执行此功能:[关闭]

Posted

技术标签:

【中文标题】什么 PHP 脚本将执行此功能:[关闭]【英文标题】:What PHP Script will do this function: [closed] 【发布时间】:2014-01-09 18:35:58 【问题描述】:

我正在寻找一个有效的 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,需要 ',' 或 ';'在 /web/user/log-htpasswd.log 第 20 行

有人知道一个可以运行的php脚本吗?

【问题讨论】:

当你写“doesn't”时你打破了你的回声,因为它包含一个' 正确转义引号。 尝试像这样反斜杠引号:does\'t PHP Parse/Syntax Errors; and How to solve them? 的可能重复项 这是真正的错误信息:... in /web/user/log-htpasswd.log?似乎不太可能。 【参考方案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也是一个解决方案,如果不是最好的。

【讨论】:

【参考方案2】:

在第 19 行检查你的问号,你需要将它们转义:

browser doesn\'t understand how to supply

【讨论】:

【参考方案3】:

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

【讨论】:

【参考方案4】:

做类似的事情

$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);

【讨论】:

这个脚本只导致 accesslog.txt 说:12-20-2013 2:56:28pm | IP 地址已访问。我如何让它告诉用户/密码也输入访问权限? 当然,您必须编辑 $content 变量以等于您希望它记录的任何信息。 不幸的是我真的是新手......那么脚本到底会是什么样子?我尝试以我认为它会起作用并出错的方式组合这两个脚本。提前致谢。

以上是关于什么 PHP 脚本将执行此功能:[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

PHP脚本无法执行MATLAB

将 websockets 与 PHP 和 MySQL 脚本一起使用的最佳方式是啥? [关闭]

用于从 Twain 源(扫描仪)获取图像的 php 脚本 [关闭]

执行 PHP 脚本以从 Android 连接服务器的路径 [关闭]

执行 PHP 脚本以从 Android 连接服务器的路径 [关闭]

PHP脚本生成一个不需要的奇怪的附加列[关闭]