Register.php
<?php include('core/init.inc.php'); $errors = array(); if(isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){ if (empty($_POST['username'])){ $errors[] = 'The user name cannot be empty.'; } if(empty($_POST['password']) || empty($_POST['repeat_pasword'])){ $errors[] = 'password cannot be empty.'; } if ($_POST['password'] !== $_POST['repeat_password']){ $errors[] = 'password not the same.'; } if (user_exists($_POST['username'])){ $errors[] = 'This user name already registered.'; } if (empty($errors)){ add_user($_POST['username'], $_POST['password']); $_SESSION['username'] = htmlspecialchars($_POST['username']); header('Location: protected.php'); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Untitled 1</title> </head> <body> <div> <?php if (empty($erros)===false){ ?> <ul> <?php foreach ($errors as $error){ echo "<li>{$error}</li>"; } ?> </ul> <?php } ?> </div> <form action="" method="post"> <p> <label for="username">username:</label> <input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlspecialchars($_POST['username']);?>"/> </p> <p> <label for="username">password:</label> <input type="text" name="password" id="password" value=""/> </p> <p> <label for="username">Retype Password:</label> <input type="text" name="repeat_password" id="repeat_password" value=""/> </p> <p> <input type="submit" value="register"/> </p> </form> </body> </html>
___________________________________________________
user.inc.php
<?php // Checks if given username exists in the table function user_exists($user){ $user = mysql_real_escape_string($user); $total = mysql_query("SELECT COUNT(user_id) FROM table_users WHERE username = '{$user}'"); return(mysql_result($total, 0) == '1') ? true : false; } // checks if the givin username and password is valid function valid_credebtails($user, $pass){ $user = mysql_real_escape_string(htmlspecialchars($user)); $pass = sha1($pass); $total = mysql_query("SELECT COUNT (user_id) FROM table_users WHERE username = '{$user}' AND password = '{$pass}'"); return(mysql_result($total, 0) =='1') ? true : false; } // adds a user to the database function add_user($user, $pass){ $user = mysql_real_escape_string(htmlspecialchars($user)); $pass = sha1($pass); mysql_query("INSERT INTO table_users (username, password) VALUES ('{$user}', '{$pass}')"); } ?>
_____________________________________________
init.inc.php
<?php session_start(); $exceptions = array('register', 'login'); $page = substr(end(explode('/',$_SERVER['SCRIPT_NAME'])), 0, -4); if(in_array($page, $exceptions)===false){ if(isset($_SESSION['username'])===false){ header('Location: login.php'); die(); } } mysql_connect('localhost', 'root', '1'); mysql_select_db('table_system'); $path = dirname(__FILE__); include("{$path}/inc/user.inc.php"); ?>