Friday, August 17, 2012

Session handling in PHP

The prime reason why server-side scripting languages are popular is that they support sessions. A session can be used to remember user details across pages. Suppose you sign in into your Facebook account and visit the settings page. To do this you don't have to sign in again. Facebook remembers that you have already signed in using a session cookie. This is possible using a feature called sessions. In this article I will teach you about handling sessions in PHP.

The first thing that is to be learned in Session handling is how to start a session. A session is usually started when a user logs in to a website. The PHP function session_start() is used to start a session in PHP. The same function can be used to continue a session (if it already exists). Usually, after starting a session, a few session variables are set (like id). This is done using the $_SESSION[] superglobal variable. $_SESSION[] is an associative array. To set a session variable, just add a key-value pair to the array. So when the user visits another page we can check whether or not he/she is logged in by checking whether or not a particular session variable is set.

// starts a new session session_start(); // set a session variable called id $_SESSION["id"] = $_POST["id"];

This can be the code in a login page. Suppose that, after login, a user visits another page in the same website. We obviously don't want him to login again. We need to first check if the user is already logged in. If yes, we let him view the page. Else, we redirect him to a login page (possibly containing a login form).

// continue an existing session if it exists session_start(); // check if the user is logged in if (!isset($_SESSION["id"]))     header("Location: login.html"); // redirect // the content of the page follows...

So, that was starting a session and checking whether a session is active. Now, let's see how one can end a session. The most common situation in which we would want to end a session is when the user logs out. To end a session, just use the session_destroy() function.

session_start(); // unset all session variables $_SESSION = array(); if (isset($_SESSION["id"])) // if the user is logged in     session_destroy(); // ..

You can learn more about the above mentioned functions at php.net.

Saturday, July 21, 2012

CSV file handling in PHP

CSV stands for "comma seperated values". CSV files look like excel spredsheets and are quite useful in storing data and also it is quite simple to parse a CSV file. By definition, a CSV file contains rows of data seperated by commas and the end of a row is marked by a newline character (this means that 1 line contains 1 row and values in a row are seperated by commas). You can think of a CSV file as a table of values. For example, a CSV file might look like this:

Mark, 25, Apple
Jack, 29, Google
Chet, 31, Facebook

The above CSV file stores information about some people like their names, ages and the companies they work in. This CSV file can be though of as a table with 3 rows and 3 columns. Most websites use CSV files to make some data programmatically available to the public. (For example, Yahoo! Finance. Yahoo! Finance makes stock available to other wesites in a CSV file) You have to open the CSV file from your PHP code and parse the file to read the data or use it on your own website.

Thankfully, PHP provides inbuilt functions to parse CSV files. The first step, of course, is opening the file using fopen() for reading. Assuming that the name of the CSV file is users.csv you can write

$handle = fopen("users.csv", "r");

Next, to get the a line (i.e a row) from a CSV file we use the function fgetcsv().

$data = array();
$data = fgetcsv($handle);

The above snippet of code reads the next line (row) from the csv file referred to by $handle and strores the values in an array called $data. Thus the return type of this function is an array. The function can take more parameters but they are optional. You can read more about fgetcsv() at php.net. The rest of the parameters are hardly of any use in simple, regular applications. To make sense of the data we retrieve using a CSV file we need to know the column order and the number of columns. This means, if you are parsing a CSV file that someone else has given you, you must take some time and figure out what each column denotes and the number of columns. After getting a row of the CSV file into an array, we can use the values of cells by getting the value at a particular index of the array. For example, $data[0] refers to the value of the first cell in the row stored in the array $data.

For a CSV file having multiple rows we can put the above lines of code inside a while loop. For example, we can do this:

$data = array();

// Open the file....

while (1)
{
    $data = fgetcsv($handle);
    if (data == NULL) // EOF has been reached
        break;

    // Print the data....
}

Writing data to a CSV file (i.e. generating a CSV file) is even easier. The first step is creating an array (representing a row of data) and putting some data into it. Then open file to write to using fopen() and use the function fputcsv to write a row of data to a CSV file.

$row = array();

// Put some data into the array.

$handle = fopen("mycsv.csv", "w"); // Open the file
fpucsv ($handle, $row);

More about fputcsv() at php.net

Saturday, July 14, 2012

The two CSS features to make your website beautiful

The whole point of CSS is to make your website beautiful in contrast to the older websites we had before the introduction of CSS. But after the introduction of CSS3 we had a few killer features that contribute more to making your website beautiful than any other features.

These two features (strictly speaking, properties!) are box-shadow and border radius. Let's see how they work and the required syntax for specifying these properties:

  • box-shadow

    The box-shadow property allows designers to easily implement drop shadows around elements. It is supported by all the new browsers. Here's the syntax:

    box-shadow: h-shadow v-shadow blur spread color inset;

    h-shadow specifies position of horizontal shadow and v-shadow does the same for vertical shadow. blur specifies the blur distance, spread the size of the shadow and color the color of the shadow. inset is used to get an inner shadow instead of an outer one which is the default setting. Except h-shadow and v-shadow all other properties are optional. Head over to w3schools.com and try this property out.

  • border-radius

    The border-radius property has been around for a long time but has become a w3c specification recently with the introduction of CSS3. The syntax for this property is really simple.

    border-radius: radius;

    radius is the border radius you want for your element. Usually is around 5px is good enough for good looks.

That's it! These two properties, when used creatively can turn an ugly website into a cool, professional looking one.

Wednesday, July 11, 2012

Hello World

This is the sign of a true geek; titling your first post as "Hello World"! The exact same thing you do when you learn a new programming language. You write the customary Hello World program! Since this is the first post ever at GeekOverlord, let me write a few lines about what GeekOverlord is and especially about what you can find here.

For starters, I don't adhere to any specific topic here at GeekOverlord. The posts will discuss a wide range of topics which might or might not (that would be rare!) relate to programming and not all posts are all that geeky. So even baginners can find something of interest here..something to learn. Anyway, I am not an experienced programming even. At the time of writing this, I haven't even started college.

What kind of things am I gonna write about? Well, many things actually. Starting from simple programming to security related concepts. I intend make the posts enjoyable and interesting to read at the same time explaining a difficult concept. I might also share my views on the current happenings in the tech world and will definitely post links to some of the best programming resources on the internet.