Home arrow MySQL arrow Page 4 - Developing Custom PHP Sessions

Developing Custom PHP Sessions

Ever wondered how to make your own session handling functions with PHP? In this article Brian shows us how to do just that using PHP, MySQL and a bit of maths!

Author Info:
By: Brian Rosner
Rating: 3 stars3 stars3 stars3 stars3 stars / 41
July 21, 2002
  1. · Developing Custom PHP Sessions
  2. · Developing the Backend
  3. · Creating the Session Functions (contd.)
  4. · Using Our Sessions
  5. · Conclusion

print this article

Developing Custom PHP Sessions - Using Our Sessions
(Page 4 of 5 )

Let's start by creating a new file named login.php. Add the following code to login.php:


include "sess.php";

if($login) {

$sess = new session;



header("Location: welcome.php");




<form method="post" action="<?= $PHP_SELF; ?>">

Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" name="login" value="Login">


The code above has our login form and if it's submitted it will initialize the class and use the proper functions to start and register the values. Then the user is redirected to the welcome.php page.

Last, but surly not least, we need to create our welcome.php page:


include "sess.php";

$sess = new session;

if($logout == "yes") {
header(“Location: login.php”);



Welcome, <?= $sess_val; ?><br>
<a href="<?= $PHP_SELF; ?>?logout=yes">Logout</a>


Since this page is the first one to use our sessions with the passing key, we call the read() function to fetch the value. As seen in the read() function, we saved the value to $sess_val so where we want that data to appear we print it to the user. Then we offer a link to log out, which uses the destroy() function to kill the current session.
blog comments powered by Disqus

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 – More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials