Wednesday, June 30, 2010

How to create a socket server in PHP

EDIT: If you need to submit bugs or want to improve my
work, its now available on github!

Ever
tried searching for information on how to properly create a multi-client socket server in PHP?  You’ll get
plenty of results with outdated and messy source code, some of which won’t even work.

This
was the conclusion I'd come to a couple years ago when I decided that I wanted to try my hand at writing an
IRC server.  The why is not important… (For the fun of building an IRC Server, if you can call
that fun
).  So I googled around a bit until I finally found some code that worked on its own, and
quickly built a semi-functional IRC server using it, and headed off to sleep at 5am.

The
next day I was very, very happy with the results of my hard labor, but it wasn't good enough, so I started
re-writing it from scratch as an Object, and thus I created class::IRCServer.

Then,
once I felt that I was finished screwing around with my newly built IRCd, I decided to modify the function
enough to be used on its own as a socket server, to share with the world.  However, that was a couple years
ago (The non-edited version of this article was written in 2010).

Recently, however,
PHP has evolved to a point where the Socket Server class I had written was out-dated, buggy, and just plain
bad.

So I built a new library from
scratch after looking at the old code, and came up with Navarr\Socket
.  The code is open-source,
MIT-license, and available on Github.  I heavily encourage you to fork and submit pull requests.  The code
is namespaced, follows the PSR-2 standard,   and even has some PHPUnit tests.

Monday, June 28, 2010

Magical Typesetting in PHP

So, well working for Route 50
I came up with a fantastic idea for “typesetting” that well exceeded the norm.  Something we constantly have
issues with is what type of string was sent to MySQL originally (some of us have different conventional
ideas about where escaping HTML should be located.) as well as outputting that string in its correct format.

Me,
with my fantastic idea, came up with a couple variables classes that I put in a file named href="http://gist.github.com/451216">class_typesetting.php.  The version on gist.github is
slightly modified from the original version on the server.

It creates three classes,
GenericVariable, String, and Number.  So far we haven’t used GenericVariable, but since the introduction of
the classes I’ve taken it upon myself to introduce them to any new code I write.  When we create Core v5
(which will Objectify everything) strings taken from SQL will automatically be re-stored as String class
variables.

First, lets examine some useful functionality.
[codesyntax
lang="php"]<?php
$title = new String($_POST[“title”]); // <strong>Hello
'World'</strong>
?>
HTML Output: <?= $title->html ?>
(&lt;strong&gt;Hello 'World'&lt;/strong&gt;)
Text Output: <?=
$title->text ?> (Hello 'World')
SQL Output: <?= $title->sql ?>
(<strong>Hello \'World\'</strong>)
HTML Attribute Output: <?=
$title->html_attr ?>(&lt;strong&gt;Hello &#039;World&#039;&lt;/strong&gt;)[/codesyntax]

This
allows for quick and easy access to the variables without having to worry about escaping them.

I
recommend you hit the download link (class_typesetting.php) and play around with it.  Tell me about anything
that’s not working correctly and if possible implement it in your future code.  (This means I’m putting this
code in the “Public Domain”).