Saturday, November 18, 2017

An Example of PHP 7 For Windows 10 (IIS)

Hi! I just revisited PHP after a long break, and I learned some new tricks. The first trick was a package manager for Windows called Cholocatey. If you follow the instructions at https://chocolatey.org/install, you should have a working copy. I needed to run the Command Prompt "as administrator" to get the DOS command to work.

Then I ran the following commands:

choco install php

choco install composer

I normally use MongoDB for my database manager, so I needed the latest PHP driver. After some digging I downloaded the 64-bit non-thread-safe ZIP file from https://pecl.php.net/package/mongodb/1.3.2/windows. After expanding that file, I copied the DLL and  PDB to the C:\tools\php71\ext directory. I had to tell PHP about the extension in php.ini; that was achieved by adding the following line:

extension=php_mongodb.dll

Telling IIS about Chocolatey's PHP build was a bit trickier. I went into the IIS feature, then into Handler Mappings, and created the following module mapping...



I also wanted to use "index.php" files for home pages. That was achieved by going into Default Document in IIS, and adding that file name to the list.

To test this setup, I created a simple website that asked for a name, then displayed an updated alphabetized list of stored names. Since the HTML for such a form is trivial, I'll just give you the "action" file...

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title>Updated List</title>
    </head>
    <body>
        <?php
        
        // put your code here
        include 'vendor/autoload.php';
        use MongoDB\Client as MongoDBClient;
        
        $client = new MongoDBClient('mongodb://localhost:27017');
        $x = $client->selectDatabase('project2')->listCollections();
        $collection = $client->project2->names;
        $firstName = filter_input(INPUT_POST, 'first_name');
        $lastName = filter_input(INPUT_POST, 'last_name');
        $inputOne = $collection->insertOne(['first_name' => "$firstName",
            'last_name' => "$lastName"]);
        echo "Added ", $lastName, ", ", $firstName, ".<br>\n" ;
  //      var_dump($inputOne);
       $result = $collection->find([], ['sort' => ['last_name' => 1,
           'first_name' => 1]]);
        echo '<ul>';
        foreach ($result as $value) {
            echo "<li>", $value['last_name'], ", ", $value['first_name'], 
                    "</li>";
        }
        echo '</ul>';
        
        ?>
    </body>
</html>

The "filter_input" function is one way to access POST form variables. Accessing $_POST entries directly has been deprecated. The "insertOne" method is how to add a single document to a particular collection. The second array in the "find" function is the the new way of sorting query results. This version alphabetizes by both last and first names in ascending order. Using an empty array in the first parameter tells MongoDB to retrieve the entire collection. Lastly, "vendor/autoload.php" is a script that allows the PHP engine to access the packages you installed using Composer. I just remembered to tell you... in order to use MongoDB in a PHP 7 site, you need to use the DOS command

composer require mongodb/mongodb 

at the site's root.