Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- common.php:
- <?php
- session_start();
- function registerUser($user,$pass1,$pass2){
- $errorText = '';
- // Check passwords
- if ($pass1 != $pass2) $errorText = "Passwords are not identical!";
- elseif (strlen($pass1) < 6) $errorText = "Password is to short!";
- // Check user existance
- $pfile = fopen("userpwd.txt","a+");
- rewind($pfile);
- while (!feof($pfile)) {
- $line = fgets($pfile);
- $tmp = explode(':', $line);
- if ($tmp[0] == $user) {
- $errorText = "The selected user name is taken!";
- break;
- }
- }
- // If everything is OK -> store user data
- if ($errorText == ''){
- // Secure password string
- $userpass = md5($pass1);
- fwrite($pfile, "\r\n$user:$userpass");
- }
- fclose($pfile);
- return $errorText;
- }
- function loginUser($user,$pass){
- $errorText = '';
- $validUser = false;
- // Check user existance
- $pfile = fopen("userpwd.txt","r");
- rewind($pfile);
- while (!feof($pfile)) {
- $line = fgets($pfile);
- $tmp = explode(':', $line);
- if ($tmp[0] == $user) {
- // User exists, check password
- if (trim($tmp[1]) == trim(md5($pass))){
- $validUser= true;
- $_SESSION['userName'] = $user;
- }
- break;
- }
- }
- fclose($pfile);
- if ($validUser != true) $errorText = "Invalid username or password!";
- if ($validUser == true) $_SESSION['validUser'] = true;
- else $_SESSION['validUser'] = false;
- return $errorText;
- }
- function logoutUser(){
- unset($_SESSION['validUser']);
- unset($_SESSION['userName']);
- }
- function checkUser(){
- if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
- header('Location: login.php');
- }
- }
- ?>
- index.php:
- <?php
- require_once('common.php');
- checkUser();
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title>Micro Login System</title>
- <link href="style/style.css" rel="stylesheet" type="text/css" />
- </head>
- <body>
- <div id="main">
- <div class="caption">Login System Demo Page</div>
- <div id="icon"> </div>
- <div id="result">
- Hello <?php echo $_SESSION['userName']; ?> ! <br/>
- <p>This site demonstartes how to use Micro Login System.</p>
- <p><a href="logout.php"> To log out click here!</a></p>
- </div>
- <div id="source">Micro Login System v 1.0</div>
- </div>
- </body>
- login.php:
- <?php
- require_once('common.php');
- $error = '0';
- if (isset($_POST['submitBtn'])){
- // Get user input
- $username = isset($_POST['username']) ? $_POST['username'] : '';
- $password = isset($_POST['password']) ? $_POST['password'] : '';
- // Try to login the user
- $error = loginUser($username,$password);
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title>Micro Login System</title>
- <link href="style/style.css" rel="stylesheet" type="text/css" />
- </head>
- <body>
- <div id="main">
- <?php if ($error != '') {?>
- <div class="caption">Site login</div>
- <div id="icon"> </div>
- <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform">
- <table width="100%">
- <tr><td>Username:</td><td> <input class="text" name="username" type="text" /></td></tr>
- <tr><td>Password:</td><td> <input class="text" name="password" type="password" /></td></tr>
- <tr><td colspan="2" align="center"><input class="text" type="submit" name="submitBtn" value="Login" /></td></tr>
- </table>
- </form>
- <a href="register.php">Register</a>
- <?php
- }
- if (isset($_POST['submitBtn'])){
- ?>
- <div class="caption">Login result:</div>
- <div id="icon2"> </div>
- <div id="result">
- <table width="100%"><tr><td><br/>
- <?php
- if ($error == '') {
- echo "Welcome $username! <br/>You are logged in!<br/><br/>";
- echo '<a href="index.php">Now you can visit the index page!</a>';
- }
- else echo $error;
- ?>
- <br/><br/><br/></td></tr></table>
- </div>
- <?php
- }
- ?>
- <div id="source">Micro Login System v 1.0</div>
- </div>
- </body>
- logout.php:
- <?php
- require_once('common.php');
- logoutUser();
- header('Location: index.php');
- ?>
- register.php:
- <?php
- require_once('common.php');
- if (isset($_POST['submitBtn'])){
- // Get user input
- $username = isset($_POST['username']) ? $_POST['username'] : '';
- $password1 = isset($_POST['password1']) ? $_POST['password1'] : '';
- $password2 = isset($_POST['password2']) ? $_POST['password2'] : '';
- // Try to register the user
- $error = registerUser($username,$password1,$password2);
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title>Micro Login System</title>
- <link href="style/style.css" rel="stylesheet" type="text/css" />
- </head>
- <body>
- <div id="main">
- <?php if ((!isset($_POST['submitBtn'])) || ($error != '')) {?>
- <div class="caption">Register user</div>
- <div id="icon"> </div>
- <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="registerform">
- <table width="100%">
- <tr><td>Username:</td><td> <input class="text" name="username" type="text" /></td></tr>
- <tr><td>Password:</td><td> <input class="text" name="password1" type="password" /></td></tr>
- <tr><td>Confirm password:</td><td> <input class="text" name="password2" type="password" /></td></tr>
- <tr><td colspan="2" align="center"><input class="text" type="submit" name="submitBtn" value="Register" /></td></tr>
- </table>
- </form>
- <?php
- }
- if (isset($_POST['submitBtn'])){
- ?>
- <div class="caption">Registration result:</div>
- <div id="icon2"> </div>
- <div id="result">
- <table width="100%"><tr><td><br/>
- <?php
- if ($error == '') {
- echo " User: $username was registered successfully!<br/><br/>";
- echo ' <a href="login.php">You can login here</a>';
- }
- else echo $error;
- ?>
- <br/><br/><br/></td></tr></table>
- </div>
- <?php
- }
- ?>
- <div id="source">Micro Login System v 1.0</div>
- </div>
- </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement