Category: PHP

  • johnro.net Now on AWS!

    My WordPress site is now on AWS! I spent a good chunk of my time debugging the All-in-One WP Migration plugin to get my migration working, and I finally realized I skipped an important step in their migration documentation:

    Migrate WordPress from PHP 5 to PHP 7

    1. Make sure that all plugins are deactivated. This will ensure your website error is not caused by any third-party software

    If you are getting stuck in the “Done Preparing Blogs” stage, then it could be because you didn’t deactivate all the plugins.

    Cheers!

  • Homebrew PHP Upgrade: “configure: error: Cannot find libz”

    I was routinely upgrading my packages on Homebrew when I ran into the following error:

    $ brew upgrade
    ==> Upgrading 1 outdated package, with result:
    homebrew/php/php70 7.0.4
    ==> Upgrading homebrew/php/php70
    ==> Downloading https://php.net/get/php-7.0.4.tar.bz2/from/this/mirror
    Already downloaded: /Library/Caches/Homebrew/php70-7.0.4
    ==> ./configure --prefix=/usr/local/Cellar/php70/7.0.4 --localstatedir=/usr/local/var --sysconfdir=/usr/local/etc/php/7.0 --with-config-file-path=/usr/local/etc/php/7.0 --wi
    Last 15 lines from /Users/johnro/Library/Logs/Homebrew/php70/01.configure:
    checking for OpenSSL support... yes
    checking for Kerberos support... /usr
    checking whether to use system default cipher list instead of hardcoded value... no
    checking for krb5-config... /usr/bin/krb5-config
    checking for RAND_egd... no
    checking for pkg-config... no
    checking for OpenSSL version... >= 0.9.8
    checking for CRYPTO_free in -lcrypto... yes
    checking for SSL_CTX_set_ssl_version in -lssl... yes
    checking for PCRE library to use... bundled
    checking whether to enable the SQLite3 extension... yes
    checking bundled sqlite3 library... yes
    checking for ZLIB support... yes
    checking if the location of ZLIB install directory is defined... no
    configure: error: Cannot find libz
    

    Of course, I did the first thing I always do when running into an error, Google it! And I found this link that suggested running the following:

    $ xcode-select --install

    After that,

    $ brew upgrade

    ran fine.

  • PHP: Connect to SQL Server Database on Linux (and Mac OS X)

    Microsoft SQL Server
    Microsoft SQL Server

    I have used PHP to connect to SQL Server plenty of time, and every time I would use the SQLSRV extension. The problem with that is, PHP’s SQLSRV extension is only available for Windows servers. Which wasn’t a problem for me because I would normally develop PHP on Windows machines. It wasn’t until recently I needed SQL Server access on a Linux machine.

    It wasn’t as straightforward as I had hoped, and it certainly did not help to learn that the MSSQL PHP extension was removed in version 7.0, which took me a few hours to discover. I am writing this post in hopes that I save some people a few hours of pain, including my future self.
    (more…)

  • PHP: How to array_merge Without Losing Numeric Keys

    PHP function array_merge is nice in that you can easily merge one (or more) arrays into a single array with a single call of the function. The keys are retained, but only if they’re not numeric keys(!).

    E.g.

     "hi");
    $array2 = array(3 => "data");
    $result = array_merge($array1, $array2);
    ?>
    
    Result:
    Array
    (
        [0] => hi
        [1] => data
    )

    Perhaps this is the behavior you want. But what if you want to retain the original numeric keys?

    (more…)

  • PHP: sqlsrv problems with UTF-8 values? Set your CharacterSet option!

    Are you banging your head trying to figure out how to convert a seemingly invalid UTF-8 value into a valid one? You might be surprised to know that the problem isn’t how PHP interprets the characters, but rather a database connection issue. If the UTF-8 value in question looks valid in the database but does not look right when displayed on a web page, that could mean your database connection settings are not set correctly.
    (more…)

  • PHP: Importing an SQL file via PHP script

    Trying to import an SQL file using PHP? Most people will tell you that using PHP function shell_exec to run the MySQL client is your best bet, but what if you simply can’t do it that way? The solution is quite simple, actually, and doesn’t require programming skills beyond the basic file parsing and query executing.
    (more…)

  • Sorting your photo collection by date the PHP way

    Ever wanted to sort your photo collection in the order by date taken? Can’t find the right program that will do it for you? I decided that I had a large enough collection myself to warrant organizing, but I could not find a program that can organize it the way I want it. I have always wanted the photos to be sorted by the date taken and grouped by the year, so I decided to create a short script for myself.
    (more…)

  • Data scraping: how to prevent

    Data scraping is common these days with so many data driven web applications out there. Regardless of the legality and ethics of the subject, it just sucks to know that there may be people out there who might hammer at your site to “take” all your hard work for their selfish use. That being said, it’s almost impossible to prevent people from taking your data, especially if your data is easily accessible by the public.

    You’re probably here because you’re suspicious of possible data scraping activities happening in your website. While there are preventive measures you can take to keep the scrapers out, there are always holes for the smart ones to get through and eventually take the data they want. What we want to do is make it very difficult for the average scraper to hack away the data.
    (more…)

  • PHP Warning: json_encode() [function.json-encode]: Invalid UTF-8 sequence in argument in god_i_hate_this.php

    Ever get the following error?

    • PHP Warning: json_encode() [function.json-encode]: Invalid UTF-8 sequence in argument in…

    Then you are in the same boat I was in, and unfortunately for me, the solution wasn’t as straightforward as I thought it was. The problem we are seeing is that the value being encoded to json is not UTF-8 encoded data. PHP function json_encode, unfortunately, will only accept UTF-8 as its parameter. (more…)

  • Convert XML to array and vice versa in PHP

    As a developer, it is common to search the web for an existing (open source) code snippet to re-use for an application, but it is rare for the developer to just copy code online and use it straight in their application without modifying it first.

    I am one of many developers who like to save time if possible by using open source code if coding from scratch takes longer than a couple of minutes, but there are times when one cannot be found. I ran into such situation when I wanted a function that converts an XML string into an array. Unfortunately, most are overly complicated for what I wanted, so I decided to create my own and share with the world. The following function converts XML strings into a PHP array while retaining the level depths of the XML but all attributes are ignored. (more…)