Tail -f apache access_log

SSH screenshot showing tail -f stream in action

Here is how to use SSH/Telnet do a search and locate your access_log file. The linux locate command will help:

locate access_log

You might see lots of logs separated by domain names. A common location for these logs are /var/log/apache, so cd your way there:

cd /var/log/apache
/var/log/apache$ tail -f access_log

Now a stream using tail of the file will occur in real time and you can watch and see who is accessing what in near real time.

Reference: Parsing Apache acces log files using PHP

PHP Markdown Extra

Script/website: michelf.com/projects/php-markdown/
Requirements: PHP
Installation time: Varies. Wordpress plugin installation takes less than a minute
Difficulty: Easy
License: Free, BSD-style open source
Download: PHP source in zip
API: N/A
Author Description: “The Markdown syntax allows you to write text naturally and format it without using HTML tags. More importantly: in Markdown format, your text stays enjoyable to read for a human being, and this is true enough that it makes a Markdown document publishable as-is, as plain text. If you are using text-formatted email, you already know some part of the syntax.”

screenshot of using PHP Markdown through Dingus demo preview

Based on John Gruber’s Markdown (Perl), PHP Markdown can be used a Wordpress plugin by copying over the markdown.php file from the zip file to the wp-content/plugins directory. There is an official demo called Dingus here. The screenshot above shows my example of using PHP Markdown syntax through Dingus.

The function Markdown($text) inside markdown.php is passed a text string and returns the marked down version, so incorporating this into another program where you want to markdownify the text is pretty straightforward, although the file is a bit on the bulky side at 55k. Would be more convenient if a version without Wordpress and bBlog code inside it were provided in the zip package. The most recent version as of this writing was September 5, 2005.

Search and research file extensions with FILExt

We have a Sony VAIO laptop that came preinstalled with Callisto’s Photoparade Slideshow and all my PHP scripts that I downloaded tried to open in that program because it had the .php extension. I changed the association to open in notetab light.

The time will come in your programming and computing travels that you come across extensions you aren’t familiar with and that’s where the FILExt website comes in handy. I want to type fileext but there is no extra ‘e’ there. Check out the .php extension filext page and you’ll see it’s shared by three other programs too like the Microsoft’s Picture It! Publishing Project File format.

How to change background color automatically on holidays

Ever notice how Google changes its logo on different holidays throughout the year? I decided this morning I wanted to change the background color and some other themes on one of our websites.

With Halloween, Thanksgiving and Christmas coming I added some code this morning to one of our sites for implementing different background/theme switching so that it would automatically change the color scheme across the website during the holiday.

I did something similar to change the background color every day of the week in my PHP Diary: December 19, 1999 (and then the follow-up on December 20, 1999 a better version using an array instead of if/else statements). That background changing script has been running pretty much unmodified every day for nearly six years now.

How to change the background color of an HTML page only on Halloween and Christmas day

<?php
$background_color = ‘#FFF’; // default WHITE background
switch(date(“md”)) {
     case 1031:
       // automatic halloween background color (light orange) on 10/31
       $background_color = ‘#FFE6CC’;
     break;
     case 1225:
       // xmas day background color (light green) on 12/25
       $background_color = ‘#D5FFD5′;
     break;
}
?>

<body bgcolor=“<?php echo($background_color);?>”>
?>

To modify the script for other colors and holidays just add additional case blocks like say we wanted the background to be red for 4th of the July we’d add the following case block:

case 0704:
$background_color = ‘red’;
break;

And so on.

Brief explanation of how to use arguments and globals inside functions

The concept of globalization, functions and arguments can be confusing to those who are new to programming. It’s not too complicated if you look at a very basic example. The following function isn’t very practical, but it works.

<?php
function addThis($a,$b) {
return $a + $b;
}

$sum_of_these = addThis(8,3); // assigns the value of 11 to the variable $sum_of_these
?>

In the above example we are passing by argument the numbers 8 and 3 to the function called addThis and that function is returning the value of 11 (which is 8 + 3) and then assigns the value of 11 to $sum_of_these variable. This would be the same as writing the following without using any function:

$sum_of_these = 8 + 3; // or $sum_of_these = 11;
 

