2017-11-03 12 views
-2

コードイグナイターで登録とログインシステムを作成しました。 登録後、すべてのユーザーデータがデータベースに保存されます。 私はログインフォームで電子メールとパスワードを使用しており、そのログインしたユーザーに関連するすべての詳細を表示する必要があります。CodeIgniterフォームでのデータ取得

私はいくつかのことを試みましたが、正確なものは得られませんでした。私はどのように進めるべきですか?

//Controller file 
 

 
<?php 
 
defined('BASEPATH') OR exit('No direct script access allowed'); 
 

 
class User extends CI_Controller{ 
 
\t function __construct() 
 
\t { 
 
    \t parent::__construct(); 
 
    \t $this->load->helper(array('form', 'url')); 
 
    \t $this->load->library(array('session', 'form_validation', 'email', 'upload')); 
 
    \t $this->load->database(); 
 
    \t $this->load->model('user_model'); 
 
     } 
 
    public function index() 
 
    { 
 
    $this->load->view('registration'); 
 
    } 
 

 
public function registration() 
 
    { 
 
    //validate input value with form validation class of codeigniter 
 
    $this->form_validation->set_rules('fname', 'First Name', 'required|regex_match[/^[A-Za-z]{2,15}+$/]'); 
 
    $this->form_validation->set_rules('lname', 'Last Name', 'required|regex_match[/^[A-Za-z]{2,15}+$/]'); 
 
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.email]'); 
 
    $this->form_validation->set_rules('phone', 'Phone Number', 'required|regex_match[/^[789]\d{9}$/]'); 
 
    $this->form_validation->set_rules('city', 'City', 'required|regex_match[/^[A-Za-z]{3,15}+$/]'); 
 
    $this->form_validation->set_rules('zip', 'Zip Code', 'required|regex_match[/^\d{6}$/]'); 
 
    $this->form_validation->set_rules('gender', 'Gender', 'required'); 
 
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[15]'); 
 
    $this->form_validation->set_rules('confirmpswd', 'Password Confirmation', 'required|matches[password]'); 
 
    $this->form_validation->set_rules('image', 'Image', 'callback_do_upload','required'); 
 

 
     //$this->form_validation->set_message('is_unique', 'This %s is already exits'); 
 
     if ($this->form_validation->run() == FALSE) 
 
     { 
 
      $this->load->view('registration', array('error' => ' ')); 
 
     } 
 
     else 
 
     { 
 

 
      $fname = $_POST['fname']; 
 
      $lname = $_POST['lname']; 
 
      $email = $_POST['email']; 
 
      $phone = $_POST['phone']; 
 
      $city = $_POST['city']; 
 
      $zip = $_POST['zip']; 
 
      $gender = $_POST['gender']; 
 
      $password = $_POST['password']; 
 
      $passhash = hash('md5', $password); 
 
      $image = $_FILES['image']['name']; 
 
      //call method for uploading image 
 
      $upload = $this->do_upload('image'); 
 
       
 

 
      //md5 hashing algorithm to decode and encode input password 
 
      //$salt  = uniqid(rand(10,10000000),true); 
 
     $saltid = md5($email); 
 
     $data = array('fname' => $fname, 
 
        'lname' => $lname, 
 
        'email' => $email, 
 
        'phone' => $phone, 
 
        'city' => $city, 
 
        'zip' => $zip, 
 
        'gender' => $gender, 
 
        'password' => $passhash, 
 
        'image' => $image); 
 

 
     if($this->user_model->insertuser($data)) 
 
     { 
 
     $this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> Registration Successful</div>'); 
 
        redirect(base_url().'user/login'); 
 
     } 
 
     else 
 
     { 
 
     $this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> 
 
     \t \t \t \t \t \t \t \t Something Wrong. Please try again ...</div>'); 
 
        redirect(base_url()); 
 
     } 
 
    } 
 
} 
 
    
 
public function do_upload($data) 
 
     {  
 
     
 
       $config['upload_path'] = './uploads/'; 
 
       
 
       $config['allowed_types'] = 'gif|jpg|png'; 
 
       $this->load->library('upload', $config); 
 
       /*$config['max_size']    = 1000000; 
 
       $config['max_width']   = 1024; 
 
       $config['max_height']   = 768;*/ 
 

 
       //base_url('uploads/') 
 
       $this->upload->initialize($config); 
 

 
       if (! $this->upload->do_upload('image')) 
 
       { 
 
         $error = array('error' => $this->upload->display_errors()); 
 
          
 
         
 
       } 
 
       else 
 
       { 
 
         $data = array('upload_data' => $this->upload->data()); 
 
       } 
 
       return $data; 
 
     } 
 
    
 

 
