PHP Exercise 1: Fetch URL and Parse Content

July 15, 2015 at 1:47 pm

Introduction

In an effort to help others learn some really nifty PHP tricks, I’m going to start this series of PHP quizzes. I’ll explain a few concepts and then pose a problem that can be solved using those concepts. Don’t expect rocket science here, but definitely use it as a resource to learn a lot about PHP in a short space of time. Especially if you are a newcomer, or if you just want to see how much PHP you know.

Concepts To Know

Function: file_get_contents

file_get_contents offers a very simple way to download a file or image or any URL. You can use it like this:

  $data = file_get_contents("http://www.ardi.co.za");

Function: explode

Explode takes a string and breaks it up into pieces and places those pieces in an array. So if you have this string:

  MY-LONG-STRING

And you use explode like this:

  explode("-", "MY-LONG-STRING");

You will end with an array like this:

  array (
    0 => 'MY',
    1 => 'LONG',
    2 => 'STRING',
  )

Quiz 1

IP2C.org offers a free IP to location service. Try it by opening these urls in your browser:

  • http://ip2c.org/127.0.0.1
  • http://ip2c.org/198.57.216.24

Your task is to:

  1. Create a PHP file that accepts an IP address as input.
  2. Use “file_get_contents” to fetch the data from ip2c.org for the ip that is specified as input
  3. Use “explode” to split the result you get from ip2c.
  4. Based on the result, either outpout
    a) the country the ip belongs to, or
    b) an appropriate error

Testing WordPress Youtube Link Preview

June 15, 2015 at 12:45 pm

How much SHOULD you be paying for web hosting and web development?

June 27, 2014 at 2:27 pm

Web Development can become a tricky thing to budget for when you don’t really know what your options are or how much those options should cost. This really great tool which is 100% FREE will ask you a couple of questions and give you some really valuable insight into how much you can expect to pay for various services.

Try it out for FREE here.

Here’s some of the advice I got regarding hosting…

Free Quote Estimation

Get your own Drupal Project Funded!

June 26, 2014 at 5:31 pm

DrupalFund

Is there a module or a feature you’ve been dying to get off the ground, but just haven’t been able to get the funding to build it? Then DrupalFund.us is just the thing for you.

I’ve been using Drupal for about 6 years and to date I’ve never found a place where once can easily submit a project and get funding for that project. I submitted my first project to DrupalFund 7 days ago and today it went into the funding stage. The module I propose is something I know people would definitely use, but how if people are willing to throw money at it… that’s the million dollar question.

You can view my project here.

 

My Skype Broke and This is How I Fixed It

June 25, 2014 at 7:33 am

Skype was working great for a while, until I recently opened it and saw this:

What my broken skype looks like

 

This is what I did to fix it:

  1. Completely uninstalled Skype, including “Click to Call”
  2. Delete the entire folder in C:/Users/~username~/AppData/Roaming/Skype (Warning: you will lose all previous chat data)
  3. Delete the entire folder in C:/ProgramData/Skype
  4. Click start and type in: %temp%\skype
  5. Now delete the delete the dbTemp folder
  6. I then downloaded an older version of skype here (i picked Skype 5.9.0.115.exe)
  7. I installed the app and all was well with the world again

How to debug SSL?

June 16, 2014 at 11:57 am

Until quite recently, this blog post would have consisted of me explaining how you use Firebug to inspect each request to see if any URLs are being called without “https”. Luckily that’s no longer necessary, thanks to this very cool tool:

whyNoPadlock

i.e. This site: http://www.whynopadlock.com/

It allows you to enter the URL giving you problems, and breaks down exactly why the SSL isn’t doing what it should. Here’s an example:

notSecure

How to deal with SSL in Laravel?

June 16, 2014 at 6:31 am

