Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-28-2007, 01:50 AM   PM User | #1
RyanRyan
New Coder

 
Join Date: Mar 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
RyanRyan is an unknown quantity at this point
flat-file login system: cant find what wrong

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 Code:
<?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 Code:
<?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>
RyanRyan is offline   Reply With Quote
Old 03-28-2007, 02:14 AM   PM User | #2
Armondo
Regular Coder

 
Armondo's Avatar
 
Join Date: Feb 2007
Posts: 144
Thanks: 3
Thanked 0 Times in 0 Posts
Armondo is an unknown quantity at this point
hmmm....idk ryan, maybe its like...well what are the errors your getting?
__________________
..
▲ ▲
Armondo is offline   Reply With Quote
Old 03-28-2007, 02:15 AM   PM User | #3
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,714
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
So, have you looked at the content of your userpwd.txt file and compared it with what your code is doing when it reads this file? Also, have you echoed any of the data to see what is being tested and used in the program to find where in the code it is working and at what point it is not?

I see two problems in your code -

1) You are rewinding the file pointer after you fopen() the file in the register and login code. When you open the file for append in your register code, this will move the file pointer from the end of the file to the beginning of the file. This will cause data at the start of the file to be overwritten.

2) When you write to the file in the register code, you use a : as the separator. However, in all the other code you are exploding using ||, which is why I asked if you have looked at the content of your userpwd.txt file. The first step in troubleshooting is verifying that the data you are operating on is what you expect.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 03-28-2007, 02:36 AM   PM User | #4
RyanRyan
New Coder

 
Join Date: Mar 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
RyanRyan is an unknown quantity at this point
Thanks soo much I was looking over and it was saving it as user : pass THEN it tried to explode it like it was user||pass thank you.
RyanRyan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:14 PM.


Advertisement
Log in to turn off these ads.