Google Mobile-friendly test in PHP

Lots of folks out there about to make your website Mobile-friendly. It’s obvious that there will be significant impact on your website if your site is not mobile-friendly or what we regularly call ‘responsive’. Mobile friendly sites will get higher rank in search results in Google. And Google already started to rolling out this algorithms.

After searching on internet, I didn’t get any quick solution to check mobile-friendliness website in PHP. Although Google published it’s official Mobile-friendliness Test Tools.

When I tried to find some solutions then I figured out that Google has it’s public Pagespeed Test API v2. But in this version Google didn’t make it clear to see Pass or fail result for mobile-friendliness test. It only shows UX score. But in their v3beta1 API version they have a clear indicator about mobile-friendliness. And I figured it out and made a simple PHP functions to check the mobile-friendliness test of any website. Here is my scripts:

In the above script you just need to put your website URL to check and Google PageSpeed API Key what you will get from your Google API Console or see the Google PageSpeed Developer Getting Starter page.

Now, run your script and you will get the following array.

array (size=8)
  'kind' => string 'pagespeedonline#result' (length=22)
  'id' => string 'http://facebook.com/' (length=29)
  'responseCode' => int 200
  'ruleGroups' => 
    array (size=1)
      'USABILITY' => 
        array (size=2)
          'score' => int 100
          'pass' => boolean true
  'pageStats' => 
    array (size=2)
      'numberRobotedResources' => int 0
      'numberTransientFetchFailureResources' => int 0
  'formattedResults' => etc.........

From the above array $result['ruleGroups']['USABILITY']['pass'] is the actual result of your website’s mobile-friendliness status. true means your website is mobile friendly and false means your failed to make your website mobile friendly. Simple.

Have fun! Write comments if you need any more assistance.

Published on 09 May 2015 » »

Twig and AngularJS curly braces conflict solution

Whoever loves to work with Twig template system for their PHP driven web application may want to implement AngularJS within Twig template. But the main problem will be Twig’s curly braces. AngularJS and Twig both use __ inside their template to output data. So simply, it can cause trouble to work with this two lovely systems together in your application.

But here is the solution. According to AngularJS $interpolateProvider docs I figured out the easy solution. You can change AngularJS __ symbol with any other symbol. Cool! So see how you will override that configuration.

var app = angular.module('myApp', []);

app.config(function($interpolateProvider){
  $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
});

Now you can use {[{ }]} symbol for your AngularJS. And Twig use **. So problem solved. From now on you can use AngularJS within your Twig template system.

Published on 14 Apr 2015 » »

AngularJS Resource use for RESTful apps

This example will help you to learn how to interact between your backened RESTful apps and AngularJS. AngularJS has a service factory called ngResource which you can use to do CRUD operation with your RESTful backend application.

Here you just need to define the service factory which will get all notes by calling an ajax request to your http://yourRESTAPIBackendserver.url/notes. Also you can do rest of the CRUD services with that script. This script represents very basic usage of ngResource. So you learn it and try to DYI.

Don’t forget to add the main angularjs library and ng resource service library in your html head tag.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://code.angularjs.org/1.2.0-rc.2/angular-resource.js"></script>

Published on 14 Apr 2015 »

Post Form Data via JQuery-Ajax Post

Case Scenario Want to prevent default HTML form submission behavior and want to post form data via jquery & ajax to any URL.

Solution Very simple solution. just with e.preventDefault() prevent default form submission and write down the following jquery code and enjoy!

Thanks. Post a comment if you face any problem.

Published on 31 Mar 2015 »

MikroTik NAT Port Forwarding - Access local pc from internet

Let me first tell you the real case scenario for this solution.

Case 01

  • You have a local network that is designed and attached with a MikroTik router that is easily managable. You have multiple computer in your local network that is running different types of applications or services. Your MikroTik router is also connected with a public ip provided by your ISP. Now you want that one of your local computer will be accessible via internet through that public ip. So in this case you can build web server, you can run mail server, you can run ftp server, other services, etc.. etc.

Expectation

  • After solving this with this article anybody from internet (or whoever you give access) can access your local pc (192.168.10.23 or any local computer) from internet.

