• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with login function

#1
[eluser]jrad[/eluser]
I'm having problems with my login in CI. I'm really in dire need of help to check on my custom login function. Here's part of the code:

Code:
function login()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
            
        if ($username = '' || $password = '')
        {
            $this->session->set_flashdata('message', 'Username and Password required.');
            $data['apptitle']         = $this->appTitle;
            $data['header_title']        = $this->appTitle;
            $data['header_subtitle']    = "Doña Ramona, Isabela City, Basilan Province";
            $this->load->view('entry_view', $data);
        }
        }

If I try to send an empty form I get a 404 page, which should not be the case because I intend to redirect back to the login page.

#2
[eluser]Sumon[/eluser]
what's your form action and please post that controller(where you check and redirect)

#3
[eluser]jrad[/eluser]
Here is the form action in my view:

Code:
<?= form_open('main_controller/login'); ?>
            <table>
                <tbody>
                    <tr>
                        <td colspan='2'>
                            <div id="message">&lt;?= $this->session->flashdata('message'); ?&gt;</div>
                        </td>
                    </tr>
                    <tr>
                        <td width="35%" height="27">&lt;?= form_label('Username', 'username'); ?&gt;</td>
                        <td width="65%">&lt;?= form_input('username').br(); ?&gt;</td>
                    </tr>
                    <tr>
                        <td width="35%" height="27">&lt;?= form_label('Password', 'password'); ?&gt;</td>
                        <td width="65%">&lt;?= form_password('password').br(); ?&gt;</td>
                    </tr>
                    <tr>
                        <td width="35%" height="27"></td>
                        <td width="65%" align="left">&lt;?= form_button('submit', 'Sign In'); ?&gt;</td>
                    </tr>
                    <tr height="5"></tr>
                </tbody>
            </table>
            &lt;?= form_close(); ?&gt;

and here's my controller:

Code:
class Main_controller extends Controller {
    
    var $appTitle;
    var $datestring;
    var $time;
    var $logdate;
    
    function Main_controller()
    {
        parent::Controller();    
        $this->appTitle     = "XXX";
    }
    
    function index()
    {
        if ($this->session->userdata('status') == 'logged_in')
        {
            redirect('appmain_controller/index');
        }
        
        $data['apptitle']         = $this->appTitle;
        $data['header_title']        = $this->appTitle;
        $data['header_subtitle']    = "XXX";
        $this->load->view('entry_view', $data);
    }

    function checkuser($username='', $password='')
    {
        $this->load->model('main_model');
        $query = $this->main_model->fetchData();
        
        return $query;        
    }
    
    function login()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
            
        if ($username = '' || $password = '')
        {
            $this->session->set_flashdata('message', 'Username and Password required.');
            $data['apptitle']         = $this->appTitle;
            $data['header_title']        = $this->appTitle;
            $data['header_subtitle']    = "XXX";
            $this->load->view('entry_view', $data);
        }
        else
        {
            if ($this->checkuser($username, $password) == TRUE)
            {
                $this->load->helper('date');
                $datestring = "%m %d, %Y";
                $time = time();    
                $logdate = mdate($datestring, $time);
                $session_data = array('username' => $username,'status' => 'logged_in','logdate' => $logdate);
                $this->session->set_userdata($session_data);
                $this->db->insert('logdata', $session_data);
                redirect('appmain_controller/index');
            }
            elseif ($this->checkuser($username, $password) == FALSE)
            {
                $this->session->set_flashdata('message', 'Login error found.');
                redirect('main_controller/index');
            }
        }
    }

    function logout()
    {
        $this->session->sess_destroy();
        redirect('main_controller/index');
    }
}

I tried submitting an empty form but rather than redirect back to the original login form i get a 404 page error.

#4
[eluser]missionsix[/eluser]
Sounds like you've set the form_open() parameter to go to the wrong place.

#5
[eluser]Sumon[/eluser]
would you please try once with
Code:
&lt;form id="something" action="&lt;?php base_url()?&gt;main_controller/login"&gt;
instead of
Code:
&lt;?= form_open('main_controller/login'); ?&gt;

Please have a look the view html what's the action actually generated by the form.

#6
[eluser]jrad[/eluser]
I've sold the problem on redirects. Now if I submit an empty form it will redirect back to the login page then show the flashdata.

I had to edit my .htaccess file because the application is within a folder in the root folder.

Then I had to tinker on my model and controller codes to remove other errors.

At least for now that problem has been solved. I'll try to make a register, delete, and edit users functions to complete the user authentication.

Thanks for the help though! I'm sure to be back here and ask for some information.

By the way, I will be presenting the Codeigniter Framework on the Software Freedom Day on September here in our place. A brief overview of the framework for an audience of college students, with a sample project for them to be able to understand the framework.

I hope it would get their interest to try and use CI.

#7
[eluser]RonPerrella[/eluser]
Jrad, shouldn't you be using equality (==) instead of assignment (=) in your if() statement?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.