Jump to content

Print error on different part of the website


matthijs110
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello all!

 

I got a login/register script. Its all working fine :). When I click on Login, and didn't fill in the fields. It prints the error as it should.
8nXeD.png

 

As you can see, its forced to print out under the menubar. But I want it into the Jumbotron. How can I do this?

This code is defined on the top of the page to print the error:
 

if($login_ok){ 
            unset($row['salt']); 
            unset($row['password']); 
            $_SESSION['user'] = $row;  
            header("Location: member.php"); 
            die("Redirecting to: member.php"); 
        } 
        else{ 
            print('<div class="alert alert-danger">Login failed!</div>');
            $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
        }

But how can I print this somewhere else? I tried to move the code and I tried this on the place where it should print:
 

if(!$login_ok){ 
     print('<div class="alert alert-danger">Login failed!</div>');
     $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
}

Unfortunately that didn't worked :(

 

Hopefully someone can help me out :)

Link to comment
Share on other sites

If you want the login error message to appear in the large grey area, then you need to output the Login Failed error message inside the HTML for for the Jumbotron element.

So I put this code into the Jumotron?

 

 

EDIT:

I just tried this, but it results in:

 

8o1fH.png

when I access the site

 

This is my complete project if you want the full overview:

https://github.com/matthijs110/Login-Project

if($login_ok){ 
            unset($row['salt']); 
            unset($row['password']); 
            $_SESSION['user'] = $row;  
            header("Location: member.php"); 
            die("Redirecting to: member.php"); 
        } 
        else{ 
            print('<div class="alert alert-danger">Login failed!</div>');
            $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
        }
Edited by matthijs110
Link to comment
Share on other sites

Okay.. Which file is processing the login?

 

index.php appears to be processing the login in two different places (on lines 4 - 36 and lines 121 - 148). Why? There is also a file called member.php which also processes the login.

 

You should only have one instance for processing the login.

Link to comment
Share on other sites

Okay.. Which file is processing the login?

 

index.php appears to be processing the login in two different places (on lines 4 - 36 and lines 121 - 148). Why? There is also a file called member.php which also processes the login.

 

You should only have one instance for processing the login.

Well I combined them. 

 

https://github.com/matthijs110/Login-Project

 

Which file should have the processing of the login? Index.php? It has the login fields.

Link to comment
Share on other sites

Okay looking further at your code it does appear to be index.php is reprehensible for processing the login.

 

So first delete the duplicated login code on lines 132 to to 161 in index.php.

                    <?php
                    if(isset($_POST["submit"])) {
                        $user=$_POST['user'];
                        $pass=$_POST['pass'];

                        $con=mysql_connect('localhost','root','') or die(mysql_error());
                        mysql_select_db('User_Management') or die("cannot select DB");

                        $query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
                        $numrows=mysql_num_rows($query);
                            if($numrows!=0) {
                                while($row=mysql_fetch_assoc($query)) {
                                    $dbusername=$row['username'];
                                    $dbpassword=$row['password'];
                                }

                                if($user == $dbusername && $pass == $dbpassword) {
                                    session_start();
                                    $_SESSION['sess_user']=$user;

                                    /* Redirect browser */
                                    header("Location: member.php");
                                } else {
                                    echo "Please fill in the fields!";   
                                }
                            } else {
                                echo "Invalid username or password!";
                            }
                    }
                    ?>

Now delete the else on line 44

        else{ 
            print('<div class="alert alert-danger">Login failed!</div>');
            $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
        }

And then change line 140 to

            <?php if(isset($login_ok) && $login_ok == false): ?>
                  <div class="alert alert-danger">Login failed!</div>
            <?php endif; ?>

            <h1>Hello, world!</h1> 

Does the error message now display in the correct location?

Edited by Ch0cu3r
Link to comment
Share on other sites

Okay looking further at your code it does appear to be index.php is reprehensible for processing the login.

 

So first delete the duplicated login code on lines 132 to to 161 in index.php.

                    <?php
                    if(isset($_POST["submit"])) {
                        $user=$_POST['user'];
                        $pass=$_POST['pass'];

                        $con=mysql_connect('localhost','root','') or die(mysql_error());
                        mysql_select_db('User_Management') or die("cannot select DB");

                        $query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
                        $numrows=mysql_num_rows($query);
                            if($numrows!=0) {
                                while($row=mysql_fetch_assoc($query)) {
                                    $dbusername=$row['username'];
                                    $dbpassword=$row['password'];
                                }

                                if($user == $dbusername && $pass == $dbpassword) {
                                    session_start();
                                    $_SESSION['sess_user']=$user;

                                    /* Redirect browser */
                                    header("Location: member.php");
                                } else {
                                    echo "Please fill in the fields!";   
                                }
                            } else {
                                echo "Invalid username or password!";
                            }
                    }
                    ?>

