Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Craig Spurrier craigweb.net

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Find the url of the directory the script is in

// description of your code here

    // Copyright (C) 2007 Craig Spurrier
    // Released under the terms of the MIT/expat license.
function baseurl() { 
    if(!empty($_SERVER["HTTPS"])){$http = "https";}else{$http = 'http';} 
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
    $basename = preg_replace('/^.+[\\\\\\/]/', '', $_SERVER['PHP_SELF'] );
    $dir = substr($_SERVER['REQUEST_URI'], 0, -strlen($basename)-1);
return "$http://".$_SERVER['SERVER_NAME'].$port.$dir; 
} 
$baseurl = baseurl();

Check/Uncheck all checkboxes based on one check box

Bassed on http://www.bigbold.com/snippets/posts/show/350
<html>
  <head>
    <script language='JavaScript'>
      checked = false;
      function checkedAll () {
        if (checked == false){checked = true}else{checked = false}
	for (var i = 0; i < document.getElementById('myform').elements.length; i++) {
	  document.getElementById('myform').elements[i].checked = checked;
	}
      }
    </script>
  </head>
  <body>
    <form id="myform">
      <input type="checkbox" name="foo"/>
      <input type="checkbox" name="bar"/>
      <BR>Check all: <input type='checkbox' name='checkall' onclick='checkedAll();'>
    </form>
  </body>
</html>

A tool to split log files by hostname

This script can be used to split a log file by the hostname in the request. It is designed for use with lighttpd virtual hosting, to prepare the log for Webalizer. It should be run with a cron job and CLI PHP.

<?php
// Copyright (C) 2006 Craig Spurrier
// Released under the terms of the MIT/expat license.

$serverip = "127.0.0.1"; //Set to your IP address or default hostname, so requests without hostnames can be sorted 
$log = file_get_contents("/var/log/lighttpd/access.log"); //Log location
$fh = fopen("/var/log/lighttpd/access.log", 'w'); //Clear the log file
fclose($fh);
$lines = explode ("\n", $log);
foreach ($lines as $line){
$parts = explode (" ", $line);
$hostname = strtolower($parts[1]);
if (substr($hostname, 0, 4) == 'www.'){$hostname = substr($hostname, 4);} //Treat www. as the same as the www-less version
if ($hostname == '-'){$hostname = $serverip;} //Set a hostname for request without hostnames
$fh = fopen("/var/log/lighttpd/users/$hostname.access.log", 'a'); //Add the entry to the log for that hostname. Make sure this script can write to this location.
fwrite($fh, "$line\n");
fclose($fh);
}
?>
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS