07-26-2008, 11:38 PM
|
#2 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 136
Thanks: 4
|
PHP Code:
<?php
# Function: Add User
function add_user($user, $pass, $url){
$fopen = fopen('info.txt', 'a');
fwrite($fopen, "\n".$user."' => '".$url."' => '".$pass."',");
fclose($fopen);
}
# Check to see if the form is submitted
if ( isset ( $_POST['submit'] ) ){
# If yes: Grab submitted values !Should be sanitised!
$username = $_POST['username'];
$password = $_POST['password'];
$url = $_POST['url'];
# Then use our function to add them to the txt
add_user($username, $password, $url );
} else {
# If the form hasnt been submitted, show the form!
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" name="username" value="" />
<input type="text" name="password" value="" />
<select name="url">
<option value="Director/index.php">Director</option>
<option value="Admin/index.php">Admin</option>
<option value="User/index.php">User</option>
</select><br />
<!-- Like the inputs above, give the input a name so we can call it above -->
<input type="submit" name="submit" /><br />
</form>
<?php
}
?>
</body>
</html>
I have added comments into the code. Ask if you need any help :)
EDIT:
I have added in some validation:
PHP Code:
<?php
# Function: Add User
function add_user($user, $pass, $url){
$fopen = fopen('info.txt', 'a');
fwrite($fopen, "\n'" . $user."' => '".$url."' => '".$pass."',");
fclose($fopen);
}
# Check to see if the form is submitted
if ( isset ( $_POST['submit'] ) ){
# If yes: Grab submitted values !Should be sanitised!
$username = $_POST['username'];
$password = $_POST['password'];
$url = $_POST['url'];
# Check to make sure user has actually inputted something!
# || means that either of the conditions needs to be true/false
# Therefore this reads: if the username is or AND the password is empty
# show a message :(
if ( empty($username) || empty($password) ){
# If we need to show our user this; they have left an input empty..
echo ':(';
} else {
# Then use our function to add them to the txt
add_user($username, $password, $url );
echo ':)';
}
} else {
# If the form hasnt been submitted, show the form!
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" name="username" value="" />
<input type="text" name="password" value="" />
<select name="url">
<option value="Director/index.php">Director</option>
<option value="Admin/index.php">Admin</option>
<option value="User/index.php">User</option>
</select><br />
<!-- Like the inputs above, give the input a name so we can call it above -->
<input type="submit" name="submit" /><br />
</form>
<?php
}
?>
</body>
</html>
|
|
|
|