Welcome Guest, Not a member yet? Register   Sign In
Redirection problem
#1

[eluser]Unknown[/eluser]
First, I'm using the latest CI and Bootstrap/Boilerplate from Arnodo.

I'm trying to get a login page up and running and not sure if I missed something or what. Whenever I try to go to /login if gives me a page not found. I do have login.php created in the view though. Maybe someone could be so kind to look over my code and maybe tell me something I may have missed.

controller/users.php
Code:
<?php

/**
*
*/
class Users extends MY_Controller
{
public function index()
{

  $this->title = "Login";
  $this->keywords = "arny, arnodo";
  
  $this->_render('pages/users');

}

function login()
{
  $data['error']=0;
  if($POST) {
   $this->load->model('user');
   $username=$this->input->post('username',true);
   $password=$this->input->post('password',true);
   $user=$this->user->login($username,$password);
   if(!$user) {

    $data['error']=1;
   }
  } else {
   $this->session->set_userdata('user_id',$user['user_id']);
   $this->session->set_userdata('account_type',$user['account_type']);
   redirect(base_url().'login');
  }

  $this->_render('pages/login');
}

function logout()
{

  $this->session->sess_destroy();
  redirect(base_url().'home');
}
}

model/user.php
Code:
<?php

class User extends MY_Controller
{

function create_user($data)
{
  $this->db->insert('users',$data);
}

function login($username,$password)
{
  $where=array(
  'username' => $username,
  'password' => sha1($password)

   );

  $this->db->select()->from('users')->where($where);
  $query=$this->db->get();
  return $query->first_row('array');

}

}

views/pages/login.php
Code:
<h1>Login</h1>
&lt;?php if($error==1) { ?&gt;
<p>Your username/password did not match.</p>
&lt;?php } ?&gt;
&lt;form acti method="post"&gt;
<p>Username: &lt;input type="text" name="username" /&gt;&lt;/p>
<p>Password: &lt;input type="text" name="password" /&gt;&lt;/p>
<p>&lt;input type="submit" value="Login" /&gt;&lt;/p>
&lt;/form&gt;

I'm going off the Tuts+ CodeIgniter essentials. Thanks in advance.
#2

[eluser]PhilTem[/eluser]
1) Your URI must be users/login i.e. www.example.com/users/login

2) It must be
Code:
if ( $_POST ) // see the underscore?
// or even better
if ( $this->input->post() )
instead of
Code:
if ( $POST )

3) Use base_url with the URI as an argument, i.e.
Code:
base_url('home'); // creates www.example.com/home

4) Maybe you want to consider using site_url() instead of base_url() since it will always work. If you don't want to have index.php in your URI, then read into the user's guide on how to remove it.

5) You would want to check for at least one row of your query before returning $query->first_row('array'); otherwise you will get errors once there is no record matching row Wink

Just my 5ct. to helping you Wink




Theme © iAndrew 2016 - Forum software by © MyBB