无法使用PHP和Codegniter启动会话

时间:2018-02-09 09:44:13

标签: php ajax codeigniter session

亲爱的; 我有启动会话的问题使用codegniter framwork尝试登录但我无法登录请尽可能帮助我

*****************这是我的控制器代码****************

public function login()
    {
        if($this->input->post("login"))
        {
            $this->load->library('form_validation');
            $this->form_validation->set_rules("username","UserName",'required|min_length[3]|max_length[30]|alpha');
            $this->form_validation->set_rules("password","password",'required|min_length[3]|max_length[30]');
            if ($this->form_validation->run())
            {
                $username=$this->input->post("username");
                $password=$this->input->post("password");
                $this->load->model("usersmodel");
                if($this->usersmodel->mainlogin($username,$password))
                {
                    $session_data=array(
                        'username' => $username
                    );
                    $this->session->set_userdata($session_data);
                    redirect(site_url("Users/dashboard"));


                }else{
                    $this->session->set_flashdata('error', 'Error With loGIN ');
                    redirect(site_url("Users/login"));
                }


            }else{
                //error for validation
            }

        }


        $data['pagetitle']="Log in";
        $this->load->view("users/login",$data);
    }
    public function dashboard()
            {
                if($this->session->userdata('username')==!'')
                {
                    echo"welcom";

                }
                $this->load->model("usersmodel");
                $this->load->view("template/admin/dashboard");
            }

***********************这是我的模特*****************

public function mainlogin($username,$password)
        {
            /*
            $this->db->select('*');
            $this->db->from("users");
            $this->db->where('username',$username);
            $this->db->where('password',$password);
            $sql=$this->db->get();
            return $sql->row();
            */
            $this->db->where('username',$username);
            $this->db->where('password',$password);
            $sql=$this->db->get('users');
            if($sql->num_rows() >0)
            {
                return true;
            }else{
                return false;
            }

********************这是我的观点*********************

<div class="container">
    <div id="container">

        <div class="col-lg-8">
            <div class="final"><label class="text-success"><?php  echo validation_errors()?></label></div>
<form  method="post" action="<?php echo site_url("Users/add")?>"  enctype="multipart/form-data">
    <div class="form-group">
        <label for="recipient-name" class="control-label">Full Name :</label>
        <input type="text" class="form-control"  name=" fullname" id="fullname" placeholder="please insert fullname" autocomplete="off" required="required">

    </div>

    <div class="form-group">
        <label for="recipient-name" class="control-label">UserName :</label>
        <input type="text" class="form-control"  name="username" id="username" placeholder="please insert user name" autocomplete="off" required="required" >
    </div>
    <div class="form-group">
        <label for="message-text" class="control-label">Password:</label>
        <input type="password" class="form-control"  name="password" id="password" placeholder="please insert yout password" autocomplete="new-password" required="required" >
        <i class=" showpass fa fa-eye fa-3" aria-hidden="true"></i>
    </div>
    <div class="form-group">
        <label for="recipient-name" class="control-label">Email :</label>
        <input type="text" class="form-control"  name="email" id="email" placeholder="please insert email" autocomplete="off" required="required" >
    </div>
    <div class="form-group">
        <label for="recipient-name" class="control-label">User Type :</label>
        <select class="form-control" name="groubid" >
            <option value="1">Administartor</option>
            <option value="0">User</option>
            <option value="2">Maker</option>
            <option value="3">cheker</option>

        </select>
    </div>
    <div class="form-group">
        <label for="exampleInputFile">Image :</label>
        <input type="file" name="image" id="image">

    </div>

    <button type="submit"   name="adduser" value="adduser" class="btn btn-primary">add</button>
</form>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:-1)

在控制器或自动加载文件中加载会话库:
$this->load->library('session');

答案 1 :(得分:-1)

在您的模型中替换此功能:

     function mainlogin($username,$password) {
            $this->db->select('*');
                $this->db->from("users");
                $this->db->where('username',$username);
                $this->db->where('password',$password);
                $sql=$this->db->get();

            if($sql->num_rows() == 1)
            {
                return $sql->result();
            }else{
                return false;
            }

        }