< Back

Ditch MySQL and Email, Store your Web Form Entries Securely in the AWS Cloud using PHP

Shared hosts are unreliable and insecure. From emails disappearing into the ether (or spam folders), WordPress admins getting hacked, or databases being injected with loads of crap, I’ve had my problems. However, shared hosts are also damn cheap, which apparently makes it all worth the hassle.

In the following article, I’ll show you one helpful option by routing your web form submissions through Amazon’s S3 service.

Set up AWS S3 and SNS

The following steps assume that you already signed up for AWS and that you know your Access Keys (which can be found in your Security Credentials).

S3

We need someplace to store our form entries. It just happens AWS has a solution called S3 that’s super easy to configure.

  1. Log into your AWS console and navigate to S3.
  2. Create a new bucket in the US Standard region.

SNS

SNS provides us with an automated way to get notified when a new form submission comes down the line. It’s also quick to configure:

  1. So, head back to your console’s dashboard and open up SNS.
  2. Create a new topic.
  3. Then, create a new subscription for your topic.
  4. Select a protocol. Since, I’m using email, I’ll add it into the endpoint input. NOTE: Amazon will send you a verification email once you finish this step.

Finally, Link your S3 bucket events with SNS

  1. Navigate back to your new S3 bucket.
  2. Click Properties and then toggle open “Events”
  3. Add a new notification, targeting the Post and Put events.
  4. Send to “SNS topic” and select your newly created SNS topic.

Great! We have our S3 bucket all set up, and a simple notification email automatically triggering when documents get uploaded.

Ok, Now the fun part, let’s write some code

Amazon PHP SDK

  1. Download the latest SDK.
  2. Drop the library into your project and include it in your PHP form handler.
    include '/path-to-sdk/aws/aws-autoloader.php';
    
    use Aws\S3\S3Client;
    
  3. Initialize the S3 client factory.
    $client = S3Client::factory(
    
    array(
        'credentials' => array(
            'key'      => '<provided by AWS>',
            'secret'   => '<provided by AWS>'
        )
    ));
    

There are more secure ways to store your credentials. I’m referencing an inline approach for readability.

  1. Slap together the body of your document to upload.
    $body = "Name: {$_POST['name']} \n" . 
    	"Email: {$_POST['email']} \n" .
    	"Comments: {$_POST['comments']} \n";
    
  2. Push it up to S3, Kapow!
    $result = $client->putObject(array(
    	'Bucket' => '<your bucket name>',
    	'Key'    => date('m-d-Y-h-i-s-A').'.txt', // whatever you want to name it
    	'Body'   => $body
    ));
    

The $result object will capture whether you successfully posted the file. It does return interesting tidbits like the newly created object’s URL:

echo $result['ObjectURL'];

And, that’s a wrap.

Connect with us

Thank You!

We really appreciate your interest in what we do.

We'll get back to you as soon as we can.