Web Development

Using Sessions to Make a PHP Login Script

Using Sessions to Make a Login Script

To begin this tutorial in PHP sessions, you must first make sure that your web server (ie. apache) has the options to allow session with the configuration.  Note: people using apache and php under a win32 enviorment must manually configure this option.

To start, this tutorial, you should have some basic knowledge of php coding.  Refer to Hotscripts for some basic and more advanced php coding tricks.

To begin, create a test folder called session.  With that you will created the two files below in that folder.

With that in mind let us begin.  First, create a text file called config.php and add in the following code into that textpage.

config.php

session_start();
if ((!$username) || (!$password)) {
echo '<form name=login method=post action="">
user:<input type=text name=username><br>
pass:<input type=text name=password><br>
<input type=submit value=go>
</form>';
}
else {
if ($username=="$uname")  {
session_register("username");
session_register("password");
echo "user is $uname, and password is $pword
<br> <a href=\"?m=1\" >unreg</a>";
}
else echo "nope";
}
if ($m==1) {
session_unregister("username");
session_unregister("password");
}
?>

Now open that session folder and you should see the code in action!!

About the author

Written by Darren W..

Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design.

Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.

If you found this post useful you may also want to check these out:

  1. How to make your own PHP template script
  2. Developing a Login System with PHP and MySQL
  3. Client Clones and Server Sessions
  4. How to Make a Drop Down Template Theme Selector with PHP
  5. Creating and Accessing MySQL Data with PHP
  6. Script Web Databases Quickly with PHP Scripting Language