Tutorial
Introduction
This tutorial will teach you the use of SESSION variables.
Normal variables are not able to get used in other files, so if we need variable
$string from file index.php in file actions.php ... we can't use it.
Step 1 - Information
There are some things you need to know before you start using session variables.
- Session variables can be changed by any one who knows what they are called and how it works.
- Session variables will exist untill the browser is closed
- In every file you want to use the session variables in, you must open the session.
This tutorial will give you some examples, and then will provide you with a small exercise.
The following example will show you what's wrong and what's right.
Wrong Example:
File: index.php
Code:
<?php
$string = 10;
echo $string;
?>
File: actions.php
Code:
<?php
$string = $string + 5; // string does not exist
$string = $string / 2; // string does not exist
echo $string; // string does not exist
?>
Good Example:
File: index.php
Code:
<?php
session_start();
$_SESSION['string'] = 10;
echo $_SESSION['string'];
?>
File: actions.php
Code:
<?php
session_start();
$_SESSION['string'] = $_SESSION['string'] + 5;
$_SESSION['string'] = $_SESSION['string'] / 2;
echo $string;
?>
Image Example

As you can see sessions are a way to save your variables in your browser so other files can use them.
The session_start(); opens the session which allows you to use session variables.
The session variables are array variables just like the POST and GET.
|
Step 2 - Exercise
Create a file: index.php
This file has to display a sum, for example: 10 * 1463 =
make a text link which will link to the next file:
action.php
when you have opened this file, this script must automaticly get the session variables to create the entire sum and calculate it.
so this file will display for example: 10 * 1463 = 14630
Try and do this exercise with both normal variables and session variables.
If you need help doing this exercise please contact any scripter from this website for help or contact us through the contact form.
Good luck !!
| « Previous | Next » |
Comments
You have to be logged in to write a comment.



