Tag: PHP

Automatically reboot minecraft on crashes

by Craig Mayhew on May.01, 2011, under Code

The latest version of minecraft has been extremely unstable for me, so I’ve written this quick script to automatically check if minecraft is running – and reboot it within 30 seconds if it isn’t. You will need php installed for this script to work.

  1. Install php on the minecraft server.
  2. Save this code to a file named “rebootCheck.php”

    <?php
    //options
    $url = 'localhost';//localhost or an ip address
    $port = 25565;//25565 is minecrafts default port
    $pauseLength = 20;//time between killing minecraft and booting it again
    //end options

    $fp = @pfsockopen($url, $port);
    if (!$fp) {
      echo 'SERVER DOWN! Rebooting it.',"\r\n";
      //kill anything with javaw.exe in the name
      `Taskkill /IM javaw.exe`;
      //take a nap, while minecraft shutsdown
      sleep($pauseLength);
      //boot minecraft
      `C:\Users\user\Desktop\minecraft alpha\Minecraft_Server.exe`;
    } else {
      echo 'SERVER UP :)';
    }
    ?>

  3. Add a scheduled task to run every 5 minutes and run the following command. Notice here that I have put php within the minecraft directory, you may not have php there and may need to adjust the directory accordingly.
    “C:\servers\minecraft alpha\php\php.exe” “C:\servers\minecraft alpha\php\scripts\check.php”
3 Comments :, more...

PHP email validation using regex

by Craig Mayhew on Jan.02, 2011, under Code

I keep seeing PHP where the programmer has written a regular expression to validate an email address.

Don’t do that! Use the filter_var() function that php comes with out of the box.

<?php
$emailAddress = 'person@example.com';

if (filter_var($emailAddress , FILTER_VALIDATE_EMAIL)) {
    echo $emailAddress,' is considered valid.';
}else{
    echo $emailAddress,' is considered invalid.';
}
?>

And if you want to tidy the email address up:

<?php
$sanitizedEmailAddress = filter_var($emailAddress, FILTER_SANITIZE_EMAIL);
?>
Leave a Comment :, more...

phpuk2010

by Craig Mayhew on Feb.27, 2010, under Code, General/Techie

Back from phpuk2010 and feeling like I really want to contribute to some open source projects! I’ll summarize the main points I took away from each of the talks I attended.

Josh Holmes – Keynote talk – The Lost Art of Simplicity:

Josh talked about how overcomplicating things seems to happen all to often and grave a great example of a man simply trying to pluck an apple from a tree. He introduced me to “the truck factor” which I knew by a different name but this one has a much better ing to it :)

Stefan Priebsch – AntiPHPatterns:

I took alot away from this talk. Stefan had some great usage cases where singletons are bad and I have come across similar problems in my own code. I still think are cases where it makes sense and they are great for chucking an example together, but for production code and unit testing they are a bit of a nightmare.

He also pointed out that using global within a class is a real pain for unit testing too and ideally you should use the class __construct() function to get any required arrays/objects from outside the class (dependencies).

cakePHP took a bit of bashing during the talk due to it’s insanely high number of globals. CakePHP wasn’t alone either it seems a great many open source PHP projects are taking steps to fix there high use of globals and singletons.

Remo Biagioni – Database Optimization:

This was a story about how a small side projected grew into a multi server nightmare in just a few short years. He gave some good examples of how not to manage your databases and some equelly good examples of how to speed up different aspects of the database server. In short:

  1. Reduce the number of queries as much as possible by merging them together.
  2. INSERT IGNORES are faster than a SELECT query and then a possible a INSERT query.
  3. Move the queue to memory
  4. Use LOW_PRIORITY on DELETEs that don’t matter.
  5. memcached is awesome, if your not using memcached or something like it then you really should.
  6. If possible, avoid; LIKE, GROUP BY, DISTINCT as these are all slow.
  7. Use something like beanstalked to manage the queue.
  8. They used an MD5 hash (calculated in php) as a primary/unique row key. This is faster than using AUTO_INCREMENT to manage the primary keys. Note: you could use sha1() for even less chance of a duplicate key.

HA Proxy, Google’s  MMM, NoSQL and CouchDB all got a mention near the end and during question time.

Kore Nordmann – CouchDB & PHPillow:

This talk introduced a completely new concept to me and got me excited about a new kind of database. CouchDB is different, very different to relational databases such a MySQL. Firstly it uses http to connect to it! This opens up immediate possibilities and new security concerns but means you can make a database connection within javascript in the users browser! There is no schema in CouchDB as the “rows” are infact JSON objects. This means each row doesn’t have to follow any kind of set rules, they could have a different number of “columns” or have very different data in each “column” such as a another object or multi dimensional array. Your probably thinking the same thing I was at this point, “do we have indexes in CouchDB? and how the hell would they work”. Well, there are indexes in CouchDB and they are very flexible, but they are harder to implemenet than in say MySQL. Indexes in CouchDB are called views. These views exist as code that is run to create e.g. a btree index. The great part is, you write the code for the view! and you can write it in javascript (or other languages with the help of plugins). This gives you far more direct control of your indexes and should result in you having a far better understanding of your databases internal workings. PHPillow is the framework for implementing coucheDB within PHP.

