Archive for the 'PHP' Category

PHP vs Perl (final)

In the first post I discussed the sick amount of functions in PHP compared to Perl, in the second one the reason why writing Perl applications goes faster and now in this third and final post, I'll discuss external connections.

Connecting to an other server is useful for a number of reasons, think about content scraping or posting messages to a website or forum. Both PHP and Perl have great support for this, but this time I think PHP wins the prize for being the most useful. This is a very strange thing to say because exploits etc are mainly written in Perl.

When you're connecting to a website in Perl, you can use LWP::Useragent, in PHP it's easier and you can just use file_get_contents("http://www.randombase.com"). These are only suitable for HTTP servers so not that useful.

As soon as you're trying something more advanced (IRC bot?) you need more powerful tools.  In PHP, the way to go is fsockopen(), this is an awesome function that is very easy to work with, it can work with virtually any protocol and gives you endless possibilities. The same goes for Perl's IO::Socket. This extension is maybe more powerful than PHP's, but for some reason I think it's unnecessary complex, there is too much information to be defined before you can actually use it.

So, my final judgement of this PHP vs Perl series would be Perl if you're writing something intended for personal usage or when you need to write a quick hack. I also think Perl is just candy for people who are good with advanced structures, because good and efficient Perl code is almost unreadable in the end. PHP is easier to pick up because of the enormous documentation (Perl's documentation is shattered on the web), and still the unbeaten king for developing websites.

Streaming things (example)

Took a while but here is my example for the post streaming things:

http://iron.randombase.com/monitor.php

This application shows the serverload, both graphically and in numbers. This example uses a random number generator instead of showing the actual serverload, so don't worry, our server isn't that unstable. I can give you the source code on request, just send me an e-mail!

Streaming things

Now I'm truly slacking off. It's been nine full days since my last blog post, but same reason as always, nothing interesting is happening and one project is sucking up all my time.

I was working on a dumb game but realised it was dirt so I quit working on it, mainly because of the forced lag you get through AJAX and PHP. Little did I know at the time I wrote it because apparently there are ways to keep the connection open, even in PHP and AJAX, instead of resending the headers each time. This would reduce lag greatly, and get normal latency. I'm only going to discuss/explain one of two methods I read about because I didn't quite get the other one.

The method is, very simply put: you keep your PHP script running and use Javascript to dynamically update the things on the page. Using usleep() to define the amount of lag you want, and of course deciding what amount of server load you're willing to give to your script. I'm not giving any example, it's late already and it's not hard.

Using Javascript's function getElementById() you can easily manipulate things on the homepage, and use flush() to get the output constantly sent to the browser.

flush() already

A function that has been on my ignore-list for quite some time, but not anymore. PHP's function flush() makes sure the output you have generated gets sent to the browser. Now, why is this useful? To make it obvious your script is still running to the end-user for example.

Imagine you have one of those lame "value my site" scripts, that connects to the Google PageRank server, Alexa, Yahoo Backlinks and what not. 99% of all scripts simply connect to the servers in one turn, making the script look frozen for atleast a couple of seconds. Statistics show all users leave if your script is running more than 9.81 seconds (OK, I made that up).

To fix the frozen state, add a flush() line between every connection, example:

<?php

connectToGoogle();

flush();

connectToAlexa();

?>

The moment the script is finished with connectToGoogle(), it will show the output it has already and continue to Alexa. Not clear enough? Here is my little example:

http://iron.randombase.com/flush.php

Distributed hash cracking through Javascript

Mad ideas always score, and this one is almost finished! The basic idea of this project is to use the CPU of our visitors for cracking passwords, encrypted in md5 or SHA1 or something like that.

The code I have so far is quite simple, it works like this:

Step 1: generate a MySQL table filled with the so called "ranges" or "blocks", a block is a part of the amount of hashes you want to generate for cracking your hash. For example, the blocksize I'm currently using is 50. So if I want to generate a million hashes, I'd create 20,000 blocks.

Step 2: create a server file for the AJAX function in the Javascript cracker. The server file is written in PHP and returns a random, uncompleted range from the database.

Step 3: the moment a user visits a page, the AJAX function retrieves a random block from the server and starts cracking it (small blocksize so the cracking only takes a few miliseconds). If the block is finished, it tells the server. If the hash is cracked, it also tells the server. The server checks this (no invalid input by evil users, kthxbye) and if this is correct, removes all remaining blocks.

The main issue here is that you need a huge website to get really good speeds. RandomBase receives around 3 hits / second, which would mean only 150 hashes / second.

Sending mails from php’s mail() to Hotmail

This is a known issue, and there is a known solution, but it's hard to find when you really need it.

When you're sending an e-mail from php mail() to a Hotmail account, most of the times it will a) never arrive or b) get marked as spam. There is a fix though: you need to send some extra headers with the request, make it look like it was sent from a real mail client.

<?php

