Save login info in a cookie using CURL (PHP)

At work I was using a simple script that connected to a secure API, logged in and saved the result. This seems simple enough and is fast enough when calling it a few times, however one of the tasks I was then assigned was actually running this script for different values over 60,000 times! Each call (including the logging in) would take a little over a second, so calling it 60,000 times would take a while.

It was then decided to try and improve this by making the script only login once and save the session, so that subsequent calls would not have to login each time. This is one of the solutions we came up with ( because finding this online was not very easy :) )

The first step is the login, the agent variable is being set to trick the API into thinking that the request has come from a browser.

//Set the cookie file name
$cookiefile = tempnam("/tmp", "cookies");
$agent     = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
//Set the user name and password values
$USER      = 'user';
$PASS      = 'pass';
//The API url, to do the login
$url = "https://www.example.com/";

//Initialise CURL
$ch = curl_init();
//Set all the various options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 0); // set POST method
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//Set the cookie file you want to use
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_USERPWD, $USER.":".$PASS);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

//execute the CURL
$mRes = curl_exec($ch);

Remember not to close curl at this point. Now we look at the loop that will carry out the 60,000 calls to the API

while ($row = [get row value for the 60,000 entries])
{
    $url       = “https://www.example.com/API?check=$row”;
    //We do not have to initialise CURL again
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_POST, 0); // set POST method
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    //Remember to use the same cookiefile as above
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    //execute the CURL call
    $mRes = curl_exec($ch);
    //DEAL WITH THE RESULT FROM THE CURL CALL
}
//only close CURL once the work is finished.
curl_close($ch);

If you’d like to know more the various CURL options I have used above like ‘CURLOPT_SSL_VERIFYHOST’ etc, have a read through the curl_setopt page on php.net. You can also read more about CURL there as well.

LOLCODE!

I’m sure most people by now know about the whole Lolcat phenomena, however, for those still living under a rock:

A Lolcat, or LOLCAT, is an image combining a photograph, most frequently a cat, with a humorous and idiosyncratic caption in (often) broken English—a dialect which is known as “Kitty Pidgin”,[1] “lolspeak”, or Lolcat.

Lolcat or Kitty Pidgin has been around for a while now, and has been made popular by sites like I Can Has Cheezburger?…however in spite of its popularity, to find a programming language based on Kitty Pidgin still came as a real surprise to me.

Lolcode, created on 25 May 2007, is a programming language created by Adam Lindsay based on lolcat.
The language is still fairly young and at the moment has the following subset of keywords

Writing a program in lolcode seems surprisingly easy for people who understand lolcat, the main thing to remember is that HAI is used at the start of a program and KTHXBYE ends the program.
The default hello world program is as simple as

HAI
CAN HAS STDIO?
VISIBLE "HAI WORLD!"
KTHXBYE

The language also contains error handling using AWSUM THX on success and O NOES which is the exception block!
The reason you probably haven’t yet seen a whole slew of lolcode applications yet, is that the language is still being defined with respect to operator priorities and correct syntax. Some compilers, however, already exist, like this one in python.

For the time being, this is another addition to my ‘must try at least once’ list…KTHXBYE!

  • Google Doctype: Check it out, its an online encyclopedia written by devs, covering areas like web security, JavaScript DOM manipulation, CSS tips and tricks etc, and also includes a bunch of test cases for checking cross-browser compatibility.

Online vi editor - for those that love their vi

I’ve been using vi and vim a lot more since I started work at my present company, and must say that while initially I thought that the only people who would enjoy using it were sadists, it has started to grow on me.
Recently, I came across a link to an online version of vi, known as jsvi, which as one can probably guess is written in javascript. It’s a pretty faithful imitation of the command line editor, with the added bonus of a spell checker. If you have some spare time, you should really check it out.

Source Lifehacker

SEOmoz - The Web Developer’s SEO Cheat Sheet

SEOmoz, is a blog I have started reading recently to get my regular fix of what is happening in the SEO world. If you haven’t heard of them, I suggest you check them out as they write some really great articles with a lot of useful information. (SEOmoz, I’ll email you the details on where to send the cheque :) )

If you’re a web developer, you should really check out their Web Developer’s SEO Cheat Sheet, which has some really great info on things developers should keep in mind when creating web sites.
The sheet covers various areas including

  • Important SEO HTML Tags
  • Search Engine Indexing Limits
  • Recommended Title Tag Syntax
  • Common Canonical Homepage Issue
  • 301 Redirect for Apache - Probably one of the most important things to know (at least in my limited experience)
  • Important Search Engine Robots
  • Robots Meta Tag
  • Common Robot Traps - Something to definitely be mindful of
  • Robots.txt Syntax
  • Sitemap Syntax