SSLAs you probably already know, Laravel has a lot of flexibility around “routes” and even more so if you combine it with “filters“. The easiest way to deal with SSL in Laravel is using filters. As apposed to struggling with .htaccess or doing it some other way in code. I created the following two filters and this solved the problem for me:

    Route::filter('force.ssl', function()
    {
        if( ! Request::secure())
        {
            return
                Redirect::secure(
                    Request::path().(empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING'])
                );
        }
    });

    Route::filter('no.ssl', function()
    {
        if( Request::secure())
        {
            return Redirect::to(
                Request::path().(empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING']), 302, array(), false
        );
    }
});

You can then use it like this:

Route::group(array('before' => 'no.ssl'), function()
{
    Route::resource('/', 'SiteController');
});

I’m sure the “QUERY_STRING” part could probably be replaced with a more Laravel-y way of doing this, but this worked for me.

Help! Avast is blocking my website!

June 13, 2014 at 1:30 pm

avastIf you ever needed a reason to do more thorough code reviews, here’s a good one. I made the mistake of leaving the following link in the code of one of my projects:

http://localhost.me/someJavascript.js

Yes, spare me the lecture. I know this is bad and could easily have been avoided. Nevertheless, I am writing about this, because what was annoying about this was how I managed to find that it was causing issues: a friend of mine actually told me they can’t access the site because Avast is blocking them. Long story short, Avast sees this kind of code as a malware attack. Rightly so too, because a malicious website could do malicious things using a tactic like this.

My problem, however, came in after fixing the link. Even after restarting my machine, and telling Avast that the URLs are safe, it still decided to block the site. First prize would’ve been for it to just forget that it came across the site and rescan it as a new potential threat. This way I would be able to tell if something was still wrong. Unfortunately, Avast doesn’t work like that. In order to fix the problem, you get to do this:

  1. Double click on the Avast icon in the bottom right
  2. Click on “Settings”
  3. Click on “Active Protection”
  4. Disable “Web Shield”

This solved the problem for me, and me only. I still need to figure out how much of a problem this is for other users. There are bound to be other Avast users that are still blocked from accessing the site. I started by reporting the issue as a false positive here.

And now I play the waiting game…

UPDATE

Its a day later and I never heard back from Avast but it seems Avast is no longer blocking my site. So either they saw my complaint, or it expires after a  certain amount of time.

How to open a port on Centos 6 that has WHM/cPanel installed

May 28, 2014 at 6:14 pm

Firstly, forget ipTables. There’s an easier way. Install “APF”:

  1. Login as Root
  2. Go to your home folder: cd ~
  3. Make a temporary download folder: mkdir downloads
  4. Go into that folder: cd downloads
  5. Download APF: wget http://www.rfxnetworks.com/downloads/apf-current.tar.gz
  6. Go into the extract folder: cd apf-9.7-2/
  7. Run the install file: ./install.sh

You should see something similar to this:

[root@root]# ./install.sh
Installing APF 9.7-2: Completed.

Installation Details:
Install path: /etc/apf/
Config path: /etc/apf/conf.apf
Executable path: /usr/local/sbin/apf

Other Details:
Listening TCP ports: 21,22,25,53,80,110,111,143,443,465,587,631,993,995,2077,2078,2082,2083,2086,2087,2095,2096,3306,43593
Listening UDP ports: 53,111,631,745,764,58037
Note: These ports are not auto-configured; they are simply presented for information purposes. You must manually configure all port options.

APF is now installed, and you can head on over to /etc/apf/conf.apf:

  1. vi /etc/apf/conf.apf
  2. Navigate to the line that reads: # Common inbound (ingress) TCP ports
  3. Below that line, you want to add something like this: IG_TCP_CPORTS=”20,21,22,25,53,80,110,143,443,465,993,995,2082,2083,2086,2087,2095,2096,3306,10000,35000_35999″
  4. Save the file and quit VIM
  5. Stop APF: service apf stop
  6. Start APF again: service apf start

You should be good to go! But, if you want to make sure APF starts on startup, do this:

  1. chkconfig –add apf
  2. chkconfig –level 345 apf on
  3. Restart Centos to make sure it works after the restart: shutdown -r now

Now you should be ready to rock!

How I Got Back Into my Centos Server After Messing up my IPTables and SSH Port

May 24, 2014 at 5:12 am

lockWhat I did seems to be a fairly common mistake, but I still feel pretty stupid. I changed my SSH port from the standard 22 to 1000. This would’ve been fine, if I didn’t forget to run this afterwards:

iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 1000 -j ACCEPT

The net effect was that when I tried to log back in, the port was not open. Luckily, there are two ways that you can get out in a scenario like this.

The first method didn’t work for me, because I couldn’t FTP into the server as root. If I could, I would have put a file in /etc/cron.d with these contents:

* * * * * root /sbin/service iptables stop

Unfortunately, I couldn’t use this method. Luckily I had cpanel and whm installed, and this offers a second way to get out of a locked server if the problem is iptable / port related. You can simply:

  1. Go to your WHM url (usually, www.yoursite.com/whm)
  2. Log in
  3. Modify the url by appending: /scripts2/doautofixer?autofix=iptablesflush (i.e. something like this: www.yoursite.com/scripts2/doautofixer?autofix=iptablesflush)

You should see a message saying the iptables were flushed:

autofixer