Now, let’s say we want to add in a third variable, called $c to the equation. How can we do this in the function addThis without passing it as an argument? We do this by making the $c variable globally available inside the function addThis like this:

<?php
function addThis($a,$b) {
global $c; // separate multiple globals by a comma like this: global $c, $d, $e;
return $a + $b + $c;
}

$c = 4; // first must assign some value to $c
$sum_of_these = addThis(8,3); // assigns the value of 15 to the variable $sum_of_these
?>

Now let’s say we don’t want to use any arguments at all, let’s globalize $a, $b and $c. This code would look as follows:

<?php
function addThis() {
global $a,$b,$c; // separate multiple globals by a comma and end with a semi-colon
return $a + $b + $c;
}

$a = 8;
$b = 3;
$c = 4;
$sum_of_these = addThis(); // assigns the value of 15 to the variable $sum_of_these
?>

Still the use of a function above doesn’t make practical sense, but it shows a very rudimentary example of how to use functions, arguments and globals. Now let’s make a better example of a simple calculator:

<?php
function simpleCalc($operator) {
global $a,$b,$c; // separate multiple globals by a comma like this: global $c, $d, $e;
switch($operator) {
   case ‘-’:
     return $a - $b - $c;
   break;
   case ‘*’:
      return $a * $b * c;
   break;
   case ‘/’:
      return $a / $b / $c;
   break;
   default:
      return $a + $b + $c;
   }
}

$a = 8;
$b = 3;
$c = 4;
$result = simpleCalc(‘+’); // assigns the value of 15 to the variable $sum_of_these
echo($result);
?>

The above example allows us the flexibility of sending along any three numbers and either add, subtract, multiply or divide. I used a switch statement to illustrate how to check for addition (+), subtraction (-), multiplication (*) or division (/). Readers will note that the default switch is addition, so there is no need for a case block for that.

So if we wanted to multiply the three variables we’d use the following code:

$result = simpleCalc(‘*’); // $result = 8 x 3 x 4 = 96
 

Subtraction, just pass along the ‘-’ like this:

$result = simpleCalc(‘-’); // $result = 8 - 3 - 4 = 1
 

This is a real crude and impractical calculator because a user could enter in a combination of more than three numbers and even multiple operators, but it clearly demonstrates how to pass and return arguments in functions, how to globalize variables and how to perform different operations based on conditions. At the most basic level that is all a calculator does: it takes input and processes.

I’ll leave this type of calculator for a more advanced tutorial (a Part 2, perhaps) or maybe readers would like to build their own? Basically to accomplish a full calculator, the programmer would need to dump all numbers and operators into an array and work through that in the function to return the value. This might be kind of fun for a quick and basic JavaScript app. Don’t even need server side PHP code for this, it could be done entirely in JavaScript in a step-by-step process because a calculator is somethng most everybody understands.

11 built-in PHP sorting options summarized and reference

PHP has 11 different built-in sorting options as of this writing and the manual, though offering good examples of how to use most of them, doesn’t really put them all on one single, quick summarized reference page. Here’s my attempt at doing just that.

sort - sort alphabetically (a-z) or numerically (0-9)
rsort - sort reverse-alphabetically (z-a) or numericall (9-0)

asort - sort alphabetically or numerically, maintain $array indices, used for associative arrays
arsort - sort reverse-alphabetically (z-a) or numerically (9-0), maintain $array indices, used for associative arrays

array_multisort (PHP 4, 5) - sort mult-dimensional or multiple arrays, numeric keys will be re-indexed but strings will be maintained.

Sorting ORDER flags: sort_asc (default) - sort in ASCending (a-z, 0-9) order
sort_desc - sort in DESCending order (z-a, 9-0)

Sorting TYPE flags: sort_regular (default)
sort_numeric - compare items numerically
sort_string - compare items as string

ksort - sort array alphabetically or numerically by keys, used for associative arrays
krsort - sort array reverse-alphabetically (z-a) or numerically (9-0) by keys, used for associaative arrays

natsort (PHP 4, 5) - case sensitive sort using human natural sorting, useful for sorting things like upper or lower (but not mixed) filenames with strings and numbers
natcasesort (PHP 4, 5) - case INsensitive sort using human natural sorting, useful for sorting things like mixed case filenames with strings and numbers

