Tuesday, June 28, 2011

Logging in to HTTPS websites using PHP cURL

To log in to a HTTPS website using PHP cURL you need to do the following:

enable cURL by uncommenting the line extension=php_curl.dll in your php.ini file.

Set up cURL to either accept all certificates or add the needed certificate authority to cURLs CA list (check out http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/)

Then you need to load the page to get the session cookie:
// Create temp file to store cookies
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

// URL to login page
$url = "https://www.securesiteexample.com";

// Get Login page and its cookies and save cookies in the temp file
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // Stores cookies in the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);


Now you have the cookie, you can POST login values (check the source of the login page to check if you need any other fields too)
$fields = array(
'username' => 'yourusername',
'password' => 'yourpassword',
);
$fields_string = '';
foreach($fields as $key=>$value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');

// Post login form and follow redirects
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Tells cURL to follow redirects
$output = curl_exec($ch);


Now you should be able to access any pages within the password-restricted area by just including the cookies for each call:

$url = "https://www.securesiteexample.com/loggedinpage.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);


Found from http://coderscult.com/php/php-curl/2008/05/20/php-curl-cookies-example/, http://davidwalsh.name/execute-http-post-php-curl, http://stackoverflow.com/questions/3519939/make-curl-follow-redirects, and http://www.php.net/manual/en/function.curl-setopt.php.
Also see http://php.net/manual/en/book.curl.php.

Tuesday, June 21, 2011

Mount TonidoPlug as Local Drive

So you want to access your TonidoPlug USB harddrive as a native drive in Windows? Here is how:

Follow http://www.codelathe.com/blog/index.php/2010/03/17/mounting-tonido-shares-as-local-drives/.

To get NetDrive, go to http://www.netdrive.net/download.html for a free for non-commercial home use version. otherwise they'll try to sell it to you. :)

Instead of using the URL or Shared Location from your shares list on the TonidoPlug in the Site IP or URL field in NetDrive, just enter the base url. E.g. http://<username>.tonidoid.com/ .

This should open your root directory of the TonidoPlug, navigate to the USB drive. You can find it under T:\Drive_Root\media (or whatever drive letter you chose). Then you can make a shortcut directly there if you want.

Now you should no longer get the
Mounting drive T: failed
Login failed. Please check the internet connection.

error.

Also check out http://www.tonido.com/forum/viewtopic.php?f=9&t=1410 if you have trouble with the username.

Thursday, May 26, 2011

SQLyog hide Query Builder and Schema Designer

When you open a new connection in SQLyog, you by default automatically get 3 tabs, the Query tab (to enter queries), Query Builder, and Schema Designer.
To prevent the Query Builder and Schema Designer tabs from opening every time you can disable them in the sqlyog.ini file.

The file can be found under C:\Users\<your username>\AppData\Roaming\SQLyog or similar in other Windows OSes. Find the lines called DefaultQueryBuilder and DefaultSchemaDesigner and change their values from 1 to 0.

SQLyog should now only open the Query tab.

(The tabs can easily be added by pressing Ctrl + K or Ctrl + Alt + D respectively or from the Powertools menu).

Wednesday, May 18, 2011

Thunderbird Ctrl+F open Quick Filter not Find in message

In Thunderbird, if you have selected a message so that it's shown below your message list (not double click to open in separate tab), pressing Ctrl + F won't give you a find box for the message. Instead, it will open the Quick Filter bar which lets you search for messages.

To get a search bar where you can search within the message text press Ctrl + F twice(!) OR press Ctrl + G.

Note: shortcut to hide the bars is escape, once for each bar.

Solution source: http://groups.google.com/group/mozilla.feedback.thunderbird/browse_thread/thread/877e7351d9bd51fd
And
http://kb.mozillazine.org/Make_Thunderbird_3.1_look_like_Thunderbird_2

Sunday, May 1, 2011

Ruby on Rails missing sqlite3.dll

I was following the official guide at http://guides.rubyonrails.org/getting_started.html on how to set up Ruby on Rails. At step 3.4 Configuring a Database I got the following error message:
"The program can't start because sqlite3.dll is missing from your computer. Try reinstalling the program to fix this problem."

After quite a lot of googling I found that the problem was that the sqlite3 gem
(installed with the bundle install in step 3.3 I believe) didn't include windows DLL. This is probably because "most Ruby developers use Linux".

The solution is to download the DLL from the official SQLite website http://www.sqlite.org/download.html. You want the "This ZIP archive contains a DLL for the SQLite library" file. At the time of writing it is the following file: http://www.sqlite.org/sqlite-dll-win32-x86-3070600.zip
Put the files from the ZIP file into your ruby/bin directory. Close and open a new console window and continue with the Ruby on Rails guide.

Thursday, April 14, 2011

Eclipse opens .class instead of .java

One neat feature of Eclipse (and other IDEs) is that you can hold CTRL and click on a function or variable to go to its definition.

A problem which may occur in Eclipse is that it opens the .class file (the compiled file) instead of opening the .java file when you do this. This is probably caused by the order of the entries in the .classpath file.

Go to your project directory and open the .classpath file. Move all entires of type classpathentry with attribute kind="src" to the top of the file (or at least above any entries with kind "output").

Restart Eclipse and the problem should be fixed.

Sunday, March 13, 2011

Firefox mouse pad move back in history

In firefox holding down shift and then swiping left, with two fingers, on your mouse pad will move you back in browsing history. This can be very annoying, especially if you just finished filling in a form and the hold shift and reach to the left the captialize the first letter of you name for signing (e.g. the H in Herikstad).

To disable this back action in firefox do the following:
Enter about:config into the url line.
Accept the warning.
Enter mousewheel.withshiftkey.action into the filter at the top of the page. You should get one line with the same name and the value set to 2.
Right click the line and change the value to 0.

Other values you can set for this field is:
* 0 - Scroll document by a number of lines (given by the numlines property)
* 1 - Scroll document by one page
* 2 - Move back/forward in history
* 3 - Make text larger/smaller
* 4 - Scroll document by a number of pixels (given by the numlines property)
Thanks to http://support.mozilla.com/bn-BD/questions/701079 and http://kb.mozillazine.org/About:config_entries#Mousewheel. for those.