0

I'm having some trouble displaying my errors on this login form.

The login works but I can't figure out how to display those errors.

I just need to display them between the login field and the footer. I suppose the problem should be the last part of the foreach that should go true the error array.

<!DOCTYPE html>
<html lang="en">
    <body>
    <?php
    include ('includes/header.php');
    ?>
        <div class="nav">
            <?php
            include ('includes/menu.php');
            $error= logInData();
            ?>
        </div>
        <section role="main">
            <div class="logIn">
                     <h3>Intranet Login</h3>
            </div>
            <form action="" method="post">
                        <fieldset>
                            <legend>Student Log in</legend>
                            <div>
                                <label for="username">Enter username: </label>
                                <input type='text' id="userN" name="userN" value = "<?php if (isset($error['usern'])){echo $error['usern'];} ?>">
                            </div>
                            <div>
                                <label for="password">Enter password: </label>
                                <input type='password' id="pass" name="pass" value = "">
                            </div>
                            <div>
                                <p class="red"><?php if (isset($error['both'])) {
                                    echo $error['both'];
                                } ?></p>
                            </div>
                            <div>
                                <input type="submit" name="submit" value="Log-In">
                            </div>
                        </fieldset>
            </form>
        </section>
<?php

function logInData (){        
    $error = array();
    $validated = array();
    $clean = array();

    $pass = false;

    if (isset($_POST['submit']) && $pass == true) {
        $inputPass = ($_POST['pass']);
        $trimPass = trim($inputPass);
        $inputUsern = ($_POST['userN']);
        $trimUsern = trim($inputUsern);
        if(!empty($trimPass)){
            if (!ctype_alpha($trimPass)) {
                $error['passw'] = 'No special characters allowed on password';
                $pass = false;
            }else{
                if(empty($trimPass)){
                    $error['passw'] = 'password field empty';
                    $pass = false;
                }else{
                    $clean['passw'] = $trimUsern;
                    $pass = true;
                }
            }
        }if ($pass == true) {
            return $clean;
        }else {
            return $error;
        }
        if(!empty($trimUsern)){
            if (!ctype_alpha($trimUsern)) {
                $error['userN'] = 'No special characters allowed on username';
                $pass = false;
            }else{
                if(empty($trimPass)){
                    $error['userN'] = 'username field empty';
                    $pass = false;
                }else{
                    $clean['userN'] = $trimUsern;
                    $pass = true;
                }
            }
        }if ($pass == true) {
            return $clean;
        }else {
            return $error;
        }
        $dir = '/home/sbau01/public_www/php/fma/data';
        if (is_dir($dir)){
            $handleDir = opendir('/home/sbau01/public_www/php/fma/data');
            $path = "/home/sbau01/public_www/php/fma/data/data.txt";
            if(is_file($path)){
                $handle =  fopen($path, 'r');
                while(!feof($handle)){
                    $dataRow = fgets($handle);
                    if(!empty($dataRow)){
                        $separate = explode(' ',$dataRow);
                        $storedUsern = trim($separate[3]);
                        $storedPassword = trim($separate[4]);;
                        if (($clean['userN'] == $storedUsern) && ($clean['passw'] && $storedPassword)){
                            $match = true;
                            header('location: intranet.php');
                        }else{
                            $error['match']='<span >Username/Password is incorrect!!</span>';
                            $pass = false;
                        }
                    }
                }fclose($handle);
            }else{
                $error['data']='<span >Data not found</span>';
                $pass = false;
            }closedir($HandleDir);
        }else{
            $error['data']='<span >Data not found</span>';
            $pass = false;
        }
    }else {
    $errmsg = '';
    foreach($error as $key => $value){
            echo "ERROR: $value<br />\n";                   
        }   
    } 
}    


?>
        <footer>
            <?php include ('includes/footer.php');?>
        </footer>
    </body>
</html>
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
  • What is in your 'includes/header.php file? is it just variables and stuff? I would move it before ` `. – Bradmage Jan 06 '18 at 20:31

2 Answers2

0

Its a simple brackets error:

$errmsg = '';
foreach($error as $key => $value){
        echo "ERROR: $value<br />\n";                   
}

The part above is in the else condition of if (isset($_POST['submit']) && $pass == true) {

Thats why this will never execute. Simply remove the bracket above this part and add it after the foreach.

Saving Passwords in text files is NOT a great idea!

In line 101 you have probably an little mistake: You just check if there are the variables, you dont check if they are equal ($clean['passw'] && $storedPassword)){

  • These are good points, but also `$error= logInData();` is called inside the menu div and outputs data. This whole block of code should be moved on its own outside of the function. – Bradmage Jan 06 '18 at 20:24
  • Yeah i haven't realised that... The whole code is a mess – not-a-feature Jan 06 '18 at 20:27
0

A couple of issues identified.

Do you have display errors turned on? https://stackoverflow.com/a/21429652/1246494

You are calling $error= logInData(); at the top, but have your function logInData() { ... } created down below.

I think what you want to do it put the whole function in an include file at the top like:

include ('includes/header.php');
include ('includes/logInFunction.php');

You then want to call logInData(); down in the body.

Another issue is your function puts data in an array and echos data. If you are going to have $error= logInData(); at the top of your page try moving this out of your function and into your body where you want to output the errors.

if(count($error) > 0)
{
    foreach($error as $key => $value)
    {
        echo "ERROR: $value<br />\n";
    }
} 
Bradmage
  • 1,233
  • 1
  • 15
  • 41