$to = "I Ron <iron@hotmail.com>";
$subject = "It arrived on Hotmail?";
$message = "Yes it did! :o";
$headers = "From: RandomBase <iron@randombase.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "Reply-To: RandomBase <iron@randombase.com>\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "X-Mailer: RandomBaseMailer";
mail ($to, $subject, $message, $headers)

?>

It's that simple. It would be even more simple if people stopped using this piece of AJAX based spyware that filters everything but spam messages.

Crap, junk and a longtail keyword finder

Hello my few readers, been a while since I last updated my blog so I felt like posting something so I didn't disappoint any of you.

I've been mainly busy with this Dutch Battlefield Heroes forum and playing the game (in beta), the addiction continues. But I've did a little coding too and I'll share the idea with you guys, not the code since it's not functional like I want it.

The very basic concept, and also the only thing it does now, is to start off with one keyword (say "php") and end up with a longer string with few results (say "php tutorial about creating games with mysql and javascript and all other things"). The point of finding these is of course to find less competitive niches that should convert well for marketers. The code uses a part of Google search.

The idea was fun, but it didn't work out very well, I'll put some more time in it later. Here are some examples:

Start: php

27200000 => php split
3920 => php split REG_EMPTY

Start: work

1030000 => worksource
173000 => worksource WA
165000 => worksource wage subsidy

Start: health

23900000 => health card
748000 => health cards ontario

I'm still trying to tweak it, I guess I should be breaking down the keywords and try to reorder them etc to reach the point of a niche with < 10000 results.

PHP vs Perl part II

Perl has these tiny little functions that make me use it more for small, personal one-time use things. PHP has support for some of them but often it feels just wrong and not supposed to be there. My favourite example is unless() (should I even add those ()?). Perl has the most awesome function ever:

print 'Hello world.' unless $world eq 'Destroyed';

That all, in one line. PHP has (together with Javascript) a good but too long and confusing option for, the shorter version of if(), I use it all the time. It would look like this in PHP:

print ($world == 'Destroyed') ? '' : 'Hello world';

Ok, that's a pretty sweet function but it requires me to type too much special characters.

The second function I would like to talk about is ... well it has no name, it's just something you can do. In short, it is this:

($arrayOptionOne,$arrayOptionTwo) = @array;

That is the Perl version, PHP has this one too but they really had to add a specific named function for it:

list($arrayOptionOne,$arrayOptionTwo) = $array;

And then one of the functions I use all the time for things that involve editing files:

open(FileHandle,"<fileName");

print join("",<FileHandle>");

That's it! That's just it, we have read a complete file in two lines of code. The PHP version is horrifying here:

$fileHandle = fopen('fileName', 'r');

$data = fread($fileHandle, filesize('fileName'));

fclose($fileHandle);
print $data;

Now I hear some people say: what's wrong with file_get_contents() then? Nothing, except it doesn't allow you to change the mode parameters (you know: read, write, append, ..) which is quite useful.

After all, anything that I need to write quickly for a task is written in Perl, but in the end PHP is the most powerful language for websites so don't get all mad on me for not mentioning that before.

Multiplayer… thing

The people I had testing it shared the same question: "what the hell is its purpose?". Well; surprise, it doesn't have one. The idea was born when r0bin and me were playing Bomberman (sweet game by the way), and I wondered if it was possible to recreate such a thing in Javascript (+AJAX for multiplayer).

The problem with Javascript is the lag you automatically have, you can't get around the headers you're sending each time. At the best moment until now, I noted down a latency around 200. That means you would be able to send five actions each second. In a normal multiplayer game you should have a latency < 75 to keep it playable and fair for the other players.

So, the plan on recreating Bomberman faded but a new one was born, creating something where you can walk around and chat, that's it, nothing more. It is working now (but can be very buggy), but will most likely stay where in the stage it is now, I don't see any bright future for it.

The code isn't magical either, it is the typical result of working on it in seperated sessions spread over a couple of days, not commenting the code and my extreme lack of knowledge from Javascript. I only know the very basics which seems to be leading to a lot of bugs in attempts to advanced applications. The serverside synchronisation is managed through a PHP script that returns an array to the Javascript application of the location of the different players. The game supports virtually an unlimited amount of players.

Even though I'm not going to "finish" this code, doesn't mean I'm not going to create more of these things in the future, I actually quite enjoyed creating this.

Details

http://iron.randombase.com/multi/

Arrow keys to move, "enter" (return) to chat.

Stupid code: Acronym solver

Solver is invalid actually, guesser is more correct. It just randomly puts words in place of the letters.

For example, it "solved" laser into:

lack anniversary slave enormous regard

And /dev/iron became:

/ distribution experiment vehicle / insist racism overnight nation

I wrote this code for fun but it has turned out to be maybe the most efficient piece of code I have ever written, which is kind of annoying since it doesn't have any real purpose. The magic link:

http://iron.randombase.com/acronym

Next Page »