簡體   English   中英

使用codeigniter成功登錄后如何顯示首頁?

[英]how to display home page after successfull login with codeigniter?

我嘗試過redirect()$this->load->view('target_page')但是對我來說沒有成功,所以請幫我解決這個問題:

我的控制器在這里:

class Login_control extends CI_Controller {    
public function index() {
$this->load->model('login_model'); 
$this->load->helper('url');
if(isset($_POST['Logusername'])|| isset($_POST['Logpassword']))
{
$user = $_POST['Logusername'];
$pass = $_POST['Logpassword'];
$data = $this->login_model->login1($user,$pass);
if($data > 0 )
{
 echo '<font color="#00FF00">'. "OK".'</font>';     
$this->load->view('testing',$data);                   
 }
else 
{ echo  '<font color="#FF0000">'. "Login Failed ! Username or Password is Incorrect.".'</font>' ;                     
}  
exit();            
}  
$this->load->view('signup_view');
}
}

試試這個代碼。 您在重定向之前已寫echo ,因此它可能無法工作。

class Login_control extends CI_Controller{    
public function index()
{
$this->load->model('login_model'); 
$this->load->helper('url');
if(isset($_POST['Logusername'])|| isset($_POST['Logpassword']))
{
$user = $_POST['Logusername'];
$pass = $_POST['Logpassword'];
$data = $this->login_model->login1($user,$pass);
if($data > 0 )
{
    redirect('testing');                   
 }
else 
{ echo  '<font color="#FF0000">'. "Login Failed ! Username or Password is Incorrect.".'</font>' ;                     
}  
exit();            
}  
$this->load->view('signup_view');
}
}

嘗試這個

在控制器中

class Login_control extends CI_Controller{    
    public function index()
    {
        $this->load->model('login_model'); 
        $this->load->helper('url');

        if(isset($_POST['Logusername']) && isset($_POST['Logpassword'])) # Change || to &&
        {
            $user = $_POST['Logusername'];
            $pass = $_POST['Logpassword'];

            $data = $this->login_model->login1($user,$pass);

            if($data == 1){ # check is there only one user
                echo '<font color="#00FF00">OK</font>';     
                $this->load->view('testing',$data);                   
            }
            else{ 
                echo  '<font color="#FF0000">Login Failed ! Username or Password is Incorrect.</font>' ;                     
            }             
        }  
        $this->load->view('signup_view');
    }
}

在模型中

public function login1($user,$pass)
{
    $query = $this->db->query("SELECT * FROM user WHERE username= '$user' AND password = '$pass' ");
    $result = $query->result_array();

    $count = count($result); # get count of result
    return $count; # return count to controller 
}

當您在Codeigniter中進行開發時,更好的用戶可以使用其內置方法來獲取和發布數據。 為了進行身份驗證,您可以創建一個庫,該庫將自動加載並檢查是否有類似於userid的會話,如果未找到,則將用戶重定向到登錄頁面。 您可以在該庫中創建一個數組,該數組將基於阻止用戶訪問已認證頁面的基礎定義公共/已認證頁面。 您可以為Controller嘗試以下操作:

class Login_control extends CI_Controller
{

    public function index()
    {
        $this->load->model('login_model');
        $this->load->helper('url');
        $Logusername = $this->input->post('Logusername', true);
        $Logpassword = $this->input->post('Logpassword', true);
        if (!empty($Logusername) && !empty($Logpassword)) {
            $user = $Logusername;
            $pass = $Logpassword;
            $data = $this->login_model->authenticate($user, $pass);
            if ($data == TRUE) {
                /* User this flashdata to display the message after redirect */
                $this->session->set_flashdata('success', 'Logged in successfully');
                redirect(site_url("dashboard"));
            } else {
                /* User this flashdata to display the message after redirect */
                $this->session->set_flashdata('success', 'Wrong Username/password');
                redirect(site_url());
            }
        }
        $this->load->view('signup_view');
    }
}

對於模型

public function authenticate($Logusername, $Logpassword)
{
    $select_col = "iUserId";
    $where = "`vUserName` =?"; //*
    $where.=" AND BINARY `vPassword`=?";
    $sql = "SELECT " . $select_col . " FROM user WHERE " . $where . "";
    $result = $this->db->query($sql, array($Logusername, $Logpassword))->result_array();
    if (is_array($result) && count($result) > 0) {
        /* Create Session and store userid */
        $this->session->set_userdata("iUserId", $result[0]["iUserId"]);
        return TRUE;
    } else {
        return FALSE;
    }
}

有很多方法可以驗證用戶身份。 檢查Codeigniter中的鈎子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM