Back Home

Open Designs

Community. Driven.

  1.  
    Is It possible to create a new directory on command with Flatfile PHP?
    Where am I going with this...
    I Am messing around and i wanted to see if people can register a username and pw then have them created... they would them go to a url: such as this and type in their registered pw... then instead of it saying /host in the url it would have their username... which they just created... I have al the other stuff down.. Just, is there a way to create a new directory through a form with php?

    Thank YOu
    •  
      CommentAuthorperthmetro
    • CommentTimeMar 20th 2008
     
    Yes... 'Filethingie' does it
  2.  
  3.  
    Sweet! that is so cool.... I Love PHP... Is there then a way to deposit a package of files to fill the newly made directory as it is created?
    • CommentAuthorseptor
    • CommentTimeMar 21st 2008 edited by septor on the 21st March 2008 at 10:11:36 EDT
     
  4.  
    copy is probably the easiest for a beginner, it'll just create a duplicate of an existing file (e.g. in a default folder).

    p.s. you wouldn't have a semicolon after a for() loop tongue
    • CommentAuthorseptor
    • CommentTimeMar 21st 2008
     
    Posted By: Christopher
    p.s. you wouldn't have a semicolon after a for() loop tongue


    Oops. I'd say I copied and pasted and just forgot to remove it, but it's the first link so I have no good excuse. shocked
    • CommentAuthorconartistdesigns
    • CommentTimeMar 22nd 2008 edited by conartistdesigns on the 22nd March 2008 at 13:53:42 EDT
     
    Ive Been trying stuff all day and I have really confused myself... will this work?

    <?php
    mkdir($_POST["username"], 0700);

    ?>
    <?php
    $file = 'test.txt';
    $newfile = '$_POST["username"]/test.txt';

    ?>
  5.  
    No, did you test it?

    Create a file called create.php and put this code in it:

    <?php
    // If we're submitting the form
    if($_POST) {
    // Send all posted information into an array
    foreach($_POST as $key => $value)
    $data[$key] = $value;
    // List the template placeholders
    $placeholders = array(
    '{username}',
    '{time}'
    );
    mkdir($data['username']);
    // Create the new user page using the data posted
    $fp = fopen($data['username'] . '/index.html', 'w+');
    fwrite($fp, str_replace($placeholders, $data, file_get_contents('template.html')));
    fclose($fp);
    } else
    // The form to submit the request
    echo '<form action="" method="post">
    <fieldset>
    <legend>Create your user page</legend>
    <label for="username">Username: <input id="username" name="username" type="text" />
    <input name="created" type="hidden" value="' . date('l j F Y') . '" />
    <input name="action" type="submit" value="Create" />
    </fieldset>
    </form>';
    ?>

    Then create a file called template.html in the same directory and put this code in it:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb">
    <head profile="http://gmpg.org/xfn/11">
    <title>user / {username}</title>
    <meta http-equiv="content-language" content="en-gb" />
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
    <style media="screen" type="text/css">
    body {
    background:#fff;
    color:#333;
    font:100.1% Arial, Helvetica, sans-serif;
    }
    * {
    margin:0;
    padding:0;
    }
    p {
    font-size:.7em;
    padding:10px;
    }
    </style>
    </head>

    <body>
    <p>This is the user page of {username}, it was created on {time}.</p>
    </body>
    </html>

    This is a very basic example and has no input checking so do not use it in a production environment! But you might be able to learn a few basics from it.
  6.  
    Wow. Thanks Christopher!!! in the create.php can I change template.html to a directory and it will copy a whole directory?
    • CommentAuthorseptor
    • CommentTimeMar 23rd 2008 edited by septor on the 23rd March 2008 at 20:17:26 EDT
     
    No, because file_get_contents() gets the contents of a file.

    Research the copy() function as already mentioned above. You would use that new found knowledge in place of:

    $fp = fopen($data['username'] . '/index.html', 'w+');
    fwrite($fp, str_replace($placeholders, $data, file_get_contents('template.html')));
    fclose($fp);
    • CommentAuthorconartistdesigns
    • CommentTimeMar 23rd 2008 edited by conartistdesigns on the 23rd March 2008 at 22:19:48 EDT
     
    thank you all. I now have it so it deposites a form that requires the user who created a directory to create a password. The password is written from a form to a txt file in the newly created directory. I have that part down. A file called login.php in also installed in the directory. The action to this form is profile.php. It is meant to open the txt file and see if the password matches... if it does it does an include for upload.php. The problem is that I cant get it to work. this is my code for profile.php:
    <?php
    $fp = fopen('password.txt', 'r');
    if (!$fp) {
    echo 'An Error Occurred';
    }
    while (false !== ($char = fgetc($fp))) {
    if ($_POST["password"]=$char
    include("upload.php");
    else
    echo("wrong");
    }
    ?>

    I know there is wrong stuff in it... Ijust dont know what it is and whot it should be... THanks
    •  
      CommentAuthorgnome
    • CommentTimeMar 23rd 2008
     
    A hashing function would be nice if you are actually going to use it (so that you aren't storing passwords as plain text).
  7.  
    gnome... so true... i dont exactally know how to do that. It's still flat file, correct?
    • CommentAuthorseptor
    • CommentTimeMar 24th 2008 edited by septor on the 24th March 2008 at 01:37:21 EDT
     
    Check out the hash() and the md5() functions. It will help you encrypt the passwords so they aren't easily obtainable/readable.
  8.  
    does hash encrypt and md5 decrypt... thats kind what i got from it?
  9.  
    also none of the hash examples work.... I get the same error every time... just copying and pasting.
    •  
      CommentAuthorgreg
    • CommentTimeMar 24th 2008 edited by greg on the 24th March 2008 at 18:44:32 EDT
     
    the whole point of md5 is that you (theoretically) can't decrypt it - so even if someone manages to get a hold of your hashed password, it's useless to them (which isn't really true, but it's more secure than them getting hold of a plain-text password). what you would need to do is md5() the password that the user enters when they try to log in, and compare the hash to the stored hash value - if it's a match, then they got the password correct.
  10.  
    that makes sence but could you go a little more indepth... Im just a newb loving the wonders of php
    • CommentAuthorseptor
    • CommentTimeMar 24th 2008 edited by septor on the 24th March 2008 at 23:45:25 EDT
     
    Before you put the password into the text file run it through the hash() function.

    When you run through the form of them putting their password in, put the password through the md5() function and see if the result matches what's in the text file.

    edit:

    So..

    $theirpassword = hash($theirpassword);

    //code to insert $theirpassword into the text file


    Then when they enter a password into the form and hit submit..

    $passwordattempt = md5($_POST['password']);

    //code to pull the hashed password from the text file

    if($passwordattempt == $theirpassword){

    // do something

    }
  11.  
    I m sorry im so dependent on you guys.. but how would you implement this into the form.
    •  
      CommentAuthorgreg
    • CommentTimeMar 25th 2008
     
    you would just need to md5() the password as you put it into the text file, and then md5($_POST['password']) when the user attempts to log in.
  12.  
    so I found a WORKING tutorial on password registration and hashing... but it uses sql not flatfile... I need some advice on what to change for it to write to a txt file and not a database.... this is the exxample code..

    <?php

    /* Store user details */

    $passwordHash = sha1($_POST['password']);

    $sql = 'INSERT INTO user (username,passwordHash) VALUES (?,?)';
    $result = $db->query($sql, array($_POST['username'], $passwordHash));

    ?>


    <?php

    /* Check user details */

    $passwordHash = sha1($_POST['password']);

    $sql = 'SELECT username FROM user WHERE username = ? AND passwordHash = ?';
    $result = $db->query($sql, array($_POST['username'], $passwordHash));
    if ($result->numRows() < 1)
    {
    /* Access denied */
    echo 'Sorry, your username or password was incorrect!';
    }
    else
    {
    /* Log user in */
    printf('Welcome back %s!', $_POST['username']);
    }

    ?>
  13.  
    Septor: I attempted you method and I cant get it to work.
    Here is my form code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <form id="form1" name="form1" method="post" action="password-action.php">
    <label>Please Create a Password :
    <input name="password" type="text" id="password" />
    </label>
    <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
    </p>
    </form>
    </body>
    </html>


    and here is the action:
    <?php
    $filename = "password.txt";
    $password = md5($_POST['password']);
    $fp = fopen ($password, "w");
    if ($fp) {
    fwrite ($fp, $password);
    fclose ($fp);
    echo ("Your Password is now written Please Attempt to Login") ;
    }
    else {
    echo ("File was not written");
    }
    ?>
    •  
      CommentAuthorgreg
    • CommentTimeMar 28th 2008
     
    your fopen() function is attempting to open $password - i think you want to be opening $filename :)
    also it would be a good idea to set the input type of the password text box to 'password' rather than 'text'.
    • CommentAuthorconartistdesigns
    • CommentTimeMar 28th 2008 edited by conartistdesigns on the 28th March 2008 at 01:16:27 EDT
     
    I changed it but it still doesn't work
    •  
      CommentAuthormarkwest
    • CommentTimeMar 28th 2008
     
    With the changes that greg suggested the code works as designed (at least for me). The php code puts an MD5 hash of the password POST var into the file password.txt.

    Is this what you were expecting it to do?

    -Mark
  14.  
    I was expecting to look at the password.txt after the form was submited and see a bunch of 6s6g8dfb5ba65h54ja6df5hb5jar5j6AFQetjkx6 instead of the password i had entered. and i saw nothing in the txt file
    •  
      CommentAuthorgnome
    • CommentTimeMar 28th 2008
     
    Conrad: permissions set?
  15.  
    yes... ive trid 666 and 777
  16.  
    i got it working thanks
    • CommentAuthorconartistdesigns
    • CommentTimeMar 30th 2008 edited by conartistdesigns on the 30th March 2008 at 00:29:06 EDT
     
    Septor: I got the pw writing thing working.... but not the verification... this is my code... i don't know whats wrong... i dont get an error... it just echos wrong every time

    Login.php
    <html><title>Step 1: Login</title>
    <body>
    <form action="login-action.php" method="post">
    Password:
    <input type="text" name="password" />
    <input type="submit" />
    </form>


    </body>
    </html>

    Login-action.php
    <?php
    $password=fopen("password.txt","r") or exit("Unable to open file!");

    $attempt = md5($_POST['password']);

    if($attempt == $password)
    echo ('Correct');

    else
    echo ('Wrong');

    fclose($password);
    ?>
    •  
      CommentAuthorgreg
    • CommentTimeMar 30th 2008
     
    echo $attempt and $password to see what the values are - it may give some clue as to what's going wrong.
  17.  
    I echoed $password and it gave "Resource id #2" and I echoed $attempt and it md5'd the $_POST['password'] like I wanted it to. what is "Resource id #2"
    •  
      CommentAuthorgnome
    • CommentTimeMar 30th 2008
     
    $password is a reference to a file right now, and you need it to be a string matching the contents of the file. I don't have time to rewrite your code for you, but it involves fread() bakie
  18.  
    I got it thank you gnome