Home > OS >  Log in Validation alert pop up even though i'm not login yet
Log in Validation alert pop up even though i'm not login yet

Time:09-16

I try to make an alert for invalid login so I use

$this->session->set_flashdata('error', 'Invalid Credentials');  
            redirect("admin/login");

and i put this in my index function

$data=[];
    if (isset($_SESSION['error'])) {

        $data['error']=$_SESSION['error'];
    }else{
        $data['error']="NO_ERROR";

    }
    
    //$this->load->helper('url');
    $this->load->view('adminpanel/loginview',$data);

but when i reload my login page, the alert was already execute, can anyone help me with this? this is my full controller

public function index()
{

    $data=[];
    if (isset($_SESSION['error'])) {

        $data['error']=$_SESSION['error'];
    }else{
        $data['error']="NO_ERROR";

    }
    
    //$this->load->helper('url');
    $this->load->view('adminpanel/loginview',$data);

}

function login_post(){
    //$this->load->helper('url'); set at autoload file
    // print_r($_POST); test the post result
     if (isset($_POST)) {
        $email=$_POST['email'];
        $password=$_POST['password'];

        $query = $this->db->query("SELECT * FROM `backenduser` WHERE `username`='$email' AND `password`='$password'");

        if ($query->num_rows()) {
            // credential are valid

            $result = $query->result_array();
                //echo "<pre>";
            //print_r($result); die();

            $this->session->set_userdata('user_id', $result[0]['uid']);

            redirect('admin/dashboard');

        }
        else{
            //invalid credentials
            $this->session->set_flashdata('error', 'Invalid Credentials');  
            redirect("admin/login");
        }

     }
     else{
        die("Invalid Input!");
     }
}

function logout(){
    session_destroy();
}

}

CodePudding user response:

Your problem is here:

 if (isset($_POST)) {
    ...
 }

This will always return true because $_POST is always available, so it will always show the alert. You can check if the request is post or not by:

if(count($_POST) > 0){
   ...
}

CodePudding user response:

also use this

if(!empty($_POST)){
   ...
}
  • Related