我在 Mac 上遇到了 Apache 服务器权限问题

我在 Mac 上遇到了 Apache 服务器权限问题

当我尝试创建登录页面时出现此错误:

Warning: file_put_contents(/users/userlist.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/register_draft.php on line 17

Warning: file_put_contents(/users/passlist.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/register_draft.php on line 18

Warning: file_put_contents(/users/pinlist.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/register_draft.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/register_draft.php:17) in /Library/WebServer/Documents/register_draft.php on line 20

我在 /Library/Webserver/Documents/ 中创建了一个名为“users”的文件夹,用于存放所有用户

这是我的代码:

<?php
$file1 = '/users/userlist.txt';
$file2 = '/users/passlist.txt';
$file3 = '/users/pinlist.txt';
$user = usermail;
$pass = password;
$pin = pi;

$safeUser = testUserInput($user);
$safePass = testUserInput($pass);
$safePin = testUserInput($pin);

if ($safeUser != "unsafe" && $safeUser != "empty" && $safeUser != "spaces") {
if ($safePass != "empty" && $safePass != "unsafe" && $safePass != "spaces") {
    if ($safePin != "empty" && $safePin != "unsafe" && $safePin != "spaces") {
        $hashedPass = password_hash($safePass, PASSWORD_BCRYPT);
        file_put_contents($file1, $safeUser . "<br>", FILE_APPEND | LOCK_EX);
        file_put_contents($file2, $hashedPass . "<br>", FILE_APPEND | LOCK_EX);
        file_put_contents($file3, $safePin . "<br>", FILE_APPEND | LOCK_EX);
        header("Location: ../users"); //change this to direct user to login page, account successfully created
}
}
}     else {
header("Location: ../users"); //change this to direct user back to     registration page with error prompt
}

function testUserInput($x) {
$test = strpbrk($x,"<>/\;()%-+.=,");
if ($test != false) {
    $x = "unsafe";  
} elseif (strlen($x) == 0) {
    $x = "empty";
} elseif (ctype_space($x)) {
    $x = "spaces";
}
return $x;
}
?>

如果您发现代码中存在任何其他问题,请告诉我。谢谢。

答案1

请尝试以下方法:

  1. 每当您收到此类消息时,请以 sudo 身份运行命令
  2. 对于您的文档根目录,执行:sudo chown _www:_www /path_to_docroot/

如果这不能解决问题,请分享结果,我们可以进一步调试。

相关内容