简体   繁体   中英

Login Process Variable Not Set

So I am having issues at current with my login page where I am trying to get the login process to return a variable so I can display a error code on my login page but cannot see where I am going wrong...

PHP Error when typing wrong login credentials

Notice: Undefined variable: output in C:\\inetpub\\wwwroot\\reveal\\store\\qadmin\\login\\login.process.php on line 27

Login Form

<div class="widget-body">

    <!-- Form -->
<?php

    if(isset ($loginerror)) {

        ?>

            <div style="width:100%; text-align:center;">
                <span style="padding-bottom: 10px; color:red;">Successfully Logged Out</span>
            </div>

        <?php

    } else {

    ?>

        <span>Either No Login Data or something is horribly wrong.</span><br /><br />

    <?php

    };

?>
<form method="POST" action="index.php">
    <label>Username or Email</label>
    <input id="username" type="text" name="username" class="input-block-level" placeholder="Your Username or Email address"/> 
    <label>Password</label>
    <input id="password" type="password" name="password" class="input-block-level margin-none" placeholder="Your Password" />
    <div class="separator bottom"></div> 
    <div class="row-fluid">
        <div class="span8">
            <div class="uniformjs"><label class="checkbox"><input type="checkbox" value="remember-me">Remember me</label></div>
        </div>
        <div class="span4 center">
            <button class="btn btn-block btn-inverse" id="submit" name="submit" type="submit">Sign in</button>
        </div>
    </div>
</form>
<!-- // Form END -->

Index.php

session_start(); 

global $error;

// Simple check if the user is logged in or not
if (isset($_POST['submit'])) {
    include("login.process.php");

    $checkUser = login($_POST['username'], $_POST['password']);

    if ($checkUser == "No") {


        $error = "true";

    } else if (is_array($checkUser)) {

        $_SESSION['user'] = $checkUser;
        $_SESSION['loggedin'] = true;

    }
}

if (isset($_SESSION['loggedin'])) {

    include ("home.php");

}

if (!isset($_SESSION['loggedin'])) {
    include ("login.php");
}

Login.process.php

function login($username, $password) {

            include("../config.php");

            // Open up a new MySQLi connection to the MySQL database
            mysql_connect($dbHost, $dbUsername, $dbPassword);
            mysql_select_db($dbTable);

            // To protect MySQL injection (more detail about MySQL injection)

            $username1 = stripslashes($username);
            $password1 = stripslashes($password);
            $username2 = mysql_real_escape_string($username1);
            $password2 = mysql_real_escape_string($password1);
            $password3 = md5($password2);
            $sql="SELECT * FROM `users` WHERE username='$username2' AND password='$password3'";
            $result=mysql_query($sql);

            if ($result > 0) {

                while($logincheck = mysql_fetch_array($result)) {
                $output = array('username' => $username2, 'uid' => $logincheck['id'], 'superadmin' => $logincheck['superadmin']);
                };

                return $output;

            } else {

                $loginerror = "Login Error";
                return $loginerror;

            };
}

I am probably doing something stupid but cannot work out why it is returning that error!!!!!

So may be for some reason your query does not work and therefore never set $output

All you need to do is to set your $output before the loop, so in case if you loop doesn't works you have $output already set as an array()

if (mysql_num_rows($result) > 0) {

    $output = array(); // here is the trick

    while($logincheck = mysql_fetch_array($result)) {
        $output = array('username' => $username2, 'uid' => $logincheck['id'], 'superadmin' => $logincheck['superadmin']);
    };

    return $output;

} else {

    $loginerror = "Login Error";
    return $loginerror;

};

You got this error because there is not any record on your table when have wrong login credentials. So Just change your if condition to count number of rows in result to remove the error. Login.process.php

if (mysql_num_rows($result) > 0) {

Instead of:

if ($result > 0) { 

try this

function login($username, $password) {

            include("../config.php");

            // Open up a new MySQLi connection to the MySQL database
            mysql_connect($dbHost, $dbUsername, $dbPassword);
            mysql_select_db($dbTable);

            // To protect MySQL injection (more detail about MySQL injection)

            $username1 = stripslashes($username);
            $password1 = stripslashes($password);
            $username2 = mysql_real_escape_string($username1);
            $password2 = mysql_real_escape_string($password1);
            $password3 = md5($password2);
            $sql="SELECT * FROM `users` WHERE username='$username2' AND password='$password3'";
            $result=mysql_query($sql);

            if ($result > 0) {

$output=array();
                while($logincheck = mysql_fetch_array($result)) {
                $output = array('username' => $username2, 'uid' => $logincheck['id'], 'superadmin' => $logincheck['superadmin']);
                };

                return $output;

            } else {

                $loginerror = "Login Error";
                return $loginerror;

            };
}

Process of login

public function index()
{   
if($this->input->post())
    {
        $this->form_validation->set_rules('user_email', 'Email Address', 'required|valid_email');
        $this->form_validation->set_rules('user_password', 'Password', 'required');
        if ($this->form_validation->run() == TRUE)
        {
            $post=$this->input->post();
            $clean = $this->security->xss_clean($post);
            $check=$this->CM->userAuthentication($clean);
             if($check!='success')
              { 
                $this->session->set_flashdata('msg',array('message'=> "$check",'class' =>'alert alert-danger'));
                redirect('login');
              }else
                {
                    redirect('dashboard');
                }
        }else
        {
            $this->load->view('sign-in');
        }
    }else
    {
        $this->load->view('sign-in');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM