2017-05-11 26 views
-2

全部。登錄/目錄創建者

我有一個很大的問題,我不知道如何去解決它。

我想創建一個登錄/註冊表單,它爲每個新用戶在服務器上創建一個目錄。這個目錄顯然不能被其他用戶公開訪問,並且必須自動複製/製作它的index.php文件和子目錄。

恐怕我不會有太多的運氣,因爲我從來沒有做過任何形式的加密登錄,更不用說像這樣的花式自動化。

沒有代碼。任何幫助感謝!

+0

這個目錄應該放在哪裏?在根文件夾? – Demonyowh

+0

這是一個非常寬泛的問題。在提出另一個問題之前,請嘗試閱讀[問]部分。 –

+0

當你需要一個新的目錄時,在每次登錄時;或在每個新的註冊?爲什麼它是必需的? –

回答

0

那麼你需要兩樣東西:

所有系統的第一次,你可以創建目錄和文件:

1 PHP(如果你不介意)文件中創建文件: 示例代碼:

<form action="function-create-new-file.php" method="post"> 
 
\t WHAT IS THE NAME OF YOUR PAGE 
 
    <input name="file_name" type="text" /> 
 
\t <br /> 
 
    KIND OF FILE YOU WISH TO CREATE 
 
\t <select name="file_ext"> 
 
\t <option value=".php" selected>php</option> 
 
     <option value=".html">php</option> 
 
\t </select> 
 
    <br /><br /><br /> 
 
    <input type="submit" class="btn" name="submit" value="Add Page" /> 
 
\t </form>

此文件將創建您的文件,你應該包括這一項來處理它:

<?php 
 
\t if(isset($_POST['submit'])) //has the form been submitted? 
 
\t { 
 

 
\t $file_directory = "./test/"; //the directory you want to store the new file in 
 
\t $file_name = strip_tags($_POST['file_name']);//the file's name, stripped of any dangerous tags 
 
\t $file_ext = strip_tags($_POST['file_ext']); //the file's extension, stripped of any dangerous tags 
 
\t $file = $file_directory.$file_name.$file_ext; //this is the entire filename 
 
\t $create_file = fopen($file, "w+"); //create the new file 
 
\t if(!$create_file) 
 
\t 
 
\t { 
 
\t die("ERROR, NOT POSSIBLE TO COMPLETE YOUR DESIRED INSTRUCTION"); 
 
\t } 
 
\t 
 
\t $chmod = chmod($file, 0755); //set the appropriate permissions. 
 
\t //attempt to set the permissions 
 
\t if(!$chmod) 
 
\t { 
 
\t 
 
\t //error changing the file's permissions 
 
\t echo("ERROR IN THE PERMISSIONS FOR THIS ACTION, PLEASE CHMOD 0755 THIS FILE AND FOLDERS"); 
 
\t echo "<br /><br /><br /><br /><br /><br /><br />"; 
 
\t include ("footer.php"); 
 
\t } 
 
\t 
 
\t //defenition of the new page contante self created 
 
\t if (fwrite 
 
\t \t ($create_file, "this is the content of your new page!") === FALSE) 
 
\t \t { 
 

 
\t echo "ERROR IN FILE: ($file)\n"; 
 
\t } 
 
\t 
 
\t fclose($create_file); 
 
\t //tell the user that the file has been created successfully 
 
\t echo "The file was created! Take a look ate your file here - <a href='$file' target='_blank'>$file</a>"; 
 
\t exit; //exit the script 
 
\t } 
 
\t 
 
\t else{ //the form hasn't been submitted! 
 
\t header("Location: function-create-new-file.php"); //redirect the user back to the add file page 
 
\t exit; //exit the script 
 
\t } 
 
\t ?>

現在第二,你需要創建一個目錄:

<?php 
 
// Desired folder structure 
 
$structure = './depth1/depth2/depth3/'; 
 

 
// To create the nested structure, the $recursive parameter 
 
// to mkdir() must be specified. 
 

 
if (!mkdir($structure, 0777, true)) { 
 
    die('Failed to create folders...'); 
 
} 
 

 
// ... 
 
?>

也請您查看此鏈接:How to Create a Directory

現在,您需要創建登錄系統的用戶。你可以在PHP中再次這樣做。

<?php 
 

 
session_start(); 
 
function encode_5t_base64($str) 
 
{ 
 
    for($i=0; $i<5;$i++) 
 
    { 
 
    $str=strrev(base64_encode($str)); 
 
    } 
 
    return $str; 
 
} 
 
function jh_password_protection($pass_string) 
 
{ 
 
\t $pass_string = encode_5t_base64($pass_string); 
 
\t $pass_string = md5($pass_string); 
 
\t $pass_string = sha1($pass_string); 
 
\t return $pass_string; 
 
} 
 
function registerUser($user,$pass1,$pass2) 
 
{ 
 
\t $errorText = ''; 
 
\t if ($pass1 != $pass2) $errorText = "Password must be the same"; 
 
\t elseif (strlen($pass1) < 6) $errorText = "Password too short"; 
 
\t $pfile = fopen("some/path/to/store/member/password.php","a+"); 
 
    rewind($pfile); 
 
    while (!feof($pfile)) 
 
\t { 
 
     $line = fgets($pfile); 
 
     $tmp = explode(':', $line); 
 
     if ($tmp[0] == $user) 
 
\t \t { 
 
      $errorText = "That user already exists"; // check it you made duplicated members 
 
      break; 
 
     } 
 
    } 
 
    if ($errorText == '') 
 
\t { 
 
\t \t $userpass = jh_password_protection($pass1); 
 
    \t 
 
\t \t fwrite($pfile, "\r\n$user:$userpass"); 
 
    } 
 
    fclose($pfile); 
 
\t return $errorText; 
 
} 
 
