www.webdeveloper.com
Results 1 to 5 of 5

Thread: Saving Input to a file using PHP or Java

  1. #1
    Join Date
    May 2012
    Posts
    3

    Saving Input to a file using PHP or Java

    Hello Everyone,
    I'm having a bit of trouble writing a small web page. My goal is to have a very basic page that will allow users to input their email address and store it on a server side file such as .txt (or anything really) that way I can go in later and ensure they're all registered correctly.

    I'm using the code below to setup the email field however I'm unable to figure out how to store these once the submit button is pressed. I'd prefer this not use ActiveX or anything like that to run. I'd like this to just be a simple web page with no pop ups or permissions required. If anyone can give me any assistance I'd greatly appreciate it.

    Code:
    <html>
    <body>
    
    Email Address:<input type="text" name="email1">
    <BR>
    <input type="submit" value="Submit">
    
    
    <?PHP
    $email = $_POST['email1'];
    
    
    ?>
    
    </body>
    </html>
    Thanks,
    Kevin

  2. #2
    Join Date
    Jun 2010
    Posts
    16
    PHP Code:
    $email $_POST['email1']; 

    Well for starters, this is an obvious security problem. You need to filter all user input. The above code could make your application vulnerable to a command injection attack.

    To answer your question, you could store any needed information into the $_SESSION superglobal. For example...

    PHP Code:
    <?php

    session_start
    ();

    $_SESSION['email_addr'] = $email;

    // ...more code here

    ?>
    See the online manual for more information:

    http://www.php.net/manual/en/reserve...es.session.php
    Last edited by dukesdemise; 05-22-2012 at 02:14 PM.
    SCJP 2, SCWCD 5, ZCE 5.3

  3. #3
    Join Date
    May 2012
    Posts
    3
    Quote Originally Posted by dukesdemise View Post
    PHP Code:
    $email $_POST['email1']; 

    Well for starters, this is an obvious security problem. You need to filter all user input. The above code could make your application vulnerable to a command injection attack.

    To answer your question, you could store any needed information into the $_SESSION superglobal. For example...

    PHP Code:
    <?php

    session_start
    ();

    $_SESSION['email_addr'] = $email;

    // ...more code here

    ?>
    See the online manual for more information:

    http://www.php.net/manual/en/reserve...es.session.php
    Thanks for the assistance. Once something like this was put in how would I go about converting this to a file which would be easily accessible to myself or other staff?

    Thanks

  4. #4
    Join Date
    May 2012
    Posts
    3
    Anyone?

  5. #5
    Join Date
    May 2012
    Posts
    42
    Of course, filter user input as always. A golden rule: never trust data from an outside source whether it's from a user, an RSS feed, or anyone else.

    Once you've taken all security precautions, you could do something like this:

    PHP Code:
    if ($file fopen('email_addresses.txt''a'))
    {
        if (
    fwrite($file$email) !== false)
        {
            echo 
    "Added email";
        } else {
            echo 
    "Couldn't write to the file."
        
    }
    } else {
        echo 
    "Couldn't open the file.";


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles