Taking tutorial submissions! Please email them to dave[AT]icemelon[DOT]com for review. Thanks!

Latest Tutorials
IceMelon IM: Add IM Functionality to phpBB [Misc] Want to add site-wide member IM to your phpBB community? Sure you do. Simple step-by-step instructions inside.

IceMelon IM: Add IM Functionality to vBulletin [Misc] Want to add site-wide member IM to your vBulletin community? Simple step-by-step instructions inside.

Crawl Your Site for PageRanks [PHP] So you know your site has a PageRank of 8. What about the PRs of all the inner pages? This tutorial will teach how you to automate the process of grabbing all these PRs.

Create CAPTCHA images [PHP] Worried about bots? Then create CAPTCHA images to prevent bots from taking advantage of your site.

Convert SHN and FLAC files to MP3 [Misc] Low on disk space and have an insensitive ear? Here are some steps to converting SHN and FLAC files to MP3 format.

[View All]

Become a sponsor for $15/month. Link is sitewide - PR5 homepage, 20+ PR4 pages, 90+ PR3 pages. Email dave[AT]icemelon[D0T]c0m.

Awesome Tutorials

Introduction to Sessions
By dave

Sessions are extremely cool and useful. They allow you to create variables which can be accessed by any page on your site. This means you don't have to send values via $_POST or $_GET (which are visible to the user). What more can you ask for?

Debriefing
Your session variables exist across browser windows (of the same application). In other words, if you generate a session variable from one IE browser window, you can echo its value from any other IE browser window you have open.

However, the lifetime of a session is also limited by the browser window(s) you have open. Once you close all those IE browser windows, your session variables will be annihiliated. So sad. That's why, after logging out, you often receive the message "Please close your browser windows."

Getting started
To begin, call the session_start function. Like so..
session_start();
This will begin your session. If you would like to access a session variable, you must call this function first.

All your session variables/values are stored in the array $_SESSION. Here's an example:
session_start();

// create a session variable 'username' with value 'icemelon'
$_SESSION['username'] = 'icemelon';

// print out "Welcome <USERNAME>" message
echo "<p> Welcome $_SESSION[username]! ";
(You can also create a session variable with session_register, but I prefer the method above.)

Keep in mind, once the $_SESSION['username'] value has been set (as I've done above), you can access it from any other PHP script on your site. Remember, though, to call session_start() on each page you wish to use $_SESSION['username'].

When a session is initially started, an unique session ID will be created with it. For instance, if 8 computers access the same page (that starts a session), each computer will have a different session ID. To grab the value of this ID, use session_id. This ID is a very long alphanumeric string. Here's an example:
echo session_id();
// e.g. output: 915f99ab59a46de42cfd389adf85c39a

Because this ID is so "random" and unique, you can use it to generate a password if you wish. For instance:
// generate an 8-letter password
echo substr(md5(session_id()), 2, 8);
Aside: The function md5 is a popular hash function—used for encryption.

I bet you realize this is an extremely useful tool. When used right, the powers that are unleashed are confined only by your imagination and morals. Here are some values you may want your session variables to store:
» usernames/userids.
» an incrementing count (maybe, of the number of pages a user has visited).
» history of pages visited (e.g. home -> tutorials -> headlines -> home)
» subliminal messages!

There may come a time when you will want to delete the session variables. For instance, if Sam logs out of a public computer, and Lamb comes to use it.. you don't want your site to continue displaying the message "Hello Sam." Lamb would be utterly infuriated.

Here's how to destroy your variables and values:
$_SESSION = Array();
Yes, just set it to an empty array to clear the $_SESSION array.

Once you've got this down, the logical next step is to incorporate cookies into your site. I'll leave that for another tutorial.

Lat0r.

P.S. Check out my new site TheManWhoSoldtheWeb.com, where I publish guides and scripts on Internet Marketing and SEO. Here is a limited time freebie: the Rapid Google Indexer.


» Bookmark this Tutorial
» Bookmark IceMelon
Icemelon -- Introduction to Sessions -- PHP, CSS, Javascript Tutorials, & More!
  © 2005-2010 Icemelon.com   Email: dave[AT]icemelon[D0T]c0m