- Impact
- 31
Here is a simple PHP sessions tutorial I put together! It's pretty fleshed out in notations. This is all assuming you've seen my previous tutorials explaining classes, pointers, etc! Enjoy!
-RageD
functions.php
init.php (Most important document! Makes everything run seemlessly)
index.php
-RageD
functions.php
PHP:
<?php
/**
* Simple Sessions Tutorial by RageD
*
* (C) RageD 2009. All Rights Reserved.
*
*
* All of our simple functions will be held here! :D
* All the login/logout will be carried out. Perhaps later
* I'll add on with a MySQL version so you can checkAuth();
* against a db, etc!
*
*/
if(!defined("AUTH_ACCEPT"))
{
echo "Unauthorized Access!";
exit;
}
class Session
{
/**
* Login
*
* Starting right on it :) No need for a __construct();
* or other variables to be defined because this isn't a
* large project carrying from multiple files, functions,
* classes, etc.! :)
*
*/
function login($user,$pass)
{
// Here we will completely carryout the login function
// according to data inserted. Since we aren't logged
// into a DB this is going to be static and only allow
// as many as you define.
if($user == "Test" AND $pass="w00t" OR $user == "User2" AND $pass == "pass2")
{
$_SESSION['username'] = $user; // Sets the session data username = to $user
print "Login Successful! Go back <a href=\"?page\">home</a>"; // Prints msg
} else {
print "Invalid login credentials!"; // Prints msg
return false;
}
return true;
}
/**
* Logout
*
*/
function logout()
{
// Unbelievably simple but... Yep, that's it! ;)
session_destroy(); // Destroys session
print "Logout Successful!"; // Prints message
return true;
}
/**
* CheckAuth
*
* Again, no DB so no checking for permissions
* only for session data really :)
*
*/
function checkAuth()
{
if(!$_SESSION['username'])
{
print "You must be logged in!";
return false;
}
return true;
}
}
?>
init.php (Most important document! Makes everything run seemlessly)
PHP:
<?php
/**
* Simple Sessions Tutorial by RageD
*
* (C) RageD 2009. All Rights Reserved.
*
* This has to be the first thing included in all documents for them
* to hold session data.
*
* Also, (besides comments such as this) session_start(); must start this
* this document before anything else!
*
*/
session_start();
// Make sure people can't just go into private files :) (E.g.: functions.php)
define("AUTH_ACCEPT", true);
/**
* Normally more information would be in here but since we're just handling
* sessions, not much more is necessary :)
*
*/
?>
index.php
PHP:
<?php
/**
* Simple Sessions Tutorial by RageD
*
* (C) RageD 2009. All Rights Reserved.
*
* Main index script! :)
*
*/
// Needs to be included first remember:
require_once("init.php");
// Include functions!
require_once("functions.php");
$session = new Session; // Declare our session and how we're going to call ptrs
$page = $_GET['page']; // Declare this to input multiple pages in this doc :)
switch($page)
{
default:
if(!$_SESSION['username'])
{ // Basically meaning if user is not logged in!
?>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form name="login" method="post" action="?page=login">
<p>User: <input type="text" name="user" /></p>
<p>Pass: <input type="password" name="pass" /></p>
<p><input type="submit" value="Login" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
<?php
} else {
?>
<html>
<head>
<title>Home (User: <?php echo($_SESSION['username']); ?> Logged in!)</title>
</head>
<body>
<h2>Options:</h2>
<p><a href="test.php">Test to see if session works on multiple pages</a></p>
<p><a href="?page=logout">Logout</a></p>
</body>
</html>
<?php
}
break;
case 'login':
$session->login($_POST['user'],$_POST['pass']);
break;
case 'logout':
$session->logout();
break;
}
?>
Last edited: