Tag Archives: php

Google App Engine Upload Error – This application does not exist

If you started getting the error “This application does not exist” after you started using OAuth to authenticate and upload your application, This is the solution.

Cause – Its only saving the OAuth token for a single account. So if you like me is using multiple app engine accounts you might want to follow this instructions.

App engine launcher saves the OAuth token on your home folder and its named –

.appcfg_oauth2_tokens

You need to find and delete this file. On windows machines it will be under C:\Users\admin or C:\Users\[Username]

Advertisements

Google Recaptcha – Defeating spam got a lot easier

Earlier version of the recaptcha was just a pain in the ass. For developers to implement and also for users who had to squint and type distorted text. Remember this?

2015-04-10 09_07_07-Customizing the Look and Feel of reCAPTCHA - reCAPTCHA — Google Developers

 

Well Google changed that with this new version where the user only had to Tick a check box and some magic happened and it checked if the user is authentic. (Its not clear on their website how exactly they do it, but i think they check for data that has been captured by their analytics tools or cookies or something to make sure that your human)

2015-04-10 09_10_56-reCAPTCHA_ Easy on Humans, Hard on Bots

Here also it falls back to a captcha text if it cant determine if your human so there is a fail safe built in.

Anyway the good news for developers is we can quickly add captcha to our pages without going through a tedious process. So I’m going to give the steps down here that I used to implement recaptcha on my free invoicing tool. (Invoicer)

Step 1.

Go to https://www.google.com/recaptcha/intro/index.html and click the get Recaptcha buton.

2015-04-10 09_14_19-reCAPTCHA_ Easy on Humans, Hard on Bots

Step 2.

Enter the Label and domains that this recaptcha needs to be active on, and click register.

2015-04-10 09_15_43-reCAPTCHA_ Easy on Humans, Hard on Bots

Step 3.

On the next screen you will get the instructions on how to add the recaptcha into your page. But Ill list down the steps here as well.

On client side,

add the script file in the head tag.

 <script src='https://www.google.com/recaptcha/api.js'></script>

Add the following code at the end of the form where you want the captcha widget to appear,

 <div class="g-recaptcha" data-sitekey="6LddIgUTAAAAAEky5noUKqsnA3QPysdXQ05WFsnB"></div> 

On server side,

You need to capture the value generated by client side code and post it to google for verification. This bit can be a bit tricky.

How I did it on Google App Engine is to do a url fetch. Think you will be able to do the same by using curl. Or also ajax.

 <?php

/* recaptcha validation */
<pre>$data = ['secret' => 'YOUR OWN SECRET KEY', 'response' => $_REQUEST['g-recaptcha-response']];
$data = http_build_query($data);
$context = [
    'http' => [
        'method' => 'POST',
        'header' => "custom-header: custom-value\r\n" .
            "custom-header-two: custom-value-2\r\n",
        'content' => $data
    ]
];
$context = stream_context_create($context);
$result = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);

You will get a json response and if the success attribute it true you can proceed with processing the form values.

<pre>$success_status = json_decode($result)->success;

if($success_status) {

}</pre>

 

More information can be found here regarding verification.

The trickiest part is handling it on server side. If you have any problems with that please post on the comments section and I will try and help. 🙂

Sorting multidimensional arrays by multiple keys in PHP

This is a very cool method to sort a multidimensional array by multiple keys. I didn't think of this though. :) Found this while searching for a solution on stackoverflow link is given here.
$mylist = array(
    array('ID' => 1, 'title' => 'Boring Meeting', 'event_type' => 'meeting'),
    array('ID' => 2, 'title' => 'Find My Stapler', 'event_type' => 'meeting'),
    array('ID' => 3, 'title' => 'Mario Party', 'event_type' => 'party'),
    array('ID' => 4, 'title' => 'Duct Tape Party', 'event_type' => 'party')
);

# get a list of sort columns and their data to pass to array_multisort
$sort = array();
foreach($mylist as $k=>$v) {
    $sort['title'][$k] = $v['title'];
    $sort['event_type'][$k] = $v['event_type'];
}
# sort by event_type desc and then title asc
array_multisort($sort['event_type'], SORT_DESC, $sort['title'], SORT_ASC,$mylist);

Sending Emails With Google App Engine PHP

So here are some tips on how to send email using PHP on Google App Engine. This basically covers how I overcame the problems when I tried to implement this.

Check https://developers.google.com/appengine/docs/php/mail/ before starting to read my version.

Sending HTML – Use htmlBody in the mail options to include the message body this will preserve your html code. other wise it will just print the html code out.

require_once 'google/appengine/api/mail/Message.php';

use google\appengine\api\mail\Message; // make sure this is at top of the page

// ...

$message_body = "...";

$mail_options = (
    "sender" => "admin@example.com",
    "to" => "user@example.com",
    "subject" => "Your example.com account has been activated.",
    "textHTML" => $message_body
);

try {
    $message = new Message($mail_options);
    $message->send();
} catch (InvalidArgumentException $e) {

}

including the Message.php file – If you include the use statement partway down the page it will not work. I had a hard time figuring this out but just php common sense. Make sure you add the use statement on the top of the page before any php code. Then you will be fine.

Also make sure the from address is a Google Apps for Business Account, then things are just plain easy.

WordPress Tip – Print parent page title and if there is no parent, print current page title.

Got this requirement on one my projects to show the Parent Page Title if there is a parent page and if there is no parent page to show the current page title. To do that use the following code,

<?php
echo empty( $post->post_parent ) ? get_the_title( $post->ID ) : get_the_title( $post->post_parent );
?>

Easy method to encode password Using PHP

We are all used to using MD5() for generating hashes when saving passwords using php and mysql. But I read some where that method is not that secure. So here is an alternative.

To encode the password before saving use,

base64_encode('password');

to decode the password after retrieving use,

base64_decode('password');