CodeIgniter tutorials for beginners Hindi me part-3

Hello dosto kaise hai aap sab? hope aapne login or register page ache se load karliya hoga. chalo aaj ham user registration with form validation pura karlete hai kya bolate ho? to chaliye shuru karte hai.

sabase pahale ham contoller me jake form validation rules set karlete hai jaise required, alfa numberic, password conf, xx filtering, words limit etc or bhi bahut default function ham form validation me use kar sakte hai aap sare function CodeIgniter user guide me check kar sakte hai https://codeigniter.com/userguide3/libraries/form_validation.html yaha sabse niche sare validation diye hue hai.

REGISTER CONTROLLER VALIDATION RULES

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Register extends CI_Controller {
	public function index()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('FullName', 'FullName', 'trim|required|alpha|htmlspecialchars|xss_clean');
		$this->form_validation->set_rules('Email', 'Email', 'trim|required|is_unique[users.Email]|htmlspecialchars|xss_clean',
		array('required' => 'You must provide a %s.','is_unique' => 'This %s already exists.'));
		$this->form_validation->set_rules('Password', 'Password', 'trim|required|min_length[5]|max_length[20]|htmlspecialchars|xss_clean');
		$this->form_validation->set_rules('passwordcnf', 'Password Confirmation', 'trim|required|matches[Password]|htmlspecialchars|xss_clean');
	
		if ($this->form_validation->run())
		{
			$FullName=$this->input->post('FullName');
			$Email=$this->input->post('Email');
			$Password=$this->input->post('Password');
			$pwd_hash=password_hash($Password,PASSWORD_BCRYPT);
			$this->load->model('registermodel');
			if($this->registermodel->add_user($FullName,$Email,$pwd_hash))
			{
				$id=$this->db->insert_id();//this function provide last created user id
				$newdata = array(
						'id'  => $id,
						'logged_in' => TRUE
					);
				$this->session->set_userdata($newdata);//on register sucsses insert session id 
				$this->session->set_flashdata('Reg_success','<p>Welcome '.$UserName.',<br>
			    Thankyou for join Hello world!');
				return redirect('login');
			}
			else{
				$this->session->set_flashdata('Reg_failed','<p>Some error occerd / try again!');
				return redirect('register');
			}
        }    
		else
		{
			$this->load->view('header');
			$this->load->view('register');
			$this->load->view('footer');
		}	
    }
    
    public function login()
	{
		$this->load->view('header');
		$this->load->view('login');
		$this->load->view('footer');
	}
}

are ye kya kardiya mene?? aap yahi soch rahe honge na.. dosto tensan na lo pura samjata me. sabase pahle hame CodeIgniter ki ak library load karani hai form validation fir form ke rules set kardo yaad rahe input me jo name diya hai vahi same name hame rules me set karna hai jaise name=”Email” hai to Email use karana hai fir usake liye rules jaise required , xss_clean sequrity ke liye htmlspecialcherector html input clean ke liye. rules set karke form validation run karana hai. ab form validation run hojaye to ham user ko register kara denge model se.. model ke bare me aage batata hu aapko. isase pahale ham error display kara dete hai register view me. jaise kisine blank rakh ke submit kardiya to error dikhana padega na jaise bhai ye bharo tab register karo hehe. chalo chalte hai register view me dosto

Register (View)

<!-- open form for form validation -->
<?php echo form_open('register'); ?>
    <!-- error message registration failed -->
    <?php  if($error=$this->session->flashdata('Reg_failed')):  ?>
        <div class="row">
            <div class="col-lg-12">
                <div class="alert alert-danger">
                    <?= $error; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>
