Welcome Guest, Not a member yet? Register   Sign In
CI norms...checking user login, displaying errors
#1

[eluser]newbrand[/eluser]
Hi there,

Another silly question from a beginner. So this is a pretty common thing that I guess most people may experience. I've done it the same way for a long time, perhaps it's time to change it up.

On certain scripts I have a number of "responses" that are handled by the view script. What I typically do is set a variable to display a certain response, pass it to the view and let it figure out the rest.

I should note that I'm also using the Template Class that a user suggested on the forums.

Take a look:

Code:
<?php

class EditAccount extends Controller {

    function EditAccount()    { parent::Controller(); }
    
    function index()
    {    
        $this->load->library('form_validation');
        $this->load->library('template');
        $this->load->library('session');
        $view = array();
        
        if ($this->session->userdata('display') == "")
        {
            $view['notloggedin'] = true;
        } else {
            if (isset($_POST['register']))
            {

                
                if ($this->form_validation->run() == true)
                {
                    
                }
            }
        }
        $this->template->parse_view('content', 'editaccount', $view);
        $this->template->render();
    }
    
}

?>

Line 14, I'm checking if they are logged in. If they aren't I set a view variable and the view handles the readout. Else anything after line 18 performs the functions once the user is logged in. This is a more specific case, but generally I have a range of responses to give to the user. I categorise them as "errors", "success", and "warnings".

Here is a sample of a view script I have created:
Code:
<div id="register">
    &lt;?php
      if (isset($notloggedin)) {
          echo "<div id='error'>You are not logged in! Login to view this page.</div>";
      } else {
          if (isset($success))
          {
              echo "<div class=\"success\">{$success}</div>";
          } else {
     ?&gt;
    &lt;?php echo validation_errors(); ?&gt;
    &lt;form action="/editaccount/" method="POST"&gt;
        <h3>First Name</h3>
        &lt;input type="text" name="firstname" value="&lt;?php echo set_value('firstname'); ?&gt;"&gt;
        <h3>Last Name</h3>
        &lt;input type="text" name="lastname" value="&lt;?php echo set_value('lastname'); ?&gt;"&gt;
        <h3>Password</h3>
        &lt;input type="password" name="pass" value="&lt;?php echo set_value('pass'); ?&gt;"&gt;
        <h3>Confirm Password</h3>
        &lt;input type="password" name="passconfirm" value="&lt;?php echo set_value('passconfirm'); ?&gt;"&gt;
        <br><br>
        &lt;input type="submit" name="edit" value="Edit Account"&gt;
    &lt;/form&gt;
    &lt;?php } } ?&gt;
</div>
Basically checks for certain logics and continues to display the correct message. I don't really need this flexibility and I feel as if it may be wrong somehow. Anyone have a better method/suggestion.

Regards,

Dave




Theme © iAndrew 2016 - Forum software by © MyBB