function loginUser($user,$pass) 
 
{ 
 
\t $errorText = ''; 
 
\t $validUser = false; 
 
\t $pfile = fopen("path/to/your/user/page.php","r"); 
 
    rewind($pfile); 
 

 
    while (!feof($pfile)) { 
 
     $line = fgets($pfile); 
 
     $tmp = explode(':', $line); 
 
     if ($tmp[0] == $user) 
 
\t \t { 
 
      if (trim($tmp[1]) == trim(jh_password_protection($pass))) 
 
\t \t \t { 
 
      \t $validUser= true; 
 
      \t $_SESSION['userName'] = $user; 
 
      } 
 
      break; 
 
     } 
 
    } 
 
\t 
 
    fclose($pfile); 
 
    if ($validUser != true) 
 
\t $errorText = "That went wrong"; // failed login message 
 
    
 
    if ($validUser == true) $_SESSION['validUser'] = true; 
 
    else $_SESSION['validUser'] = false; 
 
\t 
 
\t return $errorText; \t 
 
} 
 
function logoutUser() 
 
{ 
 
\t unset($_SESSION['validUser']); 
 
\t unset($_SESSION['userName']); 
 
} 
 
function checkUser() 
 
{ 
 
\t if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)) 
 
\t { 
 
\t \t header('Location: path/to/your/user/page.php'); // Your user location 
 
\t \t exit(); 
 
\t \t 
 
\t } 
 
} 
 
?>

現在讓我們使登錄系統,第一步,因爲你希望創建一個登錄頁面,並放置在內容類型標籤驗證碼:

<meta http-equiv="Pragma" content="no-cache">

樣本登錄系統表格:

<?php 
 
\t if ($error == '') { 
 
\t \t echo "Welcome"; 
 
\t \t echo "<a href='./path/to/your/member/area/'>Proceed</a>"; 
 
\t \t } 
 
\t \t else echo " $error "; 
 
\t ?> 
 
\t 
 
\t 
 
\t <?php if ($error != '') {?> 
 
    <form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"> 
 
\t <br/><br/> 
 
\t <label>USERNAME</label> 
 
\t <br/><br/> 
 
\t <input name="username" type="text"> 
 
\t <br/><br/> 
 
\t <label>PASSWORD</label> 
 
\t <br/><br/> 
 
\t <input name="password" type="password"> 
 
\t <br/><br/> 
 
\t <input value="LOGIN" name="submitBtn" type="submit"> 
 
\t </form> 
 
    <?php } 
 
\t if (isset($_POST['submitBtn'])){ 
 
\t ?>

在你的頁面的頂部正好有這樣的:

<?php 
 
\t require_once('file/that/operates/the/login.php'); 
 
\t $error = 'wow that was wrong'; 
 
\t if (isset($_POST['submitBtn'])){ 
 
\t $username = isset($_POST['username']) ? $_POST['username'] : ''; 
 
\t $password = isset($_POST['password']) ? $_POST['password'] : ''; 
 
\t $error = loginUser($username,$password);} 
 
?>

我想我沒有忘記什麼在這裏,但讓我知道是否有幫助。

+0

這看起來很棒!我一定會在下一次的機會中嘗試一下(幾個小時內)。會讓你知道我是怎麼走的。 – Doofitator

+0

我在我的舊系統中使用過這個,但是你應該在mysql中執行此操作。我也可以幫助你。最好的問候,所有最好的 –

+0

好吧。我遇到了一個問題。不知道如何繞過它。請參閱[鏈接](https://preview.ibb.co/mEZTO5/Screen_Shot_2017_05_11_at_4_57_10_pm.png)。請注意,第139行是我文檔的最後一行。 – Doofitator

-1
在成功註冊後處理寄存器功能 你的PHP文件

你應該添加

mkdir("yourtargetpath/" . $username, 0700); 
+1

這不回答OP的問題。這只是創建一個目錄,只有擁有該目錄的服務器用戶才能讀取,寫入或執行([chmod permissions])(http://www.thinkplexx.com/learn/article/unix/command/chmod-permissions-flags -explained-600-0600-700-777-100-等))。 OP想要一個完全成熟的加密登錄系統。 –

0

IMO將數據存儲在數據庫中而不是文件會更清潔和更容易。

但是如果你真的需要去與不同的文件夾這裏有一個解決方案:

  • 創建一個簡單的登錄/註冊表單。
  • 註冊後,在父文件夾中創建用戶文件夾,例如上傳
  • 然後你只需要配置您的服務器(你可以做到這一點與的.htaccess),使每個請求的上傳文件夾被重定向到一個控制器(例如的index.php
  • 指數.php,檢查用戶是否已登錄,並且他試圖訪問的文件夾屬於他。如果是,則呈現請求的文件,如果沒有,則呈現錯誤消息。
+0

謝謝。這將是有益的:) – Doofitator