Jump to content
xisto Community
ghostrider

Sessions And Our First Project Seventh PHP Tutorial

Recommended Posts

Intro To PHP
Tutorial 7 - Sessions and Our First Project
Released 4/27/07
By Chris Feilbach aka GhostRider

Contact Info:
E-mail: assembler7@gmail.com
AIM: emptybinder78Yahoo: drunkonmarshmellows
Website: http://forums.xisto.com/no_longer_exists/

Today, I am going to give us a project to work on together. I originally had planned that at the end of tutorial that we were going to write Solitare in PHP, but that seems hard and complex and way beyond of the intended scope of this tutorial. Perhaps for another tutorial.

My friend Laurie reccomended that we write Tic Tac Toe because it is simple and everybody knows how to play it. It will be our first project we will design and code, and possibly even debug. I might put a bug or two in their to show how to debug scripts :unsure:. Once we learn more we will come back to our project and add onto it.

After we cover working with images (which will be very fun to learn), we'll make it more graphical. Working with images is very rewarding, and I believe it is quite fun. I'm very excited to be able to teach it to you.This is going to be the last tutorial that demonstrate a new concept until after we complete Tic Tac Toe.

The next couple tutorials, probably the next 3 or 4, will demonstrate how to plan a script, how to actually code it, and its visual aspects. However that's in the future and I've already written four off-topic paragraphs. We need to cover sessions.

So what exactly are sessions? A session is simply an array that stores data. However this array is different in a very special way, it follows the user from page to page, which allows you to keep data about the user and have it accessible no matter what PHP page you are on in your website. Each session is identical to the each computer. Many login scripts depend on this technique to detemine whether a user is logged in or not.

PHP keeps track of each session by giving each session an id that is hundreds of characters long, something that no one could possibly guess. You usually don't have to worry about this as a programmer, however there is one thing you should check when implementing a script with sessions.

PHP has something called environmental variables that tell PHP how to run. There are hundreds of these. You can check them by creating a small php script like so:

<?PHPphpinfo(); // Displays all of PHP's variables.PHP?>

You need to go the section called session, and then check the variable "use_trans_id". It should be 1. If it is not equal to 1, you need to either go into your php.ini file and change "session.use_trans_id = 0" to "session.use_trans_id = 1", or get in contact with your system's administrator and tell them to change the value to one.

The "use_trans_id" variable tells PHP how to transfer the session id between pages. When it is equal to 1, PHP does this without you having to do anything special. If its 0, you need to transfer the session id from page to page. Not only does this invovle more coding for you, which takes up more of your valuable time, it is a security risk as you are revealing the id of the session of a certain user when the data is transfered from the client to your webserver. Any skilled hacker can take advantage of that.

On each page you need to tell PHP to initalize the session array, which is accessable through the variable $_SESSION['']. This function is special in the sense that it needs to be sent before any output is sent to your user's web browser. Even if you put a single space, or even press enter, it will display an error message telling you that the headers of the page have already been sent.

The function session_start() will either initialize the session variable, or create a new one if one does not exist. You need this at the beginning of your code on every PHP page that you want to be able to use the session. Once the session is started, you can add elements to the session array. Its really quite simple.

<?PHPsession_start(); // Session is started$_SESSION['fname'] = "Chris";$_SESSION['age'] = 16;PHP?>

There is no example for today. You know all you need to use sessions. The next tutorial will be out in a couple days.
Edited by ghostrider (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.