Solutions

  • First let me describe you the visual design of network design in your home or office. See Figure: A-1.

MikroTik NAT Port Forwarding example - Access Local PC from Internet

Your Public IP is 118.168.52.32 which was provided by your ISP and attached with your MikroTik router. And your local pc’s are 192.168.10.20, 192.168.10.21, 192.168.10.22, 192.168.10.23.

Now in your 192.168.10.23 pc is running your web server and you want to make it accessible from outside your network that means from internet. Now open your winbox and connect to your router.

  • Go to IP -> Firewall-> Select NAT Tab

MikroTik NAT Port Forwarding example - Winbox configuration 1

From General tab Select dstnat chain. From Protocol select tcp. From Dst. Port right down the port with which port the remote request will be connect. Here for web server I did 8080.

MikroTik NAT Port Forwarding example - Winbox configuration 1

Now go to Action tab and select Action to dst-nat, In ToAddresses select 192.168.10.23 and To Ports will be 8080. Now save that settings. When you want to access 192.168.10.23 web server from internet people can do that by hitting http://118.168.52.32:8080 and that will be connected back and forth with 192.168.10.23.

Published on 24 Feb 2015 »

Enable Debug mode in Twig Template

Sometimes you need to see your variables or object in Twig template before printing it out. So to enable debug mode in twig template system you just need to tweak few settings upon initializing twig environment. Just follow the below code where I initiated Twig environment with debug mode.

<?php
$twig = new Twig_Environment($loader, array(
    'debug' => true,
    // ...
));
$twig->addExtension(new Twig_Extension_Debug());

And inside your template where you want to dump the variable just write ``. You can read more about dump function twig template from here

Published on 17 Feb 2015 » »

Some Important MikroTik Tips and Tricks

Port Forwarding to local IP/PORT

Type the following value into a Terminal window to enter this port forwarding rule.

bash /ip firewall nat add action=dst-nat chain=dstnat disabled=no dst-port=80 in-interface=ether1-gateway protocol=tcp to-addresses=192.168.1.20 to-ports=80

In the above MikroTik NAT forwarding rule add through MikroTik RouterOS terminal what I am doing with the above command? We are just forwarding any kinds of request to our main IP in 80 and forwarding that connection to 192.168.1.20 ip which is in my local network and port 80 will be used from 192.168.1.20 for that request.

Published on 10 Feb 2015 »

Create INI file in PHP

Write configuration data in a .ini file in PHP. It’s important for large application to store global configurations about the whole/partial system for applications.

Published on 25 Sep 2014 »

Enable FTP Passive mode in PureFTP in your cPanel server

To enable the passive port range for Pure-FTPd, perform the following via the command line as the root user:

  • Open the /etc/pure-ftpd.conf configuration file in your preferred text editor.
  • Remove the comment (#) from the beginning of the line which contains the PassivePortRange option.
  • Change that line to the following: PassivePortRange 49152 65534

  • Save the changes to the configuration file.
  • Run the sudo /usr/local/cpanel/scripts/restartsrv_ftpserver command to restart the server.

These configuration changes may be overwritten by system updates. To make these changes permanent, perform the following additional steps via the command line as the root user:

  • Run the sudo rm -f /var/cpanel/conf/pureftpd/main.cache command to clear the cache.

  • Run the sudo echo "PassivePortRange: 49152 65534" >> /var/cpanel/conf/pureftpd/main command to append the passive ports to the configuration file.

Now if the port number is not opened in your iptables then do the following to enable this passive mode port range enable in your iptables.

sudo iptables -I INPUT -p tcp --dport 49152:65534 -j ACCEPT
sudo service iptables save

This articles also recommended for you to read.

Published on 23 Sep 2014 » »

Keep navigation link active with jQuery

After clicking on any navigation link in navgation and whenever that page will be open then one active class will be added in that element.

$(document).ready(function(){
 var a = $('.navbar-nav a');
 a.each(function(){
   if(window.location.href == $(this).attr('href')){
     $(this).closest('li').addClass('active');
   }
 })
})

Published on 18 Sep 2014 »

Pages »