Your Ad Here

Monday, June 4, 2012

Geany - A Good Web Development Editor (IDE)


Geany - A Good Web Development Editor (IDE)

Geany Version 0.16
Last year I finally made a switch to GNU/Linux as my operating system and had been in search of a good editor for my web development. In Windows I used Dreamweaver for the purpose and was quite happy with it. It could can easily highlight PHP, HTML, JavaScript and other codes of some other languages related to web. It can also do some Auto-Completion – a feature I think is a must-have. I also liked the fact that it (version MX) was reasonably light on system resources.
When I switched to Linux, I tried Bluefish, Komodo Edit, Netbeans IDE. Bluefish (ver 1.0.7) had  a  bad syntax highlighting feature besides other things I found to be quite irritable (working with PHP files, at least). Komodo Edit and Netbeans stood out especially Komodo Edit, in terms of functionality, usability and other small-small things. But still these were not what I was looking for. I wanted something good in terms of functionality but at the same time light on resources (Komodo Edit took about 15 seconds to load). I wanted something that had a good balance between features and memory footprint.
Geany is small (source package is about 2.2 MB), light on resources and at the same time loaded with functionality and ease of use for the size. Geany does not have all the features of a full-fledged IDE (like for example Komodo Edit) but it has got many of the important ons. And it's not just PHP and web development that you can use it for, it has support for a good number of languages (programming, scripting etc). But as I've only used it for web development, I'm strictly talking in that context throughout.
Here are some of the features I find very helpful:
  1. AutoComplete: For functions(inbuilt as well as user-defined), variables, classes etc. Inbuilt functions are shown with their argument list-very useful. And there's but only a few functions I couldn't find in its database. Just type in a few characters and it shows a nice drop-down list of suggestions, select one and it shows the function's parameters list and their types. TIP: By default you need to type in at least three characters for it to give you suggestions for AutoComplete(ion) but it can be changes from Edit-> Preferences-> Edit(Side-Tab)-> Completions(Tab). I personally have set it to be 1.
    Geany: Auto-Complete Feature
  2. Compile and Make: You can't “make” a PHP script but compile is damn good a feature. Now forget refreshing the browser. NOTE: Can only check syntax errors.
    Geany: Compile Feature
  3. Code-folding: Another useful feature especially for lengthy scripts.
    Geany: Code Folding
  4. Auto-Closing: Can close (X)HTML tags, parenthesis, curly braces, quotes etc. for you. Check Edit->Preferences->Edit(Side-Tab)->Completions(Tab) for settings.
  5. Sidebar: These are many tabs in the sidebar but one of them particularly stands ot. The one which displays “Symbols” in the document-variables, functions, classes, objects, constants tec. This one reminds me of the commercial IDEs such as Visual Studio. Just click on something to find its declaration.
    Geany: Sidebar
  6. Sessions: Just like Firefox can restore sessions of open tabs(websites), Geany can restore session of files. If I'm working on, say, ten files and I decide to go take a bath. I can close and shutdown my PC because Geany will open all the files for me the next time I run it. Great!
  7. Pre-defined Comments: Whether you want to insert copyright note or license information or just function information. Just right click and and got to “Insert Comments” menu, you have them just a click away! There also exist a  feature for inserting current date and time, for that go to “Insert Date” from the right -click menu. Useful!
    Geany: Pre-Defined Comments
  8. Windows Version: Don't want to switch to Linux yet! No problem! Just download the windows version (needs GTK libraries installed)
  9. In spite so many useful features there is one more thing I expect Geany to do – better support for HTML files. Look, PHP and HTML go hand-in-hand and I'd have loved to have HTML preview feature like Dreamweaver has. We often embed PHP in HTML pages and Dreamweaver works perfectly well for these kind of pages. This is where I miss my old Dreamweaver the most.  I'm not saying this is Geany's short-coming as it's designed not just for web development, but when you do web development you'll  feel it's missing.
Some might wonder why I'm always mentioning Dreamweaver. This is because unlike some of the editors I've used, it isn't just restricted to HTML pages-it can handle dynamic content as well. Web development isn't just HTML or PHP or ASP, it's a mix which Dreamweaver handles quite well. It's a great HTML editor as well as it's a quite-great PHP editor, this is what I love about it.

PHP Sessions


PHP Session Variables

When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.
A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.
Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.

Starting a PHP Session

Before you can store user information in your PHP session, you must first start up the session.
Note: The session_start() function must appear BEFORE the <html> tag:

<?php session_start(); ?>

<html>
<body>

</body>
</html>

The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.

Storing a Session Variable

The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:

<?php
session_start();
// store session data
$_SESSION['views']=1;
?>

<html>
<body>

<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>

</body>
</html>

Output:

Pageviews=1

In the example below, we create a simple page-views counter. The isset() function checks if the "views" variable has already been set. If "views" has been set, we can increment our counter. If "views" doesn't exist, we create a "views" variable, and set it to 1:

<?php
session_start();

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>


Destroying a Session

If you wish to delete some session data, you can use the unset() or the session_destroy() function.
The unset() function is used to free the specified session variable:

<?php
unset($_SESSION['views']);
?>

You can also completely destroy the session by calling the session_destroy() function:

<?php
session_destroy();
?>


Note: session_destroy() will reset your session and you will lose all your stored session data.


PHP $_GET Variable & $_POST Function


In PHP, the predefined $_GET variable is used to collect values in a form with method="get".

The $_GET Variable

The predefined $_GET variable is used to collect values in a form with method="get"
Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.

