php Quick Tips

$_SERVER[“REMOTE_ADDR”] not working with Cloudflare configuration

so You might want to get the user ip in a PHP website, and you have searched and found $_SERVER["REMOTE_ADDR"]is the answer.

But despite of everything you do, you get to a road block of getting the same IP address.

The problem is, cloudflare basically routes the traffic to your server, hence the same ip address every time, but the solution is, cloudflare also sends a custom header with the request HTTP_CF_CONNECTING_IP

so instead of doing $_SERVER[“REMOTE_ADDR”] you only have to do $_SERVER["HTTP_CF_CONNECTING_IP"]


Map Data structure

A hash map or key val. C# equivalent is Dictionary<T1,T2>


Install Free SSL on AWS EC2 (Windows AMI)

That’s how I did it, but the process may be different for you. I had a scanerio where our main domain ( was

I have a hostgator hosting, and a domain from PKNIC, I add A record for with the Elastic IP of AWS EC2.

Created the Let’s Encrypt certificate from hostgator panel. Downloaded it in .pem file. and used this SSL Converter to convert .pem into .pfx

Then in IIS, imported the certificate from Server Certificate Menu of IIS.

After that, go to my website, edit the binding and select the certificate I imported for HTTPS protocol. and that’s it.

P.S Hook me up with any questions you have, as I didn’t have time to write a long and well explanatory post.

Software Engineering

Loose Coupling, Why bother?

For me, it all revolves around this.

Build to last, stronger foundations = powerful castle !

But how? everyone is under a crunching time machine, no one cares how you do it, they all want what is done in the quickest way possible.

Sure, it will save you few bucks at first, but as a wise man said to me. A software is a never ending process, even if it is a simple calculator application. You’ll always get to build and build and build and so on…

Okay, all that, but is loose coupling really matters?

For this, let get into another paradigm. How to measure a software’s code quality?

It’s now how fast you coded it in the first place, but the efficiency of implementing client feedback.

That’s how loose coupling came into all this.

All classes have single responsibility and adding some feedback points of a client in some class won’t disturb other methods. And for some reason you decided to port the class to multiple platforms, you won’t have to reinvent the logic, (yes there are other factors too). But generally speaking, a rule of thumb is.

Loose Coupling = Interfaces = Contracts

So you have defined a contract (interface) which is a set of guidelines, and every class implementing the interface has to obey.

This keeps code maintainability, across multiple plateforms, and assemblies, and for some reason you have revise the complete logic of a class, you just don’t have think the logic what, how , when , where.

Just inherit the interface to another class, the methods are there, implement their logic! easy as a pie… 3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 06286 20899 86280 34825 34211 7067


The Quest for Repository pattern

So it all started with the hype of design patterns and a new project I was going to work on. Truth be told I’ve never worked on repository before and didn’t found a perfect implementation for it as well.

There are many concepts around internet, like you should make generic repositories and the creation of services.

But be aware my friend, generic repositories are never the answer to any project, as I have learned and read in some book maybe in Programatic Programmer or Code Complete, a project never ends!

Maybe you will not the person who will work on after project completes, but there always will be someone else .

So for the sake of code maintainability and unforeseen scenarios, you should never go for Generic Repositories.

First they will be fun and very less code, so much inheritance and polymorphism that you begin to think this is it. Now you don’t have to do the hard work, everything is managed now. Thanks to the people who invented it, But no, it will be out of hand very soon.

So Generic Repositories is BIG NOOOO.


HTML Template with JQuery

Yes, you heard right, there are tons of new JS frameworks which are more powerful with this stuff like react native, angular, backbone, knockout, ember and the list goes on.

But our old favorite JQuery and it’s tons of plugins still has almost like 80% of the Internet market share.

Often you need to perform ajax calls and load dynamic data into your html from Ajax calls. If you are like the most people, you’ve probably done with declaring html markup in javascript variables and then append it. like

    <ul id="result">
var li = '<li>'this is my data'+data.value</li>';

Yes its very easy and manageable if you just have to print these li but what if you need to display address, names, telephone numbers of users in a bootstrap cards layout. You will write the whole card layout in your JS, e.g.

var htmlTemplate = '
'<div class="card">'+
  '<div class="card-header">'+
  '<div class="card-body">'+
   ' <div class="telephone">'+
    '<div class="address">'+

This code is beginning to smell now. And soon as the requirements raises, code will become unmanageable, unreadable, and prone to errors and bugs. What if I tell you there is a cleaner and more manageable way to do that.

So here are the steps to do that:

  • Create the html template at bottom of your page like this:
<div class="card">
  <div class="card-header">
  <div class="card-body">
    <div class="telephone">
    <div class="address">

Now set this template block’s visiblity to none.

  • Now add this html based template visibility to none
  • Get that whole template element with jquery
  • Clone the element with jquery’s clone()
  • replace the variables in {} with your data
  • append the final element to the content area of your html.


Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!