Unregistered Avatar

Login System


This is a very easy way of creating a login page. Its for one user and it uses sessions. To create this script you need a webserver with PHP support.

1. Open notepad or any other text editor of your choise and first make the login page. To create this we only need HTML.

Code:
<form action="secret.php" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value=" Login ">
If you arent familiar with HTML is this just a simple form with two boxes to type in 'username' and 'password'. Save this file as login.html

2. Now we need to create the code that checks if the password and username is right. Again use notepad or any other text editor and write this:

PHP Code:
<?php

// Session start
session_start();

// Checks password and username
if($username == "test" AND $password == "1337") {
        
// Session userid and loggedin is true
    
$_SESSION[username] = "test"
    
$_SESSION[userid] = 1;
    
$_SESSION[loggedin] = 1;
    echo 
"<font face='verdana' size='1'>You are now logged in as 'test'</font>";

} else {


        echo 
"You need access to view this page!"
    
exit;

}


php?>
Save this as secret.php

The '//' is comments. And you can offcourse add HTML on this page.

4. Now we gonna create a second page were you are logged inn with sessions.

PHP Code:
<?php
session_start
if($_SESSION['loggedin'] == 1) {

// Add html if you want, this what to be shown when access

} else {
// This is what happend when someone don't got access to the page.
 
echo "You need access to view this page!"
 
exit;

}
?>
Save this as secret2.php

5. If you don't use sessions the second logged in page wouldnt work, beacuse it check if you have loggedin = 1 and you have that from the secret.php
Edit the password and username in secret.php, just change the values of $password and $username. You can also create alot of pages like secret2 just remember to add the php code:
PHP Code:
<?php
session_start
if($_SESSION['loggedin'] == 1) {
// logged in
} else {
// if not logged in
}
?>
If you need any help, reply here

Quick Tutorial Search