Now delete the else on line 44

        else{ 
            print('<div class="alert alert-danger">Login failed!</div>');
            $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
        }

And then change line 140 to

            <?php if(isset($login_ok) && $login_ok == false): ?>
                  <div class="alert alert-danger">Login failed!</div>
            <?php endif; ?>

            <h1>Hello, world!</h1> 

Does the error message now display in the correct location?

 

Yes it does :)

 

But now I found my next problem :( Registering.

 

When I hit Register, it opens the forum, if I click on the Register button of that form, it results to:

8o2Yz.png

Link to comment
Share on other sites

 

In register.php n line 104 the forms action should be set to register.php

<form action="register.php" method="post">

Okay, thats working  now :) But when I clicked "Register", it gives me the following error: 

Notice: Undefined variable: db in /Applications/XAMPP/xamppfiles/htdocs/Login-Project-3/register.php on line 24

 

Fatal error: Call to a member function prepare() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/Login-Project-3/register.php on line 24

 

But it is defined.

 

 

$query = " 
                SELECT 
                    1 
                FROM users 
                WHERE 
                    username = :username 
            "; 

$stmt = $db->prepare($query); 
Link to comment
Share on other sites

No, $db is not defined. 

 

This line

$stmt = $db->prepare($query); 

Is trying to use a method called prepare from an the $db object.

 

The variable $db is in fact defined in config.php. So after line 1 in register.php you'd include that file

include 'config.php';
Link to comment
Share on other sites

 

No, $db is not defined. 

 

This line

$stmt = $db->prepare($query); 

Is trying to use a method called prepare from an the $db object.

 

The variable $db is in fact defined in config.php. So after line 1 in register.php you'd include that file

include 'config.php';

I had that line before in the file. But I deleted it because of this error:

 

8o4NT.png

Link to comment
Share on other sites

Oh, I didnt see you were including register.php in index.php as part of a modal.

 

What I recommend you to do then is to move the registration form out of register.php

<form action="register.php" method="post"> 
    <div class="input-group">
        <span class="input-group-addon" style="min-width: 106px;"><i class="fa fa-user"></i> Username</span>
        <input type="text" class="form-control" name="user" placeholder="Username">
    </div>
    <br>
    <div class="input-group">
        <span class="input-group-addon" style="min-width: 106px; text-align:left;"><i class="fa fa-envelope"></i> Email</span>
        <input type="text" class="form-control" placeholder="name@example.com">
    </div>
    <br>
    <div class="input-group">
        <span class="input-group-addon" style="min-width: 106px;"><i class="fa fa-link"></i> Password</span>
        <input type="password" class="form-control" name="pass" placeholder="Password">
    </div>
    <br>
        <button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
        <input type="submit" class="btn btn-success" value="Register" />
</form>

 And put it into its own file, call this file register_form.php.

 

Now in index.php on line 188 you'd include register_form.php instead of register.php

<?php include"register_form.php"; ?>

And then in place of the form in register.php you'd now include register_form.php

Link to comment
Share on other sites

Oh, I didnt see you were including register.php in index.php as part of a modal.

 

What I recommend you to do then is to move the registration form out of register.php

<form action="register.php" method="post"> 
    <div class="input-group">
        <span class="input-group-addon" style="min-width: 106px;"><i class="fa fa-user"></i> Username</span>
        <input type="text" class="form-control" name="user" placeholder="Username">
    </div>
    <br>
    <div class="input-group">
        <span class="input-group-addon" style="min-width: 106px; text-align:left;"><i class="fa fa-envelope"></i> Email</span>
        <input type="text" class="form-control" placeholder="name@example.com">
    </div>
    <br>
    <div class="input-group">
        <span class="input-group-addon" style="min-width: 106px;"><i class="fa fa-link"></i> Password</span>
        <input type="password" class="form-control" name="pass" placeholder="Password">
    </div>
    <br>
        <button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
        <input type="submit" class="btn btn-success" value="Register" />
</form>

 And put it into its own file, call this file register_form.php.

 

Now in index.php on line 188 you'd include register_form.php instead of register.php

<?php include"register_form.php"; ?>

And then in place of the form in register.php you'd now include register_form.php

For some reason when I hit Register, it says:

 

Failed to run query: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'salt' in 'field list'

 

It is in my SQL database

Link to comment
Share on other sites

You sure you're using the correct database table in your queries?

 

In index.php for processing the login you're using the Members table but in register.php you're using the Users table in your queries? 

Woops, my mistake o: Forgot to change it xD

 

Thank you very much for your help. I really appreciate it :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.