Example

<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

When the user clicks the "Submit" button, the URL sent to the server could look something like this:

http://www.w3schools.com/welcome.php?fname=Peter&age=37

The "welcome.php" file can now use the $_GET variable to collect form data (the names of the form fields will automatically be the keys in the $_GET array):

Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!


When to use method="get"?

When using method="get" in HTML forms, all variable names and values are displayed in the URL.
Note: This method should not be used when sending passwords or other sensitive information!
However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
Note: The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.



The $_POST Variable


In PHP, the predefined  $_POST variable is used to collect values in a form with method="post".

The predefined $_POST variable is used to collect values from a form sent with method="post".
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).

Example

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

When the user clicks the "Submit" button, the URL will look like this:

http://www.w3schools.com/welcome.php


The "welcome.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array):

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.


When to use method="post"?

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

The PHP $_REQUEST Variable

The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.

Example

Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.

PHP Forms and User Input


The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.

PHP Form Handling

The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.

Example

The example below contains an HTML form with two input fields and a submit button:

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>

When a user fills out the form above and clicks on the submit button, the form data is sent to a PHP file, called "welcome.php":
"welcome.php" looks like this:

<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>

Output could be something like this:

Welcome John!
You are 28 years old.

The PHP $_GET and $_POST variables will be explained in the next chapters.

Form Validation

User input should be validated on the browser whenever possible (by client scripts). Browser validation is faster and reduces the server load.
You should consider server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.


Sunday, June 3, 2012

PHP Installation


What do you Need?

If your server supports PHP you don't need to do anything.
Just create some .php files in your web directory, and the server will parse them for you. Because it is free, most web hosts offer PHP support.
However, if your server does not support PHP, you must install PHP.
Here is a link to a good tutorial from PHP.net on how to install PHP5

Download PHP
Download PHP for free here: http://www.php.net/downloads.php

Download MySQL Database

Download MySQL for free here: http://www.mysql.com/downloads/

Download Apache Server

Download Apache for free here: http://httpd.apache.org/download.cgi

PHP Tutorial


PHP is a powerful tool for making dynamic and interactive Web pages.
PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.
In our PHP tutorial you will learn about PHP, and how to execute scripts on your server.

PHP Introduction
      PHP is a server-side scripting language.

      What You Should Already Know

      Before you continue you should have a basic understanding of the following:
      • HTML/XHTML
      • JavaScript

      What is PHP?

      • PHP stands for PHP: Hypertext Preprocessor
      • PHP is a server-side scripting language, like ASP
      • PHP scripts are executed on the server
      • PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
      • PHP is an open source software
      • PHP is free to download and use

      What is a PHP File?

      • PHP files can contain text, HTML tags and scripts
      • PHP files are returned to the browser as plain HTML 
      • PHP files have a file extension of ".php", ".php3", or ".phtml"

      What is MySQL?

        • MySQL is a database server
        • MySQL is ideal for both small and large applications
        • MySQL supports standard SQL
        • MySQL compiles on a number of platforms
        • MySQL is free to download and use

      PHP + MySQL

        • PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

      Why PHP?

        • PHP runs on different platforms (Windows, Linux, Unix, etc.)
        • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
        • PHP is FREE to download from the official PHP resource: www.php.net
        • PHP is easy to learn and runs efficiently on the server side

      Where to Start?

      To get access to a web server with PHP support, you can:
        • Install Apache (or IIS) on your own server, install PHP, and MySQL
        • Or find a web hosting plan with PHP and MySQL support



HTML Forms and Input HTML Forms and Input HTML Forms and Input


HTML Forms

HTML forms are used to pass data to a server.
A form can contain input elements like text fields, checkboxes, radio-buttons, submit buttons and more. A form can also contain select lists, textarea, fieldset, legend, and label elements.
The <form> tag is used to create an HTML form:

<form>
.
input elements
.
</form>


HTML Forms - The Input Element

The most important form element is the input element.
The input element is used to select user information.
An input element can vary in many ways, depending on the type attribute. An input element can be of type text field, checkbox, password, radio button, submit button, and more.
The most used input types are described below.

Text Fields

<input type="text" /> defines a one-line input field that a user can enter text into:

<form>
First name: <input type="text" name="firstname" /><br />
Last name: <input type="text" name="lastname" />
</form>

How the HTML code above looks in a browser:
First name: 
Last name: 
Note: The form itself is not visible. Also note that the default width of a text field is 20 characters. 

Password Field

<input type="password" /> defines a password field:

<form>
Password: <input type="password" name="pwd" />
</form>

How the HTML code above looks in a browser:
Password: 
Note: The characters in a password field are masked (shown as asterisks or circles). 

Radio Buttons

<input type="radio" /> defines a radio button. Radio buttons let a user select ONLY ONE of a limited number of choices:

<form>
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form>

How the HTML code above looks in a browser:
 Male
 Female

Checkboxes

<input type="checkbox" /> defines a checkbox. Checkboxes let a user select ONE or MORE options of a limited number of choices.

<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>

How the HTML code above looks in a browser:
 I have a bike
 I have a car

Submit Button

<input type="submit" /> defines a submit button.
A submit button is used to send form data to a server. The data is sent to the page specified in the form's action attribute. The file defined in the action attribute usually does something with the received input:

<form name="input" action="html_form_action.asp" method="get">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>

How the HTML code above looks in a browser:
Username:  
If you type some characters in the text field above, and click the "Submit" button, the browser will send your input to a page called "html_form_action.asp". The page will show you the received input.