TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-26-2008, 08:43 PM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application Selfsubmit form problem

Good day to you all,
I'm working on a php text based login script.
I'm at building the script to add, delete or edit account.

Here my problem :
When I enter a name password, it add it twice to the txt file.
Also, it oly add the ame of the field and not it value.


Here's my code :

PHP Code:
<?php


$username 
$_POST['username'];
$password $_POST['password'];
$url $_POST['url'];

function 
add_user($user,$url,$pass)
{
    
$fopen fopen('info.text''a');
    
fwrite($fopen"\n,'".$user."' => '".$url."' => '".$pass."'");
    
fclose($fopen);
}

add_user('username','password','url');
?> 


<html>
   <body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
         <input type="text" name="username" />
         <input type="text" name="password" />
         <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 />

         <input type="submit" /><br />
         
         
      </form>
      
      
      Preview:<br />
      <?php if(isset($_POST['html'])) echo stripslashes($_POST['html']); ?>
      
      
      
      
   </body>
</html>

Thanks !
Take care !
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 07-26-2008, 11:38 PM   #2 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

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>
Gareth is offline  
Reply With Quote
The Following User Says Thank You to Gareth For This Useful Post:
Peuplarchie (07-27-2008)
Old 07-27-2008, 07:01 AM   #3 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

Thanks, it's now solved !
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:47 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design