RyanRyan
03-28-2007, 01:50 AM
I have made some code for a flat-file login system, because my server doesnt have mysql, and I can see whats wrong, it registers fine, but when it comes to login... the code for common.php is <?php
session_start();
function registerUser($username,$password1,$password2){
$errorText = '';
$validUser = false;
// Check the passwords
if ($password1 != $password2) $errorText = "Your passwords dont match";
elseif (strlen($password1) < 6) $errorText = "Your 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] == $username) {
$errorText = "That user name is taken";
break;
}
}
// If everything is OK -> store user data
if ($errorText == ''){
// Encrypted password string
$userpass = md5($password1);
fwrite($pfile, "\r\n$username:$userpass");
}
fclose($pfile);
return $errorText;
}
function loginUser($username,$password){
$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] == $username) {
// User exists, check password
if ($tmp[1] == md5($password)){
$validUser = true;
$_SESSION['userName'] = $username;
}
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');
}
}
?> and heres my login <?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);
}
?>
my html code would go here
<?php
}
if (isset($_POST['submitBtn'])){
?>
<?php
if ($error == '') {
echo "Welcome $username! <br/>You are logged in!<br/><br/>";
echo '<a href="index.php">Now you can visit the homepage!</a>';
}
else echo $error;
?>
<br/><br/><br/>
<?php
}
?>
</body>
session_start();
function registerUser($username,$password1,$password2){
$errorText = '';
$validUser = false;
// Check the passwords
if ($password1 != $password2) $errorText = "Your passwords dont match";
elseif (strlen($password1) < 6) $errorText = "Your 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] == $username) {
$errorText = "That user name is taken";
break;
}
}
// If everything is OK -> store user data
if ($errorText == ''){
// Encrypted password string
$userpass = md5($password1);
fwrite($pfile, "\r\n$username:$userpass");
}
fclose($pfile);
return $errorText;
}
function loginUser($username,$password){
$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] == $username) {
// User exists, check password
if ($tmp[1] == md5($password)){
$validUser = true;
$_SESSION['userName'] = $username;
}
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');
}
}
?> and heres my login <?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);
}
?>
my html code would go here
<?php
}
if (isset($_POST['submitBtn'])){
?>
<?php
if ($error == '') {
echo "Welcome $username! <br/>You are logged in!<br/><br/>";
echo '<a href="index.php">Now you can visit the homepage!</a>';
}
else echo $error;
?>
<br/><br/><br/>
<?php
}
?>
</body>