[PHP Tutorial] Sessions for Beginners

Discussion about the creation of websites in general, HTML, PHP, Java.

This tutorial was useful for you?

Yes
1
100%
No
0
No votes
 
Total votes : 1

[PHP Tutorial] Sessions for Beginners

Postby thestrifer on Sat Sep 09, 2006 12:07 pm

Hello People, i'll colaborate with some PHP Tutorials of my authory.
Please, feel free to ask, to advise about errors.
Those tutorials are directed for peoples that are starting on PHP like me, if you're a expert just disconsider it Embarassed.

1st. What is Sessions?
Its a method of store data on server.
Generally are used, and its one of the best methods to do a Login System on PHP. This Tutorial will teach a little Log-in System, based on Sessions. The Sessions mechanism are easy, simple and pratice and efficient as well.
It does not require a browser with Cookies Enabled. It'll work independently if cookies are allowed or not at the user's browser.


2nd. The Functions
The functions that we'll use to handle the sessions:

Code: Select all
<?php
// Allow to use sessions in a Page, must be the first code in the header of page
session_start(); // No arguments

// In the sessions the data are stored in variables.
$username = "TheStrifer";
session_register("username"); // Note that the variable is wrote without the  $ indicator.

// With this one, we'll delete all the used variables, but we'll NOT delete the session:
session_unset();

// With this one, you can know if the variable was registered or not:
session_is_registered("username);

// You might use one control structure as well
if (session_is_registered("username")) {
    echo "Theres a logged User";
} else {
    echo "Theres no logged User.";
}

// And with this function, we destroy the whole data of the Visitant's session, ideal for Log-off pages
session_destroy(); // No arguments



3rd. Introduction
The sessions can be easily simplified as:

    This command should be used in all the pages that will handle the session.
Code: Select all
session_start(); // No arguments

    So, you define the variable that will store the session and its value.
Code: Select all
$username = "TheStrifer";

    After, insert the variable in the session:
Code: Select all
session_register("username"); // Without the "$" indicator

    To see the content of one variable, you must use the $_SESSION system's array.
Code: Select all
echo "Currently User: ".$_SESSION['username'];

    To delete the variable, you must use session_unregister(), by doing this you'll only delete the variable, not the whole session.
Code: Select all
session_unregister("username");


This is the main mechanism of the sessions.
Now lets start a Pratice Example.


3rd. Pratice Example: The Login System
Assuming that the page sent by the POST Method with the Username and Password by a form.
Assuming as well that you've tested on the database if the Username and the Password are corrects and the same created the variable $did_login, that will store the value 1 if the login was correct or 0 if occured any error (wrong password, for example):

    login.php: Creates the Session and Redirect the user to the password protected page.
Code: Select all
<?php
session_start();

if ($didLogin == 1) {
    $username = $_POST['string_username']; // Insert the form data to the variable
    session_register("username"); // Insert the variable to the session
    header("Location:mainpage.php"); // Redirect the user to the desired protected page.
} else {
    echo "The Username and/or Password are incorrect.<br><a href=\"javascript:history.back(-1);\">Click here and Try Again.</a>";
}
?>


The page above is a example for the login.php, the sessions must have a exclusive page for create and destroy the session. After, the protected page.

    mainpage.php: The page protected by the log-in.
Code: Select all
<?php
session_start();

if (session_is_registered("username")) {
    // If User did login
    // Here the login protected content.
} else {
   // Or Else
   die("Access Denied");
}
?>


In the protected page you might put a Log-off link, that should point to a page like this:

    logoff.php: The page that will destroy the log-in data.
Code: Select all
<?php
session_start();
// Destroy the whole data from this user
session_destroy();

echo "The log-off was did.";
?>
[/code]


[size=18]4rd. Warning[/size]
    Before the "session_start()" the code CAN'T have any kind of Output.
    It includes Echo, Printf, Headers, nothing. Or else it'll results in the famous error:

    [b]Cannot modify Header information - headers already sent by (...)[/b]

    So, be careful with the I/O and the Sessions, the session_start() sends a header to the browser that should be the primary header, thats why it must have a exclusive page to handle the creation/destruction of sessions.


[size=18]Conclusion[/size]
    Here ends the tutorial.
    Hope you enjoyed it, i've basic explained what is the Sessions, the functions used to handle it, and i gave one sample code.
    I plan to post also one MySQL + PHP tutorial for beginners and a second version (using database) of this login system.

    Feel free to ask, bye.[/list]
thestrifer
Contributer
Contributer
 
Posts: 21
Joined: Sat Sep 09, 2006 11:13 am
Location: Brazil

Return to General Webmastering

Who is online

Users browsing this forum: No registered users and 1 guest

cron