public function login() 
 
{ 
 
    $this->load->view('login'); 
 
} 
 
public function check_login() 
 
{ 
 
    $email = $_POST['email']; 
 
    $pass = hash('md5', $_POST['password']); 
 
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
 
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[15]'); 
 
    if($this->form_validation->run() == FALSE) 
 
    { 
 
     $this->load->view('login'); 
 
    } 
 
    else 
 
    { 
 
     $res = $this->user_model->check_user($email , $pass); 
 
     if(!empty($res)) 
 
     { 
 
      echo "you are registered"; 
 
      echo "<br>"; 
 
      echo "<br>"; 
 
      echo "your details are as follows: "; 
 
      echo "<br>"; 
 
      echo "<br>"; 
 
      echo "email:".$email; 
 
      echo "<br>"; 
 
     } 
 
     else 
 
     { 
 
      $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">email/password not found</div>'); 
 
     redirect(base_url().'user/login'); 
 
     } 
 
    } 
 
} 
 
    function setSession($userId,$userName) { 
 
     
 
     $userSession = array('userId'=>$userId, 
 
          'userName'=>$userName, 
 
          'loggedIn'=>TRUE); 
 
     $this->session->set_userdata($userSession); 
 
    } 
 
    public function logout() 
 
    { 
 
     $this->session->sess_destroy(); 
 
     redirect(base_url().'user/login', 'refresh'); 
 
    } 
 

 

 
}

//View File 
 

 
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <title>Registration</title> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <!-- Latest compiled and minified CSS --> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <style type="text/css"> 
 
    .form-box { 
 
     max-width: 500px; 
 
     position: relative; 
 
     margin: 5% auto; 
 
    } 
 
    </style> 
 
</head> 
 

 
<body> 
 
    <div class="wrapper"> 
 
    <div class="container"> 
 
     <div class="row"> 
 
     <div class="form-box"> 
 
      <div class="panel panel-primary"> 
 
      <div class="panel-heading text-center"> 
 
       <h3>Register</h3> 
 
      </div> 
 
      <?php 
 
         echo $this->session->flashdata('msg'); 
 
        ?> 
 
      <div class="panel-body"> 
 
       <div class="row"> 
 
       <div class="col-sm-12"> 
 

 
       </div> 
 
       </div> 
 
       <form action="<?php echo base_url('user/registration');?>" method="post" enctype="multipart/form-data"> 
 

 
       <div class="row"> 
 
        <div class="col-sm-6"> 
 
        <div class="form-group"> 
 
         <label class="control-label" for="fname">First Name</label> 
 
         <div> 
 
         <input type="text" class="form-control" id="fname" name="fname" placeholder="First Name" required=""> 
 
         <span class="text-danger"><?php echo form_error('fname'); ?></span> 
 
         </div> 
 
        </div> 
 
        </div> 
 
        <div class="col-sm-6"> 
 
        <div class="form-group"> 
 
         <label class="control-label" for="fname">Last Name</label> 
 
         <div> 
 
         <input type="text" class="form-control" id="lname" name="lname" placeholder="Last Name" required=""> 
 
         <span class="text-danger"><?php echo form_error('lname'); ?></span> 
 
         </div> 
 
        </div> 
 
        </div> 
 

 
        <div class="col-sm-12"> 
 
        <div class="form-group"> 
 
         <label class="control-label" for="email"> Email</label> 
 
         <div> 
 
         <input type="email" class="form-control" id="email" name="email" placeholder="Email" required=""> 
 
         <span class="text-danger"><?php echo form_error('email'); ?></span> 
 
         </div> 
 
        </div> 
 
        </div> 
 
        <div class="col-sm-6"> 
 
        <div class="form-group"> 
 
         <label class="control-label" for="phone">Phone Number</label> 
 
         <div> 
 
         <input type="text" class="form-control" id="phone" name="phone" maxlength="10" placeholder="Phone Number" required=""> 
 
         <span class="text-danger"><?php echo form_error('phone'); ?></span> 
 
         </div> 
 
        </div> 
 
        </div> 
 

 
        <div class="col-sm-6"> 
 
        <div class="form-group"> 
 
         <label class="control-label" for="city">City</label> 
 
         <div> 
 
         <input type="text" class="form-control" id="city" name="city" placeholder="City" required=""> 
 
         <span class="text-danger"><?php echo form_error('city'); ?></span> 
 
         </div> 
 
        </div> 
 
        </div> 
 

 
        <div class="col-sm-6"> 
 
        <div class="form-group"> 
 
         <label class="control-label" for="zip">Zip Code</label> 
 
         <div> 
 
         <input type="text" class="form-control" id="zip" name="zip" placeholder="Zip Code" required=""> 
 
         <span class="text-danger"><?php echo form_error('zip'); ?></span> 
 
         </div> 
 
        </div> 
 
        </div> 
 

 
        <div class="col-sm-6"> 
 
        <div class="form-group"> 
 
         <label class="control-label" for="gender">Gender</label> 
 
         <div> 
 
         <input type="radio" name="gender" value="male" id="gender" checked="true"> Male 
 
         <input type="radio" name="gender" value="female" id="gender"> Female 
 
         <input type="radio" name="gender" value="transgender" id="gender"> Transgender 
 
         <span class="text-danger"><?php echo form_error('gender'); ?></span> 
 
         </div> 
 
        </div> 
 
        </div> 
 
        <br> 
 
        <div class="col-sm-12"> 
 
        <div class="form-group"> 
 
         <label class="control-label" for="pswd">Password</label> 
 
         <div> 
 
         <input type="password" class="form-control" id="pswd" name="password" placeholder="Password" required=""> 
 
         <span class="text-danger"><?php echo form_error('password'); ?></span> 
 
         </div> 
 
        </div> 
 
        </div> 
 
        <div class="col-sm-12"> 
 
        <div class="form-group"> 
 
         <label class="control-label" for="cn-pswd">Confirm Password</label> 
 
         <div> 
 
         <input type="password" class="form-control" id="cn-pswd" name="confirmpswd" placeholder="Confirm Password" required=""> 
 
         <span class="text-danger"><?php echo form_error('confirmpswd'); ?></span> 
 
         </div> 
 
        </div> 
 
        </div> 
 
        <div class="form-group"> 
 
        <div class="row"> 
 
         <div class="col-sm-offset-5 col-sm-3 btn-submit"> 
 
         <button type="submit" class="btn btn-success">Register</button> 
 
         </div> 
 
        </div> 
 
        </div> 
 

 
        <div class="col-sm-12"> 
 
        <div class="form-group"> 
 
         Select image to upload:<br> 
 
         <input type="file" name="image" id="image"> 
 
         <br/><br/> 
 
        </div> 
 
        </div> 
 
       </form> 
 
       </div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</body> 
 

 
