Displaying a list of the errors and re-displaying a form???
Posted: Wed Dec 14, 2011 5:42 pm
Hi there guys im doing a simple login system using a flat file, i've managed to do the hardest but for some reason i cant seem to figure it out how to echo the errors as a list below the form also i'm having problems in re-displaying the form so that when a user writes his username and does not write his password he gets a message saying something like 'choose a password' but the username stays in the form...any ideas!!!
here is the code:
common.php
register.php
and form.php
Thank you guys in advance.
here is the code:
common.php
Code: Select all
<?php
session_start();
function registerUser($user,$pass1,$pass2){
$errors = '';
$clean_data = array();
$file_dir = 'xxxx/xxxxx/xxx.txt';
// Check username
if(preg_match('/^[a-z\d]{4,20}$/i',$_POST['name']) ) {
$username = trim($_POST['name']);
} else {
$errors = 'Username not valid! - minimum 4 characters, no spaces ';
}
if ($pass1 != $pass2) {
$errors = "Passwords are not identical!";
} elseif (strlen($pass1) < 6) {
$errors = "Password is to short!";
}
// Check user existance
$handle = fopen($file_dir,"a+");
rewind($handle);
while (!feof($handle)) {
$line = fgets($handle);
// $usi = user name input ex tmp
$usi = explode(':', $line);
if ($usi[0] == $user) {
$errors = "Username not available choose another!!";
break;
}
}
// If everything is OK -> store user data
if ($errors == ''){
// Secure password string
$userpass = md5($pass1);
fwrite($handle, "\r\n$user:$userpass");
}
fclose($handle);
return $errors;
}
function loginUser($user,$pass){
$errors = '';
$validUser = false;
$file_dir = '/home/pcurad01/private_data/users.txt';
// Check user existance
$handle = fopen($file_dir,"r");
rewind($handle);
while (!feof($handle)) {
$line = fgets($handle);
$usi = explode(':', $line);
if ($usi[0] == $user) {
// User exists, check password
if (trim($usi[1]) == trim(md5($pass))){
$validUser= true;
$_SESSION['username'] = $user;
}
break;
}
}
fclose($handle);
if ($validUser != true) $errors = "Invalid username or password!";
if ($validUser == true) $_SESSION['validUser'] = true;
else $_SESSION['validUser'] = false;
return $errors;
}
function logoutUser(){
unset($_SESSION['validUser']);
unset($_SESSION['username']);
}
function checkUser(){
if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
header('Location: login.php');
}
}
?>Code: Select all
<?php
require_once('common.php');
if (isset($_POST['submit'])){
// Get user input
$username = isset($_POST['name']) ? $_POST['name'] : '';
$password1 = isset($_POST['password1']) ? $_POST['password1'] : '';
$password2 = isset($_POST['password2']) ? $_POST['password2'] : '';
// Try to register the user
$error = registerUser($username,$password1,$password2);
}
require 'form.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>PHP FMA 2011</title>
</head>
<body>
<h1>PHP FMA 2011</h1>
<div id="navbar">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About Us</a></li>
<li><a href="database.php">Search Our Database</a></li>
</ul>
</div>
<div id="main">
<?php
if ((!isset($_POST['submit'])) || ($error != '')) {
echo '<h2>Register</h2>';
echo $output;
}
?>
<?php
if (isset($_POST['submit'])){
if ($error == '') {
echo " User: $username was registered successfully!<br/><br/>";
echo ' <a href="login.php">You can login here</a>';
}
else {
echo $error;
}
}
?>
</div>
</body>
</html>Code: Select all
<?php
$output = '';
$output .= '<form action="'.htmlentities($_SERVER['PHP_SELF']).'"method="post">
<fieldset>
<div>
<label for="fn">Username</label>
<input type="text" name="name" id="fn" size="40" value="'.(isset($clean['name']) ? htmlentities($clean['name']) : '') .'" />
</div>
<div>
<label for="em">Choose a Password</label>
<input class="text" name="password1" type="password" value="'.(isset($clean['password1']) ? htmlentities($clean['password1']) : '') .'" />
<div>
<label for="em">Re-Enter your Password</label>
<input class="text" name="password2" type="password" value="'.(isset($clean['password2']) ? htmlentities($clean['password2']) : '') .'" />
</div>
<div>
<label for="em">Enter your e-mail address</label>
<input type="text" name="email" id="em" size="40" value="'.(isset($clean['email']) ? htmlentities($clean['email']) : '') .'" />
</div>
<input type="submit" name="submit" value="Log In" />
<input type="submit" name="logout" value="Log Out" />
</fieldset>
</form>';
?>