Archive for the ‘PHP’ Category

I’m currently about half-way finished with a project that allows a user to check their Myspace Mail inbox through WML and WAP (mobile internet standards). The user simply visits this service, dubbed mBridge at the moment, on their mobile phone. They will be prompted for their Myspace login information. mBridge will pass the credentials on to Myspace and retrieve the 10 most recent messages, strip any code, and display them for the user to read. For those of you interested in how this works, read on.

First, mBridge has a login page written in WML that includes a form allowing the user to type their email and password for Myspace. After the user submits this information, mBridge will take it and use it to connect to Myspace via sockets. A socket is opened to, headers for the homepage are written, and a token is retrieved. mBridge then opens a socket to and uses a POST request to send the user credentials. After a few redirects and setting of cookies, we have a valid session with Myspace. mBridge retrieves the mail center page and uses a regex to grab the dates on all of the messages, their subjects, and the message ids of all of them. Now we open a socket for each message found in the inbox and retrieve the content.

The subject lines are stripped of repetitive “RE:” flags and the messages are stripped of code that won’t display properly in a mobile browser. The user is sent a list of links to each of the messages for them to read. When they read a message, they have a link to respond to the message.

When a message is sent, we log back in, visit the mail center, read the message, simulate a click on the reply button, and insert the mobile users response in the field before clicking send.

Will Myspace try to stop this from happening? While it’s possible that they will change the login method, links, or method for sending message, we can always adapt to it. If your browser can login, so can a script. It is also possible that they will incorporate JavaScript into the service to set cookies that we would otherwise miss with a script, but we can always determine the behavior of the script and make it work no problem. It is possible for Myspace to start blocking our IP address, but we can go buy more. Myspace can force users to verify a CAPTCHA before logging in, but we will develop OCR that can answer them just as accurately as a human, or pass them on to the mobile user and allow them to answer them.

The only thing Myspace might do is try to shut us down through legal methods. I’m not aware of anything illegal about what mBridge does, though I’m not a lawyer. I’d hope that since Myspace does not have a service competing with this they won’t get their panties in a bunch that their customers don’t have to login to something they made to get their messages, but really, can they continue to expect people to put up with a lack of updates, no integration with mobile phones, and constant errors?


PHP is a great language for beginners and advanced programmers all alike (especially with v. 5). Like other scripting languages, it is compiled at run time, so there is no extra work on the developers part before running the code.

First off, where do you write PHP? Well, you can write PHP in any text editor. If you like to use a specific program to write HTML, you can use that same application to write PHP. You start a block of code with “<?php” and end it with “?>”. Pretty easy huh?  Here’s a sample that will output “Hello World!”:


echo “Hello World!”;


You can put this code in any HTML page and save it with a .php extension. Upload it to your web host (assuming they offer PHP) and access it just like you would any HTML file. You should see “Hello World!” in the place of the code.

All that’s happening here is a single statement. We are using the echo function to output data to the browser. Echo will output whatever you put after it, in this case, we used text. If you’re new to programming you should start getting used to calling text a string (because it’s a string of characters). That’s what we call text in programming, so getting that down to start is helpful.

PHP can be used for a variety of things. It can build images on the fly and output them, handle forms, insert and retrieve information from a database, talk to other applications, access other websites, send email, and much more!

Next we’ll go more into actually using the language!

PHP doesn’t have a built in proper case string function, though it does have upper and lower case functions. This isn’t too big of a problem though, since we can build one that acts just like we want anyways!

If you just have a short phrase or word that needs to have the very first letter capitalized, we’ve got a nice and easy job. Here’s the code:

$string = strtolower($string);
$string = substr_replace($string, strtoupper(substr($string, 0, 1)), 0, 1);

That might look a bit messy if you don’t like putting things together all at once, so here’s the jist: On the first line we make the whole string lowercase since we don’t know if there were capital letters where they shouldn’t be. The most inner part of the second line (substr) pulls the first character from our string. Moving outwards, the next function makes it uppercase, then finally, we replace the first letter of our string with the new uppercase letter.

Now what if we wanted every word in the phrase capitalized? Simple.

$words = explode(” “, $string);
for ($i=0; $i<count($words); $i++) {
$s = strtolower($words[$i]);
$s = substr_replace($s, strtoupper(substr($s, 0, 1)), 0, 1);
$result .= “$s “;
$string = trim($result);

Here we split the phrase up based on each space character and then did the same as we did above, only for each word. Then we built it back together word by word.

UPDATE:  I didn’t realize it, but the ucwords function in PHP will do just this.


I’ve proved this in the past, but I suppose it’s time I do it again. Libcurl is generally faster than file_get_contents by a considerable amount.

Many people argue that it’s not any significant amount. This point is used time and time again when comparing functions that don’t access anything except memory, but it doesn’t apply to accessing files over a network.

Having not looked over the source code of libcurl and PHP, I can’t tell you for sure why one is faster than the other, but I can give you proof and some rational thoughts on the matter.

First, libcurl allows you to customize your request a considerable degree. This allows it to lighten up it’s processing on trying to figure out what it’s connecting to and how it should handle everything. It’s opponent is a one-size-fits-all solution, meaning it fits, but not very well. It has to do more work to decide exactly what it should do with each query. There are also a number of headers that could be sent differently between the two that I don’t care to test or look at.

Anyways, here’s the proof you’ve been waiting for: Test

The test runs through a list of websites, connecting 10 times to each using libcurl and file_get_contents and compares the times. Some websites work faster with file_get_contents, and others work faster with libcurl. Occasionally the total will end up within 10 seconds, but I’ve never seen file_get_contents spend less time than libcurl.

As an additional note, even if you are connecting to a website where file_get_contents has been proven to be faster, you should still use libcurl due to it’s error handling abilities. If you’re really opposed to libcurl or can’t use it, use sockets. 🙂