uasort - sort array with a user-defined comparison function and maintain index association
uksort - sort array by keys using a user-defined comparison function, useful if no other available sorts apply like sorting objects

An additional function that might come in handy once sorting some arrays like the natsort is array_reverse (PHP 4, 5) which will take the contents of an array and return them reverse. For example, I needed to sort the following data:

ref_08_2005
ref_09_2005
ref_10_2005

so that it would appear in a dropdown SELECT menu like this:

ref_10_2005
ref_09_2005
ref_08_2005

This was accomplished by using the following code:

<?php
$unsorted = array( ‘ref_08_2005′‘ref_09_2005′‘ref_10_2005′);
array_reverse($unsorted);
foreach($unsorted as $each) {
  print “$each<br />”;
}

/* 11 sorting functions PHP manual links
http://php.net/sort
http://php.net/rsort
http://php.net/asort
http://php.net/arsort
http://php.net/array_multisort
http://php.net/ksort
http://php.net/krsort
http://php.net/natsort
http://php.net/natcasesort
http://php.net/uasort
http://php.net/uksort
*/

?>

Over time the plan is to update and refine this PHP sorting reference based on comments/feedback and future research so please use the comments area to help improve this information and keep it relevant timely. This one, if you like, might be good for a bookmark.

get_defined_vars returns defined variables

Today in the #scriptschool chat we talked about how to get the current defined variables used in a script. Check out the following code which illustrates the use of get_defined_vars(). The variables set will be at the end of the output:

<?php
$myname = ‘TDavid’;
$mydomains = array(‘http://www.php-scripts.com’,‘http://www.tdscripts.com/’);
$arr = get_defined_vars();
echo ‘<pre>’;
var_dump($arr);
echo ;
?>

This is particularly useful for keypoint checking/debugging.

Apache, PHP, JSP and MySQL on Windows XP / 2000

My friend, Lestat, and I were talking earlier today about PHP and Windows earlier today and tonight in my PHP stream I noticed the following article from mpcon.org:

This is a quick guide to install and configure the Apache web-server with PHP and JSP support on a Windows XP Pro SP2 machine. I also included PHPMyAdmin, MyODBC, and WordPress.

Looks like this might come in handy as a reference.

BTW, fellow coders, you can find several of us down at irc.scriptschool.com #scriptschool throughout the week. Feel free to stop on by and say hello :)

Upgrading the built-in WP search

Producing search results from a Wordpress-powered blog that look like something a traditional search engine would produce is not as difficult as it might seem, thanks to a comprehensive plugin and template design. While making some changes, you can also try using a different search results algorithm than the default one.

The Search Reloaded Wordpress Plugin by Denis at semiologic seeks to improve upon the built-in WP search.

search reloaded plugin plus page navigation screenshot

To use just download, unzip, and FTP to the wp-content/plugins directory. Then go into the admin area and activate the plugin.

If you want to adjust the look of the search results you’ll need to edit:

function sem_search_results()

This function is at the end of the included sem-search-reloaded.php file and contains code that looks very similar to what is in the default template search.php file.

In the example pictured above, we also used a slightly modified version of GaMerZ Page Navigation plugin. This plugin is useful for spanning page, category and search results.

ARSC v3.0.2 RC 2 free chat

Looking to add chat to your server? Don’t want to mess with java and IRC? Want to try a PHP/MySQL solution? ARSC could be for you.

ARSC free PHP / MySQL chat

From the 4-step installation from INSTALL docs:

- Create a MySQL database
- Provide the hostname, username, password, and database
name to allow ARSC to connect to your MySQL Server
- Copy the ARSC files onto the webserver
- Open the install script url in the webbrowser

We have been looking to do some kind of chat for Webmaster Cookbook, so I just conducted a quick installation there to see how long the base installation actually took.

Started 8:02am. End: 8:15am. 13 minutes which included creating the database and user/pass in PHPMyAdmin and FTPing the files.

Notes
- don’t forget to delete the install subdirectory
- customize the outside and inside pages using the layout option in the admin area

« » Pages (999999) : « First ... 4 5 [6]7 8 ... Last »