Ping with PHP

March 13th, 2010

The traditionally ping command uses the ICMP protocol rather then TCP.
I made this php script which uses the TCP protocol instead.

#!/usr/bin/env php

<?php

try {
    $input = $_SERVER['argv'];
    list($host, $port, $byte) = array($input[1], $input[2], $input[3]);
    if(!empty($host) && !empty($port)) {
        $time = microtime(true);
        if($socket = fsockopen($host, $port, $errno, $errstr, 20)) {
            fwrite($socket, str_repeat('x', $byte));
            fclose($socket);
            $newtime = microtime(true);
            print ($newtime - $time) * 1000 . ' ms' . "\n";
        }
        else {
            throw new Exception($errno . ': ' . $errstr);
        }
    }
    else {
        throw new Exception('The host/port was empty');
    }
}
catch(Exception $e) {
    print $e->getMessage() . "\n";
}

?>

Usage:
Create a new file, name it ping.php and paste the code into it.
Write chmod +x ping.php.
Run it with “./ping.php google.com 80 32″
80: port number, 32: number of byte.

echofish@ubuntu:~$ ./ping.class.php google.com 80 32
209.147930145 ms

Nettby bot

March 5th, 2010

Want to get more page hits on your profile on a community site?
This script will visit all/many profiles, and when the ones you have visited will see that you have been on their profile and will most likely visit your profile too. I made this script for the Nettby community site many years ago and I just found it going through some old documents.

The script should work on other community sites as well, you obviously need to change the url, though.

The script creates a window inside you current window and will jump through profiles at a very high rate. The rate is in milli seconds and the default value are 100ms per profile, but you could just change it.

Be careful if you try to use this on Nettby now days, you can only visit 2500 profiles a day or something like that, or you will get banned.

Usage:
Login to Nettby or any other site and write this in the address bar. Make sure to not leave the page while its running, open a new tab or something if you want to browse the page while the bot crawls through the profiles :)

Click on the SyntaxHighlighter’s view source button ;)

javascript:document.body.innerHTML += "<iframe id='if' height='400' width='400'></iframe>"; var i=0; var frame = document.getElementById('if'); setInterval(function(){ frame.contentWindow.document.location = 'http://www.nettby.no/user/?user_id='+(i++)}, 100); void(0);

Swap values of two variables

February 24th, 2010

Here is a cool little thing, we use xor to change values of two variables, without a temp. variable, function or anything else:

php > $a = 'a'; $b = 'b';
php > $a = $a ^ $b;
php > $b = $a ^ $b;
php > $a = $a ^ $b;
php > print $a;
b
php > print $b;
a

MySQL escaping

February 23rd, 2010

Every now and then I see some sites that fails to input/output data from the database, like: don\’t.
My heart hurts every time, and it’s not just small pages, it’s big community/news pages as well.

The reason why this happens, is that people forget to check if magic_quotes_gpc are true. magic_quotes_gpc is a setting in php.ini and will escape input automaticly from GET, POST and COOKIE. If this setting is on (and in many cases it is) and then use mysql_real_escape_string as well, it will result in double escaping. On output some people tend to forget to strip slashes.

I have made these filter functions to show how it could be done, but there are always better alternatives.

<?php

function mysql_in($data) {
    if(get_magic_quotes_gpc()) {
        $data = stripslashes($data);
    }
    return mysql_real_escape_string($data);
}

function mysql_out($data) {
    $data = stripslashes($data);
    //$data = htmlspecialchars($data);
    //$data = nl2br($data);
    return $data;
}

?>

Why Cal Henderson hates Django

February 23rd, 2010

Cal Henderson talks about why he hates Django. It’s very entertaining and good learning :)