<h1>Register form</h1>
<form>
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="inputEmail4">Full Name</label>
      <input type="text" class="form-control" name="FullName" placeholder="Full Name" value="<?php echo set_value('FullName'); ?>">
      <div class="text-danger"><?php echo form_error('FullName') ?></div> 
      <label for="inputEmail4">Email</label>
      <input type="email" class="form-control" name="Email" placeholder="Email" value="<?php echo set_value('Email'); ?>">
      <div class="text-danger"><?php echo form_error('Email') ?></div>
      
      <label for="inputPassword4">Password</label>
      <input type="password" class="form-control" name="Password" placeholder="Password" value="<?php echo set_value('Password'); ?>">
      <div class="text-danger"><?php echo form_error('Password') ?></div>
      <label for="inputPassword4">Password Cnf</label>
      <input type="password" class="form-control" name="passwordcnf" placeholder="Password" value="<?php echo set_value('passwordcnf'); ?>">
      <div class="text-danger"><?php echo form_error('passwordcnf') ?></div>
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Sign in</button>
</form>

<?php echo form_open(‘register’); ?> dosto ye form validation ke liye add karana hota hai

flash data session ka hi ek veriabale ye flash message desplay karne ke liye use kar sakte jaise register hogaya to usaka message display kara sakte failed pe fail ka error.

<?php echo set_value(‘FullName’); ?> dosto ye function kissi karan koi error aaya to user last input save rakhta hai. taki user ko dubara na likhana pade. agar koi bada form ho jisame 10 15 input box ho or hamane ye function use nahi kiya ho or koi error aagaya to sab clear hojayega user ko pura form fir se bharna padega hai na. aisa aapake sath bhi hua hoga na kahi bar. aisa hone per user form bharega nahi dubara or site close kardega. Or hamare liye to per user important hota hai to ye function jarur use kare form chota ho ya bada. User ko 1% bhi rework ka moka nahi dena chahiye hai na?

<?php echo form_error(‘Email’) ?> Dosto ye function bhi bahut kam ka chiz hai user ko batayega bhai aapko is box me kya karna hai kya nahi to ise bhi use kare. jaisa desplay horaha hai na errors waisa hoga dosto.

dosto ye sb function achhe se kam kar jaye to apana form validation run hojayega. isake bad hame user ka data email, full name, password encrypt karke store kara dena hai database me or user ko next page pe bhej dena hai session create karke usaka

dosto session lybrarry or form helper autoload me lod karle nahi to error aayega aapako.

autoload php

$autoload['libraries'] = array('database','session');
//yaha sessions load karana hai
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|	$autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url','form','security'); yaha form jaha hamne pehale url helper load karaya tha

for xss clean function you have to set config file update bellow this

$config['global_xss_filtering'] = TRUE;

database php (config)

$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'mydb',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);


dosto chaliye form validation run kara ke model se database me user data insert kara dete hai. ye bahut easy bana diya codeigniter ne. let’s creat Register model in model folder model also need first word capital yaad rakhane ki cize hai choti choti dosto.

Registermodel php (model)

<?php
class registermodel extends CI_Model{
    public function add_user($FullName,$Email,$pwd_hash)
    {
      $new_user_data = array(
        'FullName' => $FullName,
        'Email' => $Email,
        'Password' => $pwd_hash,
      );
      return  $this->db->insert('users',$new_user_data);
      
    }
}

login php (View)

<!-- error message registration failed -->
<?php  if($error=$this->session->flashdata('Reg_success')):  ?>
        <div class="row">
            <div class="col-lg-12">
                <div class="alert alert-success">
                    <?= $error; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>
<h1>Login Form</h1>
<form>
  <div class="form-group row">
    <div class="col-md-6">
       <label for="exampleInputEmail1">Email address</label>
       <input type="email" class="form-control" name="Email" aria-describedby="emailHelp" placeholder="Enter email">
       <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
       <label for="exampleInputPassword1">Password</label>
       <input type="password" class="form-control" name="Password" placeholder="Password">
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

lo dosto hogaya register user kitana simple hai na? CodeIgniter me database insert bhi kitana simple hai na lambi chodi insert quarries ko kitana short bana diya hai na. Dosto chote mote error aate hai coding me sb error ka solution hota hai bus google karlo error khatam. Bus aapko semicolon or double coat single coat ka dhyan rakhana hai ye mistek sabase jyada hoti hai.

or koi problem aaye to aap muje yaha comments kar sakte me aapako jarur solution bataunga

happy coddind and happy diwali aaj diwali hai 🙂