Wednesday, March 26, 2014

Login Form - Without Styles

Ok, now we will make a simple login form, which submits the username and password to a perl script (which we will cover later). This tutorial is more about HTML than Perl, but is very important.


I really had a hard time in the past myself, figuring out how to use Perl & HTML for my website, I almost gave up on Perl, and almost decided to use PHP. So pay attention please :)


A thing to note is this, that even though we can make a perl script, and put HTML into it, we can't do the opposite i.e, make a HTML file and put Perl in it. This is a Frequently Asked Question(FAQ) for Web Perl. 

Yet, for our login form, we will just make a HTML file, and have it send data to a Perl Script. There are various reasons for doing so instead of just making a big Perl file/script, or making two Perl script.

1. If you put all the code in one Perl file, it is unsafe, as the login form is publicly available, people should not know how there login credentials are actually being authenticated/checked. That is a big no no! The data should just dissappear for checking, like a high class hotel's butler, who goes inside a special room to check whether you are invited to the party or not. 

2. Also, instead of making two Perl files, one frontend (which sends the credentials/data) and second Backend (which checks if your username/pass combination matches the records) using one HTML and one Perl script is better. As HTML is easier to maintain. 

So again, in this tutorial we will only be making a frontend of the login system that is the 'form' only. Things to note about it are:

- It sends data, but we have not yet created a Perl script in the back to receive and process that data.
- I have also left out the CSS styling for the time being, so the form will appear without styles on the top-left of your browser.


Now here is the explanation for the above code. If you are familiar with HTML, read from Line 9. 


Line 1: The standard HTML 5 starting line which announces that this is a HTML5 document. 



Line 2: It basically says everything after this is html code. 


Line 3: It basically says everything after this is the head of the html document.

Line 4: Just optional to say that the encoding of this document is in UTF-8 characters. If you skip this line, everything should work normally, so don't worry.

Line 5: In html, this is called the ending tag (notice the / before head). Which basically says that the head of this html document, which should have started earlier, has now ended, so anything after this is not head.  The line 4 is the only line inside the head tag.

This is basic HTML knowledge, in case you have any confusion, go to

http://www.w3schools.com/html/DEFAULT.asp 

Line 6:  The body tag starts here, and everything after it is inside of the body of this HTML document.

Line 7: The div tag starts here.

Line 8: Now this is the most important part of this tutorial. We have started the form tag here.

we have defined two things here about our form:
- the method of sending it to the Perl Script is POST ( and not GET. For security reasons).
- secondly, the data is being sent to a perl file called 'login.pl' located in a folder called cgi-bin on the same server as our html file. See the picture below:



 Line 9: In this line, we first write the text "Username" inside a paragraph tag (the two p) and after that, is the white box in which to enter the text. Again, we have given it a few important properties:

- The type of the input is text
- The 'name="username" basically says that what ever the value is sent, it should be marked as username, so when the Perl script would receive the data from this HTML form, it would be able to distinguish that among the values it received, what value is actually a username or what value is a password.
-The value="" basically resets the form each time. As you see there is no value between the two inverted commas, it is empty. From what I have heard, it also serves as a security measure against data leakage.
-maxlength="40" basically says that only 40 characters can be entered and no more. This is a basic security measure as well.
- The 'required' makes it mandatory for the user of the form to enter something before submitting the form.
- The 'autofocus' is explained here, if not obvious: http://www.w3schools.com/tags/att_input_autofocus.asp

Line 10: This is password text and the input field. Works similarly to the Line 10, so read above carefully.

Line 11: This is the Submit button. Here only one thing is new(compared to line 10 & 11), and is worth noting about. The value is "Login". What this does is different from the username/password input feilds. In case of a submit button, it is the text that appears on the submit button. Try changing it to something like "THIS IS THE BIG BAD LOGIN BUTTON!"

Line 12: The form ends here.

Line 13: The div containing the form ends here.

Line 14: The body of the document, containing the div, ends here.

Line 15: The html document ends here.

The last thing to do now, is to copy the above code, into one of your favorite text editor like Notepad++ and save the file as test.html. Then right click on it, and open it in a browser like Google Chrome/Mozilla Firefox.

Thats all for now, next time.. we will make the perl script that receives the input from this form. For now try out your newly created form, and try styling it yourself, using CSS.

Friday, February 28, 2014

Hello World!

Ok, now we are going to make the traditional 'Hello World!' script.

For those unfamiliar to this programming tradition, it is basically a script, that upon execution prints out the text "Hello World!" on your screen.


Now, here is the explanation for the above code:

Line 1:  
#!<some path> ----> this is required for your script to work, it tells where the perl files are located.  #!/usr/bin/perl is saying that perl files are located in a local folder, which is located at /user/bin/perl  

The above is correct for a Linux system, where Perl is installed in its default location. 

While -T is for taint mode (a security feature) and w is for warnings.

Line 2: use strict is a security feature, and is strongly recommended!

Line 4: This line is very very important, and you would need to use it in all pages generally. It says, that after this whatever is accompanied with a print command, is shown on the monitor.

Line 5: This is the first print command, that would be displayed on screen. Try typing anything instead of the 'Hello World!' text. The \n is for newline after the text.. similar to what we use in C++

The last thing to do now, is to copy the above code, into one of your favorite text editor like Notepad++ and save the file as test.pl

Then put it into the cgi-bin folder, on your website and set its permissions as 600 or 700 or 705. To test it, set its permission as 700, and then go to:  www.yourwebsite.com/cgi-bin/test.pl

You should see a Hello World! written on the top-left side.

Note: You might need to add the following line for shared hosting accounts, like of Godaddy, since they have cpanel installed, make this your Line No.2 in that case:

use cPanelUserConfig; #needed for shared hosting having cpanel like Godaddy


Basic Notes about Perl/CGI

Before we jump to our first "Hello World!" script. Lets get some basics up and ready.

Things You Should Know About Perl


- It is used by major websites like the BBC.co.uk, Craiglist, DuckDuckGo, Amazon, ImDB, LiveJournal and projects like the Cpanel, Slashdot, Bugzilla, etc. So yeah, its not as obscure as one would initially think.

- Perl is often said to be difficult, but that might be because it is a "Jack of all Trades". It requires a bit more patient approach to learn. So the rule to remember, when learning Perl is "Stay in the Ice".

- You can find perl scripts to accomplish almost everything on
http://www.cpan.org
https://www.metacpan.org

But here is the catch: Explanations are a bit hard to follow. And there is no attached forums, to discuss the modules. When you get stuck, try to Google a solution or ask a question on:

http://www.Perlmonks.org
https://www.stackoverflow.com


Things You Should Know about CGI


- CGI is old, slow and inefficient for most of the stuff. Around 10x times slower than its modern avatar FCGI. 


-Most of the time people will tell you how CGI is outdated/slow/crap/etc, and would tell you about one of the Web Frameworks of Perl, like Mojolicious, Dancer, Catalyst etc. Well listen to them, if you want to take the easy road, these frameworks really are a 'job well done' and makes a lot of things easy for you. 

But if you want to take the hard way(slightly), its better to learn the very roots on which these frameworks are built.

- Also, on various Shared Hosting (like Godaddy.com) you would find a folder called 'cgi-bin'. You would have to put all of your .cgi/.pl/.fpl scripts into this folder in order to run them. With Permissions 600 or 700 or 705 in order to run them.