CodeIgniter Forums
Optimizing login form - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Optimizing login form (/showthread.php?tid=58427)



Optimizing login form - El Forum - 06-11-2013

[eluser]Unknown[/eluser]
I am learning CI, and I have a doubt, i want to optimizate the code in the login, because i have a lot of pages, where if you are not logged, the login form will appear.

The example of the tutorial:

Controller:
Code:
<?php

class Form extends CI_Controller {

function index()
{
  $this->load->helper(array('form', 'url'));

  $this->load->library('form_validation');

  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');
  $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
  $this->form_validation->set_rules('email', 'Email', 'required');

  if ($this->form_validation->run() == FALSE)
  {
   $this->load->view('myform');
  }
  else
  {
   $this->load->view('formsuccess');
  }
}
}
?>


View:

Code:
<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('form'); ?>

<h5>Username</h5>
&lt;input type="text" name="username" value="" size="50" /&gt;

<h5>Password</h5>
&lt;input type="text" name="password" value="" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="" size="50" /&gt;

<h5>Email Address</h5>
&lt;input type="text" name="email" value="" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

This work fine, but i want to optimizate this for multiple page's with a option of login, the tipical .php View:

Code:
&lt;?php
     $sid = session_id();
     if($sid) {?&gt;
      <div class="blabla">
       &lt;!-- HTML --&gt;
      </div>&lt;?php
     }
     else {?&gt;
    
      
      
      <div id="login">
       <h3 class="hr_divider">Member Login</h3>
       &lt;?php
        echo validation_errors();
       ?&gt;
       &lt;?php
        echo form_open('form');
       ?&gt;
      
        <label for="username">Username:</label> &lt;input type="text" name="username" value="" size="50" /&gt;
        <label for="email">Password:</label> &lt;input type="text" name="password" value="" size="50" /&gt;
        <div class="cleaner_h10"></div>
        &lt;input type="submit" value="Submit" /&gt;
       &lt;/form&gt;
      </div>&lt;?php
     }
    ?&gt;

I search in forum, and understand the MVC model, i can't call Controllers from controllers or views (or is not right). The option is a library or helper, but i dont know how i can do it. I wanna save the name of variables, and dont put in all controllers this:

Code:
$this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');

Thanks!!

PD: Sorry the english Sad


Optimizing login form - El Forum - 06-12-2013

[eluser]Pert[/eluser]
You can load subviews in your view files, so you can put your login form check in to <b>application/views/login-box.php</b> and in your view files you can do this:

Code:
<strong>some html here</strong>

&lt;? $this->load->view('login-box') ?&gt;

<p>More html here</p>



Optimizing login form - El Forum - 06-12-2013

[eluser]Unknown[/eluser]
Thanks for the reply!

But i have a doubt about that... Ok, i can put a subView into mi View, but i have to put a Controller, with the conditions of validation.

Example:
Code:
$this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');

And i don't want put this lines in all controllers of application.


Optimizing login form - El Forum - 06-13-2013

[eluser]Pert[/eluser]
Sure. Usually you have one page or controller that deals with login.

So lets say you are on "About us" page. The login form on that page will always post to login page url
Code:
&lt;form action=" mycontroller/login " &gt;
...
&lt;/form&gt;

When user enters login details, it is always sent to single controller, if login details were wrong, the controller shows new form with error message on the on it's own.

To make things more user friendly / more complicated for you, you can, after successful login redirect users back to the original page with either using <b>$_SERVER</b> referrer or adding hidden input field with URL to a the form.