2017-05-11 91 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