SAJAX – Simple Ajax Toolkit by ModernMethod – XMLHTTPRequest Toolkit for PHP

Now that XMLHTTPRequest has been renamed Ajax by Joe Public, it’s gaining more and more attention.
SAJAX is another toolkit, along the same lines as JPSpan in that you can call PHP functions directly from your web browser. The latest release also has backends for Perl and Python. Other language backends are a possibility if they’re contributed.
One major drawback of this project in it’s current state is, “Lack of type checking and support for rich return types”. JPSpan allows complex data types such as arrays to be passed back and forth between Javascript and PHP and from what I can see of the examples, SAJAX doesn’t support asynchronous calls either, yet.

multiplication example

/* PHP */
function multiply($x, $y) {
  return $x * $y;
}

/* JAVASCRIPT */
function do_multiply() {
  // get the folder name
  var x, y;

  x = document.getElementById("x").value;
  y = document.getElementById("y").value;
  x_multiply(x, y, do_multiply_cb);
}

Bye bye Referer Spammers!

Take a quick look at your logfiles any time and you’re likely to see referer spam in there somewhere. Not only do those requests pollute your log files and stats pages, but they also consume resources on your server when you serve them pages that aren’t even going to be viewed by anyone. Here’s one way of stopping the spammers eating into your server resources:

  • Look through your logfiles and examine the referers. Here’s a quick bit of code to do that. (Remove the backslashes (“\”) from before double quotes. WP is putting them in on me!) :
    awk '{print $11}' < /var/log/apache2/access_log| sort|uniq -c|sort -r|grep -v "mydomain.com"|less
  • Copy and paste any likely looking referer spam sites somewhere else for safe keeping. The ones that use most of your resources will be at the top of the list.
  • Add this code to some page that every page on your site loads, it should be included before main execution of the page occurs. Fill in the array of referer sites with the list your assembled from your log file. I’ve added a few from this morning’s log file.
    if( isset( $_SERVER["HTTP_REFERER"]  ) )
    {
        $referers_to_avoid = array(
                "ttp://texas-holdem.andrewsaluk.com",
                "ttp://www.highprofitclub.com/",
                "ttp://www.sex4singles.com/",
                "ttp://www.parishillton.com/",
                "ttp://www.moneylinebet.com/",
                "ttp://www.free-hentai-anime-sex.com",
                "ttp://www.bondage-bdsm.us",
                "ttp://www.handjob-movies.us",
                "ttp://www.zoothumbnails.com",
                "ttp://www.bestiality-animal-sex-stories.com",
                "ttp://www.gay-men-sex-movies.com",
                "ttp://russ-darrow-kia.gq.nu/",
                "ttp://nissan-xterra.sbn.bz/",
                "ttp://nissan-thermos.gq.nu/",
                "ttp://folding-chair.wol.bz/",
                "ttp://www.xcites-0-cost-interracial-cum-teen-sex-movie.com"
        );
        while( list( $key, $val ) = each( $referers_to_avoid ) )
        {
            if( strpos( $_SERVER["HTTP_REFERER"], $val ) )
            {
                die();
            }
    
        }
    }
  • Add an error_log() to the “if” condition to spot when a spammer visits.
  • Add this to index.php of a WordPress installation to protect your blog and make your legitimate requests go that much faster!

PHP Future – Java or Simplicity

Conflict PHP is an opinion piece on the state of PHP. PHP5 (which I haven’t tried yet) is going down the road of Java and copying it’s object model and other ideas.
That’s no bad thing but most PHP developers surveyed don’t want that.
Adoption of PHP4 was much faster than PHP5 is, possibly because there was such a huge difference between versions 3 and 4. If PHP5 features were really “must haves” then people would be crying out for them. Zend is getting more “corporate” and I hope it doesn’t backfire on them!
John Lim has more on the issue.

Validate Email Address in PHP

It’s a common task but one that I haven’t looked at in as much detail as this devshed article. There’s a few bugs in the code, and a function was posted to the comments that’s all over the place.. here’s the same PHP function, cleaned up, to verify an email address. Note that it won’t work in Windows as-is because “checkdnsrr()” isn’t available on Windows platforms. (Remove the \ characters from before the ” characters surrounding the regex.. WordPress adds them when I post this. *grrr*)