While there, also checkout

Where are the variables in CSS?

For something used by designers and developers the world over, CSS still misses one of the most fundamental aspects of programming and that is the ability to create and set variables. If someone wanted to the look of a page by only changing the colour scheme, it would require changes in a few places (depending on how the stylesheet was set up).
Recently, however, I came across a proposal set forward by Daniel Glazman and David Hyatt for CSS to support variables. Have a read through, it does make for some interesting reading. They propose defining variables using the at-rule (@variables) in a block.
It would look something like so (example taken from the proposal whose link is provided above)

@variables {
  CorporateLogoBGColor: #fe8d12;
}

div.logoContainer {
  background-color: var(CorporateLogoBGColor);
}

Now the real question is, how long would a proposal like this take to get adopted. I personally think it is a great idea and would make CSS a whole lot more powerful, and easier to manage.

At the moment if I want to use variables with CSS, I tend to lean towards using PHP to generate it. Its not too bad (although is a little slower), have a look at David Walsh’s blog to get more of an idea as to how this is done.

Forgot Your XP admin Pass? Reset It For Free!

A friend (who shall remain nameless) managed to log himself out of his computer because he forgot his Windows XP admin password! He’d been overseas for a couple of months, came back and realised he could not remember his admin password anymore. While looking online for something that could help him (without dishing out a few hundred on recovery tools and without reformatting his drive). I came across this ‘great’ article, which talks about using a little known access hole in XP which can be exploited during the XP repair process and all you need is your windows XP CD!
Just remember that this tip is meant to help people with genuine issues only.

Getting Rid Of The Default Gravatar

I’ve been playing around with Wordpress 2.5 and a new theme on the blog over the last few days, and must say that I am quite impressed with all the new bells and whistles.
One of the newer inbuilt features, Gravatar has been especially interesting. The Gravatar site has this to say when defining a gravatar

A gravatar, or globally recognized avatar, is quite simply an avatar image that follows you from weblog to weblog appearing beside your name when you comment on gravatar enabled sites. Avatars help identify your posts on web forums, so why not on weblogs?

Now Gravatars have actually been around for a while, but they have been gaining in popularity expecially since Automattic recently acquired gravatar and WordPress 2.5 released with inbuilt support.
Adding Gravatars to your wordpress theme is fairly easy, all it requires is calling the new get_avatar() function in your comment template php file

<?php
   echo get_avatar( $comment, $size = '96', $default = '<path_to_url>' );
 ?>

where $comment is the authors id and is a required field, $size controls the size of the avatar and $default is the path to your default image.
Now, whenever someone makes a comment on your site, if they have a predefined gravatar, it will show up otherwise your default image will appear.
Initially my default avatar looked something like this
default gravatar
However while reading the gravatar blog recently, I came across this article, which talks about gravatar’s new support for Identicons, MonsterID, and Wavatars.
In a nut shell what these projects try to do is uniquely identify users, using information like IP addresses so that they can generate random images which are then unique to that user, so that whenever they leave a comment on your blog, instead of being presented with the default image they are also treated as part of the ‘family’ :)

This led me to play with the $default value in the get_avatars() function and I came across an undocumented feature, if I change the value of $default, my gravatar changes as well!
If you try any of the following,

echo get_avatar( $comment, $size = '96', $default = 'wavatar' );
OR
echo get_avatar( $comment, $size = '96', $default = 'monsterid' );
OR
echo get_avatar( $comment, $size = '96', $default = 'identicon' );

you can automatically see the changes to the default gravatars, which is pretty cool (at least in my opinion)
So now I can get unique avatars for each commenter (who are not already using gravatar)



NOTE: At the moment for some reason only wavatar seems to be working, the others are throwing ‘504 Gateway Time-out’ errors The others seem to be back up now.

Windows’ ‘No To All’ Option

This is a pretty useful trick I cam across today on Lifehacker. If you’re using windows and if you’ve ever tried copying a lot of files from one place to another, chances are you have come across the confirm file replace dialog which generally gives you three options, ‘Yes‘, ‘No‘ and ‘Yes to All‘, however there was never a ‘No to All’ option. In the past I have sat there clicking No forever, however there is a hidden ‘No to All‘ option. If you keep the Shift key pressed when you click on ‘No‘, windows treats it as a ‘No to All‘!
Trust windows to hide something as useful as this.