Archive for September, 2011

PHP command line accept inputs – $argv

Wednesday, September 28th, 2011


~a sudo php scriptname.php 100

pass IN 100 to the script.

In the script, receive input:

$batchNumber = $argv[1]

test it:


handy when calling a php script via a cron, thus allowing the script parameters to be configurable via the cron.

PHP $_Server Host Name etc. command line equivalent

Wednesday, September 28th, 2011

string php_uname ([ string $mode = “a” ] )
mode is a single character that defines what information is returned:

  • ‘a’: This is the default. Contains all modes in the sequence “s n r v m”.
  • ‘s’: Operating system name. eg. FreeBSD.
  • ‘n’: Host name. eg.
  • ‘r’: Release name. eg. 5.1.2-RELEASE.
  • ‘v’: Version information. Varies a lot between operating systems.
  • ‘m’: Machine type. eg. i386.

Secure HTML form example using PHP htmlentities, passing a token PHP, AJAX submission via jQuery

Wednesday, September 21st, 2011

<? php
// form page (index.php): initialize a session so that we can set a unique, random, encrypted value to a user session
$token = md5(uniqid(rand(), true));
$_SESSION[‘token’] = $token;
? >
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>
<html xmlns=””>
<script src=”js/jquery.js” type=”text/javascript”></script>
<script src=”js/jquery.validation.js” type=”text/javascript”></script>
<script src=”js/registerSubmit.js” type=”text/javascript”></script>
<!– The Form –>
<form id=”topForm” name=”topForm” method=”post”>
<input type=”hidden” name=”token” id=”token” value=”<? php echo $token; ? >” />
<input name=”emailAddress” id=”emailAddress” value=”Email address” onclick=”if( this.value == ‘Email address’ ){ $(this).val(”); }” onblur=”if( this.value == ”){ $(this).val(‘Email address’); }” />

// The jquery.validation.js class uses a regular expression to stop the process if the email entered isn’t acceptable

;(function($) {
$.validation = {};
$.extend( $.validation, {
email:function(email) {
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if ( filter.test(email) ) {
return true;
return false;

// The registerSubmit.js submits the entered email via AJAX, returning a success or failure message, to our contactSubmit.php script, which handles the submission of the new email to a database

$(document).ready(function() {


$(“#topForm”).submit(function(event) {
var $form = $( this ),
emailAddress = $form.find( ‘input[name=”emailAddress”]’ ).val(),
token = $form.find( ‘input[name=”token”]’ ).val(),
formaction = $form.attr( ‘action’ );

$( “#reg-description” ).html (“submitting…”);
type: “POST”,
url: “contactSubmit.php”,
data: “firstName=” + firstName + “&lastName=” + lastName + “&emailAddress=” + emailAddress + “&token=” + token,
success: function(){
$( “#reg-description” ).html( “Thank you. You have been registered.” );
$( “#emailAddress” ).val (“Please enter a valid email address”);

// contactSubmit.php:  receive the email, check the token, clean the data, pass to a database layer API

<? php


if (isset($_SESSION[‘token’]) && $_POST[‘token’] == $_SESSION[‘token’]){
$emailAddress = htmlentities($_POST[’emailAddress’]);
} else { $emailAddress = ”; }

$contact = new EmailContact($emailAddress)

try {
} catch (Exception $e) {
; // do something
//$retrieve = new EmailContact($emailAddress);
? >

PHP retrieving user input when executing a script from the command line

Friday, September 16th, 2011

So either execute the script via the browser, http://someserver/somescript.php
or via the command line >php somescript.php
the example below shows how to support either eventuality

//first we check to see whether the script is being run via the command line, or via a browser

if(defined(‘STDIN‘) ){
echo “Running from CLI” . PHP_EOL;
echo “How many items would you like to batch?”;
$handle = fopen (“php://stdin”,”r”);
$line = fgets($handle);
$batchNumber = trim($line);
// and so on…
echo “Running from the Browser<br />”;
// and so on…