function checkEmail($email)
{
  // checks proper syntax
  if( !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
  {
    return false;
  }

  // gets domain name
  list($username,$domain)=split('@',$email);
  // checks for if MX records in the DNS
  $mxhosts = array();
  if(!getmxrr($domain, $mxhosts))
  {
    // no mx records, ok to check domain
    if (!fsockopen($domain,25,$errno,$errstr,30))
    {
      return false;
    }
    else
    {
      return true;
    }
  }
  else
  {
    // mx records found
    foreach ($mxhosts as $host)
    {
      if (fsockopen($host,25,$errno,$errstr,30))
      {
        return true;
      }
    }
    return false;
  }
}

What's the GPL? WordPress and PEAR Cache Problems

Ben Ramsey explores some of the issues when you write GPLed code that uses code from the PEAR library.
I had forgotten about the differing licenses used by PEAR and WordPress. They’re unfortunately incompatible and you can’t ship PHP licensed code in a GPL project without an “exception clause” in your GPL license. A change to the license of WordPress would require the agreement of *all* copyright holders of code in the project AFAIK.
Thankfully, I don’t ship PEAR Cache with WordPress MU. I use it if it’s installed already, WPMU isn’t dependant on PEAR Cache being available to work.
I think that gets around the incompatibility. Doesn’t it?

F-Spot mini review

I like F-Spot. It’s useful, it does tagging that no gqview and gthumb don’t. It has organisation features that they don’t have either, but it’s a little too buggy for widespread use yet.
It was really easy to use however. Just “import” a few directories of photos, create a few tags, and then select a few photos, right click and add a tag to those photos.
Unfortunately it crashed a few times after an import, and exif reading seemed to be completely broken but then I never checked if the proper library was installed. Except for rotating, I *never* use a simple photo viewer to manipulate images so I didn’t try out those features of the app. IMO that’s a job for the GIMP and I save modified files to a new directory.
I’m going to keep a close eye on it to track how it progresses. It’s shaping up to be a great desktop application!
A hint for Gnome users – if you haven’t got much room in your home directory move the “~/.thumbnails” folder out of the way somewhere else with plenty of space and symlink it back. Mine’s at 171MB and growing fast!

While we’re on the mono theme, a mono developer, zbowlin1 on attracted my attention when he said..

<zbowlin1> I just did something really really cool. I have a much better version of php+gtk by calling the GTK and System.Windows.Forms (SWF/MWF) classes using PHP as a runtime and using the .NET/Mono invoking classes in PHP 🙂
<zbowlin1> I just made php as runtime and used GTK# like a runtime, and used my Gecko# port and opened a stream and used PHP methods like calendar and Smarty and stuff to generate the content for the page.. most php scripts should work and you can run them and render tables and stuff in gecko#/gtkhtml windows and the rest in lables and menus
<zbowlin1> its like a much much better, cross platform (thanks to mono), PHP+GTK that kills the crap out of the need to make stubs and makes it gtk2 compatible and opens up billions and billions of possibilities for people who only write PHP or people who want to code one set of common functions for like an application version and web page version.. and it can work on the fly
<zbowlin1> humm… i’m on the mono team and i’ve working on my C# based offline wordpress blog writer with a WYSIWYG interface and spell check.. this just made that idea like 3923501235 * better
<zbowlin1> i don’t have to rewrite interfaces. I can use built in php xml-rpc support. AAAAH man it boggles the mind the amount of possiblities..
<zbowlin1> you can gain access to xpcom in mozilla so you can automate XUL with PHP… you could write Mozilla extensions php.. you can call anything, or do anything, on mac, windows, linux, freebsd, etc..
<zbowlin1> holly crap
<zbowlin1> i’m mind boggled
<Firas> XUL and GTK are pretty different toolkits?
<Firas> * aren’t
<zbowlin1> they are very different
<zbowlin1> but xul will render with gtk and gtk2 if you have them
<Firas> oh, right
<zbowlin1> but it also render with QT, Win32, Cocoa, BeOS, etc
<Navid> XUL is nuts. I need to pick up on that.
<zbowlin1> you write everything mostly in javascript and xml
<zbowlin1> you could write the blogging interfaces and menus into my interfaces… if you have local access to the database it should work that way but you could automate it with some work and make it work with XML-RPC

zbowlin1 is at zacbowling.com and go-mono.net
I always want to create cross-platform applications, if .net and mono can do that I need to look more closely at them!