</html>

// Model file 
 

 
<?php 
 
defined('BASEPATH') OR exit('No direct script access allowed'); 
 
class User_model extends CI_Model 
 
{ 
 
    public function insertuser($data) 
 
    { 
 
    return $this->db->insert('candidates', $data); 
 
    } 
 
    public function verifyemail($key) 
 
    { 
 
    
 
     $this->db->where('md5(email)', $key); 
 
     return $this->db->update('candidates', $data); 
 
    } 
 
    public function check_user($email,$pass) 
 
    { 
 
    $sql = "SELECT id , fname FROM candidates where email = ? and password = ?"; 
 
    $data = $this->db->query($sql, array($email,$pass)); 
 
     return ($data->result_array()) ; 
 
    } 
 

 
    
 
     // Function To Fetch Selected Record 
 
public function show_user_id($data){ 
 
     $this->db->select('*'); 
 
     $this->db->from('candidates'); 
 
     $this->db->where('email', $data); 
 
     $q=$this->db->get('candidates'); 
 
     return $q->row_array(); 
 
     
 
     } 
 
     // Update Query For Selected Student 
 
public function update_user_id1($id,$data){ 
 
     $this->db->where('email', $id); 
 
     $this->db->update('candidates', $data); 
 
     } 
 
} 
 

 
?>

答えて

0
// While registering time your are using md5 encryption for password but while 
// retrieving time you are passing password without encryption formate 
$data = $this->db->query($sql, array($email,$pass)); 
// Use this in your model 
$data = $this->db->query($sql, array($email,hash('md5', $pass))); 
+0

@Ayyappa amaraありがとうございますが、今は電子メール/パスワードが見つからないことを伝えています。あなたの提案を実装した後、変更箇所がどこにないのか教えてください。 – Abhi

+0

あなたのコードをチェックしましたが、エラーはありません。 "メール/パスワードが見つかりません" - >正しいemaiidとパスワードを入力していないことを意味します。新しいメールIDとパスワードで登録してからログインしてください。 –

関連する問題