Skip to content

Instantly share code, notes, and snippets.

@stefany-newman
Created June 26, 2019 15:54
Show Gist options
  • Save stefany-newman/ad2e595f25b716a03da31ac6a420b6c8 to your computer and use it in GitHub Desktop.
Save stefany-newman/ad2e595f25b716a03da31ac6a420b6c8 to your computer and use it in GitHub Desktop.
Process subscription form submissions in AMP
<?php
/*
Function siteURL() courtesy of Chris McKee - https://gist.github.com/ChrisMcKee/1284052
Get sute URL with protocol (http or https)
*/
function siteURL()
{
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'
|| $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domainName = $_SERVER['HTTP_HOST'];
return $protocol.$domainName;
}
// -> Example.com
function domainName()
{
return ucfirst($_SERVER['SERVER_NAME']);
}
// cleaning the string
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// End of functions
$domain_name = domainName(); // Example.com
$siteURL = siteURL(); //https://example.comP
$email = $_POST['subscriber_email']; // required
/*
AMP boilerplate CORS code and setting the content type as JSON
Since all errors / sucess messages will be encoded in JSON
*/
header("Access-Control-Allow-Origin: ".
str_replace('.', '-',''.$siteURL.'') .".cdn.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin:$siteURL");
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
header("Access-Control-Allow-Credentials: true");
header("Content-Type: application/json");
// List of our subscribers' emails
$our_subscribers = ['[email protected]', '[email protected]'];
/*
Check whether Email is filled in.
Add error to the error array. And send a JSON string to
display the errors on the front end.
*/
if(!isset($_POST['subscriber_email'])
|| empty($_POST['subscriber_email']) ) {
$error_array = ['verifyErrors'
=>[['name' => 'missing email','message'
=> 'Please enter your email!']]];
// error to be displayed in amp - mustache
header("Content-Type: application/json");
echo json_encode($error_array);
header("HTTP/1.1 400 Bad Request");
return false;
}
/*
Checking of the email is valid, i.e. if it contains
an '@' and at least one dot (i.e. [email protected])
*/
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error_array = ['verifyErrors'
=>[['name' => 'Invalid email','message'
=> 'Enter a valid email!']]];
// error to be displayed in amp - mustache
header("Content-Type: application/json");
echo json_encode($error_array);
header("HTTP/1.1 400 Bad Request");
return false;
}
/*
Checking whether the user hasn't already
subscribed with this email
*/
if(in_array( $email, $our_subscribers) ) {
$error_array = ['verifyErrors'=>[['name' =>
'Email exists','message'
=> 'Email already exists in our database.']]];
// error to be displayed in amp - mustache
header("Content-Type: application/json");
echo json_encode($error_array);
header("HTTP/1.1 400 Bad Request");
return false;
}
// If there are no errors, submit the form.
// Below we are pretending we are adding
// the submitted email in the DB
if(1 == 1)
{
$data = ['submit-sucess'=>'Success!'];
echo json_encode($data);
header("HTTP/1.1 200 OK");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment