Category Archives: php

PHP monitor server log

PHP monitor server log
Occasionally you'll be working on a webserver and not have SSH access to it. Below is a simple script I've written that reads a server log (in this case the error log) and outputs a portion of it to a webpage.
PLAIN TEXT
PHP:

$no_lines = 25;

 

$filename = $_SERVER['DOCUMENT_ROOT']."/../logs/error.log";

$handle = fopen($filename, "r");

$contents = fread($handle, filesize($filename));

fclose($handle);

 

$contents [...]

PHP: File Downloads HTTP Headers

In PHP you can provide file downloads by using a PHP page to “pass through” the file. Of course you could just link to the actual file, however this technique means your file can kept outside of your website root. Another advantage is you can integrate this system into a PHP based user/password system easily.
Now [...]

PHP: Get First Index in Associative Array

The other day I was programming in PHP and needed to get the first index in an associative array. There doesn’t appear to be a built in function to deal with this so I wrote my own.
PLAIN TEXT
PHP:

function getArrayFirstIndex($arr)

{

foreach ($arr as $key => $value)

return $key;

}

Here's a bit of code you can test it on:
PLAIN [...]

PHP: Swap Variables One Liner

Here's a nifty one liner in PHP which let's you swap 2 variables values.
PLAIN TEXT
PHP:

list($a,$b) = array($b,$a);

Much like the PHP XOR swap there's no real reason this is any better than using a temporary variable, but you might be able to impress chicks with it or something.
/* Pete Graham */

Drupal: Hide Navigation on Forum

If you want to hide certain Navigational Blocks on your Drupal sites forum(s) here’s how to do it:
1. Go to "Administer » Blocks"
2. Click "configure" on the block you want to hide.
3. On Page specific Visibility Settings click the option for "Show if the following PHP code returns TRUE"
4. Enter the following code in the [...]

PHP: XOR swap

The XOR swap lets you swap the value of integer variables without using a temporary variable, it's also slightly faster.
PLAIN TEXT
PHP:

$x ^= $y;

$y ^= $x;

$x ^= $y;

I can't actually think of a good reason Why you'd want to use this in PHP since memory isn't normally a big issue, however it might be good [...]

PHP: Remove Values from Array

If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:
PLAIN TEXT
PHP:

// our initial array

$arr = Array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red");

print_r($arr);

 

// remove the elements who's values are yellow or red

$arr = array_diff($arr, array("yellow", "red"));

print_r($arr);

 

// optionally you could reindex [...]

Email Interaction with Web Applications (using PHP, Postfix and Regular Expressions)

One of the popular facilities on phuser.com is the ability to create polls. Once a poll has been created it can be sent to people by SMS or Email. The recipients can reply with their choice of answer and a optional comment using SMS, Email or phuser web interface, lovely!
Handling of emails replies has easy [...]