There are a few different ways to pass information from one page to another in PHP. I can name four right off the bat: Sessions, URL query strings, database queries and static file manipulation. Once you grasp each concept, you should come to the conclusion that using sessions may be the most effective way even if it isn't the easiest. PHP session management has come a long way and is very reliable if you have the settings correct in the php.ini file (or set the code appropriately in a configuration file of some sort).
Settings
For most uses, the default settings in the php.ini file are sufficient. If you don't have access to the php.ini file (like with shared hosting), you can change the settings in the .htaccess file or in your application code, depending on the settings.
What I like to do is develop my code on Microsoft Windows because I know that if I get everything working correctly on Windows, it will work on Linux or Mac computers. I use one of several personal web servers and each one runs on Windows almost like it does on Linux (I don't know about the Mac). I want to make sure my code is transportable because I'll never know for sure that I'll have the right hosting for anything specific I may create.
I change the pertinent settings in the code itself, via a configuration file that I call from each page. Now, I won't get into all the details of why one setting is better than another because the circumstances dictate what you need based on what kind of web application you're working with.
If you want to know all of the settings in exquisite detail, you should visit the session handling page at php.net. Here's what I have set in a configuration file I'm presently using:
# 30 minute garbage collection (1800/60 = 30)
ini_set('session.gc_maxlifetime',1800);# These two settings mean garbage collection happens for everyone, every time
ini_set('session.gc_probability',1);
ini_set('session.gc_divisor',1);# Set session cookie to expire on browser close in case the ini settings are different
session_set_cookie_params(0);# Start the session
session_start();
Since I include the configuration file at the top of every page, I don't need to repeat the "session_start()" line. Sure, the other settings get set every time, but that doesn't matter – it just changes in memory. Sessions, on the other hand, are stored on disk.
Speaking of including the configuration file, I use require 'config.php'; if I'm including it from a page in the root of the site and the configuration file is in the root as well. It gets trickier if you don't know what the path is going to be if including it from a page not in the root. Here's how I do it: require $_SERVER['DOCUMENT_ROOT'] . '/config.php';
It's a bit more complicated if the root of the site is not in the root directory of the server. If it's one directory deeper, I use this:
$a = explode('/', $_SERVER['PHP_SELF']);
require $_SERVER['DOCUMENT_ROOT'] . '/'. $a[1] . '/config.php';
Why? Because I won't always know what that subdirectory name is.
Storing and Retrieving Session Information
Once you're sure you have the settings correct, it's time to start using sessions. To store something in a session, use $_SESSION['variable'] = $variable for single variables or $_SESSION['variable']['variable'] = $variable['variable'] for arrays. To read from a session, just flip the code on each side of the equal signs. Of course, these are simplistic examples. Here's a real world example of how to pull a multiple fields from a MySQL database table and stash them in sessions:
$sql = "SELECT * FROM table_name";
$result = mysql_query($sql, db_connect());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($row as $key => $value) {
$_SESSION[$key][] = $value;
}
}
That is exactly the code I use to pull data from an options table. I'll provide the db_connect() function I use in another article because there's more than meets the eye with that one.
Summary
I was originally going to include another section but I realized it would make this article too long to digest in one sitting and I just briefly touched on sessions. Entire books have been written on session management, but I think that's too in-depth for beginners. You should experiment with the basics before you move on to more advanced methods.
The manual at php.net covers the functions quite well and is laid out much better than other language sites (like the one for MySQL, in fact). I have read everything, including the comments, for every function related to sessions and I still get confused and have to go back and check the instructions periodically. I'm sure that people who do PHP programming for a living do the same thing – there's just way too much to memorize.
Similar Articles:
- PHP Configuration Files and Reusable Code
- PHP and the ".user.ini" File
- PHPText Blogger: Pre-Alpha Software Notes
- An Easy Way to Stop Automated Comment Spam
- A Unique Flat File Data Format
This article is published as: PHP Programming for Beginners: Using Sessions
I apparently know even less about php than I thought, lol. Going to download and read the user manual, then hopefully maybe this will make some sense to me :)
Chelle recently posted..Get Over 100 Free Digital Scrapbooking Layouts
I've been wanting to try my hand at PHP. Great post. You make it sound easy.
Do you think that tizag.com is a great site for those who wish to learn PHP?
David@Military Coins recently posted..Custom Order Management System
I don't know. I'm not familiar with it.
I'm using same concept to collect number of users online meaning in the period I had set for the garbage collector (about 20 minutes).
Only thing is I get number of user for the overall server, not for a specific site. To see the script look on my page http://blog.yaaqui.com/index.php an notice the number of users.
To differentiate them, you need to store $_SERVER['HTTP_HOST'] and then your query has to include WHERE host_field = '$host' along with the rest or you will always get the same thing for all the hosts on that server.
Sessions is great, I use sessions to set cookies for setting up members only sections of my websites. This makes it so I don't have to log into my account everytime I want to access my members area. Great post, very helpful information.