PHP Tutorial: Register and Login (User Account System)

Ask about a PHP problem here.
Post Reply
hellboj
Posts: 18
Joined: Sat Jan 28, 2012 9:16 pm

PHP Tutorial: Register and Login (User Account System)

Post by hellboj »

Good day, I already did this PHP tutorial and great work we all just that little mistake I can not in any way to eliminate ....

Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/password/login.php on line 44

login.php
<?ob_start();?>
<?php

include('core/init.inc.php');

    $errors = array();
    
    if (isset($_POST['username'], $_POST['password'])){
        if (empty($_POST['username'])){
            $errors[] = 'Prosim vnesi Up.ime: .';
        }
        if(empty($_POST['password'])){
            $errors[] = 'Prosim vnesi Geslo: .';
        }
        if (valid_credentials($_POST['username'], $_POST['password']) === false){
            $errors = 'Up.ime: / Geslo: neplavilno.';
        }
        if (empty($errors)){
            $_SESSION['username'] = htmlentities($_POST['username']);
            
            header('Location: protected.php');
            die();
        }
    }


?>
<!DOCTYPE html PUBLIC "-//W3C//XHTML 1.0 STRICT//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml-strict.dtd">
<html xmlns="http//www.w3.org/1999/xhtml" />
    <head>
            <meta http-equiv="Content-Type" content="text/html; charst=utf-8" />
            <link rel="stylesheet" type="text/css" href="ext/css/still.css"
            <title> Polna Pljuca "Prijava"  </title>
    </head>
<body>
        <div>
            <?php
            
                if (empty($errors) === false){
                    ?>
                    <ul>
                        <?php
                        
                        foreach ($errors as $error){
                            echo "<li>{$error}</li>";
                        }
                        
                        ?>
                    </ul>
            <?php
                }else{
                    echo 'Nov racun? <a href="register.php">Registriraj se...</a>';
                }
            ?>
            
        </div>
    <form action="" method="post">
        <p>
            <label for="username">Up.Ime:</label>
            <input type="text" name="username" id="username" value="<?php  if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
        </p>
        <p>
            <label for="password">Geslo:</label>
            <input type="password" name="password" id="password" />
        </p>
        <p>
            <input type="submit" value="Prijava"
        </p>
    </form>
</body>
</html>
<?ob_flush();?>
We can ask how to help, are you in advance thank you. :P
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Register and Login (User Account System)

Post by jacek »

Okay, the err is coming form this line
foreach ($errors as $error){
The problem is that $errors is not an array, most likely caused by this error happening
        if (valid_credentials($_POST['username'], $_POST['password']) === false){
            $errors = 'Up.ime: / Geslo: neplavilno.';
        }
Since here you just define $errors as a string instead of adding a string to the existing array, it should be
        if (valid_credentials($_POST['username'], $_POST['password']) === false){
            $errors[] = 'Up.ime: / Geslo: neplavilno.';
        }
Image
Post Reply