2016-11-02 102 views
2

創建使用NGINX PHP和PHP-FPM文件使用PHP-FPM,nginx的,CentOS的6.4 文件被稱爲= register.php 我不能在我的目錄/.../wingd/profiles不能在CentOS 6.4

目標創建一個文件:當用戶註冊我的網站時,通過創建文件來生成用戶配置文件。

錯誤:

Warning: fopen(profiles/test.txt): failed to open stream: Permission denied in /usr/share/nginx/wingd/register.php on line 150

在我register.php我:

$file = fopen("profiles/test.txt", "w") or die("Unable to open file!"); 

事情我已經嘗試:

  1. chmod 777的所有文件和相關的文件夾(在/ usr /共享/ nginx的/ wingd /配置文件中列出的所有留下的所有目錄都是777目前包括「register.php」(我的理解是777安全擔憂,並擔心安全問題以後,一旦我明白了當前的問題,以及如何解決)

  2. 曾用:它表明我的腳本所有者和腳本的亞軍是nginx的用戶

echo 'Current script owner: ' . get_current_user() . "<br>"; 
        echo 'posix: '.posix_getuid()."<br>"; 
        echo getcwd()."<br>"; 
        $last_line = system('whoami', $retval); 
        echo $last_line . " " . $retval; 

  1. 已使nginx成爲上述#1中所有文件/目錄的所有者。
  2. 已經看過/etc/php-fpm.d/www.conf,看到「用戶= nginx的」和「組= nginx的」當前
  3. 我沒有/var/www文件夾,我看到了很多類似的問題,指的是/var/www這些論壇帖子的(這是Apache的事情而已?)
  4. 安裝strace的,但我不能確定如何使用它或閱讀它的strace php register.php
  5. 產量能夠su nginx然後使用觸摸命令觸摸/profiles/test.txt。它確實創造了無差錯
  6. 文件被告知,我需要找出php-fpm使用用戶名以及它的權限設置,不知道如何做到這一點。我在另一個stackoverflow主題上看到,php可能使用apache的用戶名,我試圖給這個用戶所有權的文件,並沒有得到的地方。

我不知道還有什麼我可以試試。

代碼:

<?php 

include ("config.php"); 

?> 
<html> 
<body> 

<a href="index.php">Index</a><br> 
    <form name="registrationForm" method="post" onsubmit="return validateForm()"> 
     <input type="text" class="form-control" placeholder="Enter username" name="username"><font color="red">*</font> 
     <input type="text" class="form-control" placeholder="Enter email address" name="email"><font color="red">*</font> 
     <input type="password" class="form-control" placeholder="Enter password" name="password"><font color="red">*</font> 
     <input type="text" class="form-control" placeholder="Enter first name" name="first"><font color="red">*</font> 
     <input type="text" class="form-control" placeholder="Enter last name" name="last"><font color="red">*</font> 
     <input type="text" class="form-control" placeholder="Enter zip code" name="zip"><font color="red">*</font> 
     <input class="btn btn-default" type="submit" name="submit" value="Submit"> 
    </form> 

    <?php 
    ini_set('display_errors',1); 
    error_reporting(E_ALL); 

    if (isset($_POST['submit'])){ 
    //if($_SERVER["REQUEST_METHOD"] == "POST"){ 
     $username = $_POST["username"]; 
     $email = $_POST["email"]; 
     $password = $_POST["password"]; 
     $first = $_POST["first"]; 
     $last = $_POST["last"]; 
     $zip = $_POST["zip"]; 

     // Check connection 
     if ($db->connect_error) { 
      die("Connection failed: ".$db->connect_error); 
     } 

     //flag to detect if the username exists in db 
     $flag = 0; 
     $sql = "SELECT username FROM users WHERE username = '".$username."';"; 
     $result = $db->query($sql); 
     if ($result->num_rows > 0) { 
      $flag = 1; 
     } 

     //If the username already exists then alert the user, else insert the record to the db and send user to login.php 
     if ($flag == 1){ 
      echo "<script>alert(\"Sorry, that username is already taken. Please choose another.\");</script>"; 
     } 
     else { 
      $sql = "INSERT INTO users (first, last, username, email, password, zip) VALUES ('".$first."', '".$last."', '".$username."', '".$email."', '".$password."', '".$zip."')"; 
      if ($db->query($sql) === TRUE) { 
       //echo "Registration successful"; 
       //$filename = "/profiles/".$username.".php"; 
       //chmod($filename,0777); 
       echo 'Current script owner: ' . get_current_user() . "<br>"; 
       echo 'posix: '.posix_getuid()."<br>"; 
       echo getcwd()."<br>"; 
       $last_line = system('whoami', $retval); 
       echo $last_line . " " . $retval; 
       $file = fopen("profiles/test.txt", "w") or die("Unable to open file!"); 
       //$txt = "This is a user profile for ".$username; 
       //fwrite($file, $txt); 
       fclose($file); 
       //header('Location: login.php'); 
      } else { 
       echo "Registration error, please try again later."; 
      } 
     } 
    } 
    $db->close(); 
    ?> 
</body> 

+0

'在/ var/www'是對於許多Web服務器默認的DocumentRoot的共同選擇。請參閱http://unix.stackexchange.com/questions/47436/why-is-the-root-directory-on-a-web-server-put-by-default-in-var-www。如果它不是您的DocumentRoot(或任何nginx調用它),則替換您的系統使用的路徑。 (對不起IDK的答案,在審查編輯時看到了這個問題。) –

+0

該配置文件不能成爲數據庫的一部分嗎?你的意思是排除fopen上的主要斜線嗎? (/profiles/test.txt vs profiles.txt)。 phpinfo()可能會有所幫助,請檢查open_basedir或safe_mode條目。 – strobelight

+0

謝謝你們兩位。彼得,我不知道如何處理DocumentRoot,即使我知道nginx放在哪裏,但我認爲它是/ usr/share/nginx/wingd是doc根,但不知道是什麼意思。 wingd文件夾是網站上顯示的內容,包含我的index.php和其他相關文件。 Strobelight,是的,用戶配置文件將從數據庫中填充。這個想法是一個用戶將註冊的網站和配置文件頁面將生成。如果我使用/profiles/test.txt,那麼這將來自根,並認爲是絕對路徑正確? – Retro

回答

0

所以我不再去嘗試創造潛在的幾百,幾千等,以實現我的想法..的用戶配置文件文件在我的服務器上,而是遵循現在似乎是慣例,只是有一個配置文件頁面,然後填充基於參數的數據。這個參數(比如用戶id或用戶名)可以用於從數據庫中拉出來,並且你沒有文件的長度,並且對於安全問題也更好。感謝所有花時間閱讀和/或回覆此問題以幫助我的人。希望這篇文章能夠幫助其他用戶在將來使用我的技術堆棧和初步設計思想。外賣:詢問如何最好地解決問題/想法,而不是詢問如何最好地解決您正在處理的問題,與您當前的設計/實施理念。

見一個更好的解釋和代碼片段如下:Generate Profile Page Upon Registration - PHP