Articles
Archive for the ‘php’ tag
Compare phpinfo's Across Two Servers
Work Environment
For work, I have to set up my own development environment that is able to run my employer's code on my own laptop. It's PHP/MySQL, so it's a simple LAMP setup.
sudo aptitude install apache2 php5 mysql-server libapache2-mod-php5 php5-mysql php5-curl phpmyadmin
Of course, then I have to modify some config files, pull down the MySQL DB for testing and change the document root.
But wait, what about the settings in PHP? Remember magic quotes or register globals? Well installing PHP 5.3 in Ubuntu 10.04 Lucid Lynx, these are of course turned off by default, but my work requires them. What about other settings though? I don't want to have to keep chasing bugs as I try and develop/hack on my employer's code. Luckily I had this exact same problem with another employer and came up with a solution: compare two phpinfo files.
<?php phpinfo(); ?>
This function is a special function in PHP that dumps environment and config information into a table. The problem is, when it comes to comparing two from different servers, it's long and the markup is extensive.
The Script
The code I use are recycled functions found on the Internet (referenced of course) mixed in with some cURL and loops. The script is able to fetch two phpinfo files from any web accessible URL, including ones that need an htaccess (also known as basic authentication) password, and spit out differences between the two files.
You can download the code and see an example of two readily accessible phpinfo files found on the Internet (note: don't make your phpino file readily accessible on the Internet). Just make sure to edit lines 313 and 314 to the two phpinfos you need to compare and run.
If you see a blank screen, it's probably because php5-curl isn't install. Run
sudo aptitude install php5-curl
Good luck!
Simple jQuery AJAX Tutorial
I'm a huge jQuery fan and use it any time I need to write JavaScript. It's extremely easy to use, more logical than straight JavaScript and can be separated out of HTML allowing for external scripts and easier updates.
If you've ever ran a PHP/Ruby/Python/Perl web script that takes a considerable amount of time to load (5 seconds or more), then you might want to look into AJAX. AJAX essentially allows a page to load while asynchronously loading other scripts, allowing users to navigate to another page or view content on that one page while waiting for another section to load.
It increases usability and eases user frustration dramatically.
Install jQuery
Firstly, we'll load jQuery from Google's caching server, allowing for fast-transfer speeds, lower bandwidth usage on our server and since many other sites use it, the script may already be cached in the user's browser, again saving bandwidth and transfer time.
To add jQuery to your website, add this inside your <head></head> tags:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"
charset="utf-8"></script>
Add an AJAX Request
The AJAX is extremely simple in jQuery and looks something like this:
$.ajax(
{
type: 'POST',
url: 'path/to/php/scripts/script.php',
data: 'data=this_is_some_data&another_variable='+dynamic_variable,
success: function(msg){
$('#result').html(msg)
}
});
How the AJAX Works
First you load the .ajax method, then tell it you want to use POST, then the URL to the PHP script, any data you want to send to it (you can omit this part if you don't accept any variables) and then the success function essentially dumps the return data to <div id="result"></div>. The function also uses the .html method instead of .text so that the HTML is loaded in the browser's DOM and doesn't look like garbage.
Increasing Usability
In between the <div id="result"></div> tag, you may want to add an animated GIF and the text 'Loading...' so the user knows something is about to load. When the AJAX request is complete, the 'Loading...' text and the animated GIF will be overwritten by the results of your script. Not difficult to implement, but gives the user a better indication of what is being performed.
Example
Loading...
(Nothing is actually going to load here, so don't wait for anything. However, do feel free to copy the image and use it within your own website or go to ajaxload.info to find different styles and colours!)
Security
Also, since the URL to your script is now available to the public, make sure you use a different folder path than your other PHP scripts or make sure they're programmed for no direct access.
e.g. index.php (or any directly accessed file) would have:
define('INDEX', true);
while your scripts included from this file would start with:
<?php defined('INDEX') OR die('No direct access allowed.');
That way, if it's a security concern, no one can execute your script directly.
Web Development in Ubuntu, 4-part Series
Between December 2008 and March 2009, I wrote a 4-part series on web development in Ubuntu for the online magazine Full Circle Magazine. All articles have been translated into Chinese, French, Hungarian, Italian, Russian and Turkish.
- Part 1 - Software for web development in Ubuntu (issue 20)
- Part 2 - Basic HTML and CSS (issue 21)
- Part 3 - Install a LAMP (web) server (issue 22)
- Part 4 - Basic PHP (issue 23)
I will be re-posting this series in HTML and extending the series soon.