Juliette Folmer – Regex-fu:

Although there’s not alot to blog about from the regex talk, you really had to be there… Two things I did pick up was that PCRE is faster than POSIX and the php documentation for it is here http://php.net/manual/en/book.pcre.php. Also the built in and blindingly fast PHP Filter extension is often forgotten or not known about and the documentation is here http://www.php.net/manual/en/intro.filter.php.

Damien Seguy – PHP Code Audits:

Damien gave a good talk on security in PHP, he covered the usual stuff about REGISTER_GLOBALS being a terrible idea on any system, production or development. But went into far more detail on how he searches for security issues in a short space of time so that you can quickly audit your own code. The bottom line is search the php code with the help of a tokenizer for php injection in places that use backticks require/include etc and eval. Search the codes notes for swearing and keywords such as “todo”.

A good tip for finding redundent code he gave was search for variables that only appear once. Some of these will be global variables but some of them may well lead you to old or troubling code.

Tools he recommended were Groogle, Reviewboard, Rietvold and Smartbear.

Leave a Comment :, , , , more...

PHP UK Conference 2009

by Craig Mayhew on Mar.03, 2009, under General/Techie, News

Wow, that was worth the wait. I don’t think I’ve ever picked up so much knowledge in one day since college. Great organization and an all round brilliant day!

Here’s a summary of the talks I attended but I unfortunately had to miss talks by Scott MacVicar, David Axmark, Staurt Herbert and Stefan Koopmanschap. But I will be watching them when they are made available on the PHPUK website.

Talk 1: “Keynote” by Aral Balkan

Aral kicked the talks off by going through the history of computing and some of the more humorous sides of where programming began with the “ancient” punch card programs of the early 1900’s. He showed parallax scrolling with HTML/CSS, an augmented reality program in flash and talked about the new swx format. He also offered his solution “Gaebar” to the issue of Google not allowing you to save or backup your database from their cloud network. He also reminded us all about the Game of Life which was first created in the 1970’s and then showed us how he had used it within his companies logo. He really wanted to press the point that programming should be fun, really fun and if it’s not then you should stop and figure out how to make it fun again.

Talk 2: “Sharding Architecures” by David Soria Parra

I really didn’t take much away from this talk. It’s possible that I missed the point but others that I spoke to felt the same. David talked about the problems of splitting a database across multiple servers and then needing to know what data is on what computer by use of an algorithm or lookup. This is a very real problem for companies which have a large database and have hundreds or more disk writes as to split a database across multiple computers. However he did have some good points and shared his experience on designing a system that would know where to look for user data by simply knowing the user id.

Talk 3: “Of Lambda Function Closures And Traits” by Sabastian Bergmann

I was intrigued by the traits and the magic method/interceptor “invoke”. I will definitely be installing PHP 5.3 and having a play with these new features. Unfortunately many of the questions that audience members asked about closures such as how to access global variables and closure recursion were not fully answered due to these still being in the development stage.

Talk 4: “PHP on Windows – The Undiscovered Country” by Hank Janssen

I really wasn’t sure what to expect from this talk. Hank spoke about the division of Microsoft he was from, the “Open Source Technology Center”. I was amazed to hear just how much money Microsoft were pouring into open source projects and improving the open source software in the windows platform. Hank explained how his divison of Microsoft have been busy rewriting, patching and optimizing PHP on the Windows platform in a bid to make it run faster than on Linux. I have to say that if it made economic sense to change to a windows server rather than buy two linux servers, I would! It would cost less each month after the inital cost of the windows install and would pruduce much less carbon.

Talk 5: “Flex and Air for PHP Programmers” by Mihai Corlan

Even though I’m not a big user of adobe products and their languages, I could see some immediate applications for Flex + Air. Mihai showed some code examples and showed several real world applications. I was especially interested in AMF and will be looking at this in detail.

Talk 6: “Security Centered Design – exploring the impact of human behavior” by Chris Shiflett

This was a really entertaining talk on how humans interact with web sites. He showed several example videos and images of the effects of “change blindness” which cemented a “best practice” that we’ve been using for a while. He explained the use of login seals to help prevent phishing attacks (Which I thought was an awesome idea and really want to know why my bank hasn’t got this facility!?). He gave some good examples of mislabeled/misleading option names in software and on websites. One in particular was Apples air port that has the option for a “closed network” which just stops broadcasting the SSID. Many people could mistake this for securing the network which would infact be adding strong encryption.

Conclusions:

An excellent conference and will be booking my ticket for php2010 as soon as they become available!

Leave a Comment :, , , , , , , , , , more...

Visit our friends!

A few highly recommended friends...