-1

This is my first time using CodeIgniter and I am trying to create a Log In form. Whenever I click on the submit button that I have created, it brings me to a 404 Page Not Found Error.

I have made a view_login.php in the view folder with this code:

<?php echo form_open('user/login') ?>
<ul>
<li>
<label> username </label>
<div>
<?php echo form_input(array('id' => 'username', 'name' => 'username')); ?>
</div>
</li>
<li>
<label> password </label>
<div>
<?php echo form_password(array('id' => 'password', 'name' => 'password')); ?>
</div>
</li>
<li><?php echo validation_errors(); ?></li>
<li>
<?php echo form_submit(array('name' => 'submit'), 'login'); ?>
</li>

This is my user.php controller:

<?php
class User extends CI_controller
{

public function __construct()
{
    parent::__construct();
    $this->load->model('user_model');
}


public function register()
{
$this->load->library('form_validation');


if($this->form_validation->run() == FALSE)
{
    $this->load->view('templates/header', $data);   
    $this->load->view('user/view_register');
    $this->load->view('templates/footer');
}

else
{
echo 'this is being processed. thank you'; 
}   

    $this->load->view('user/view_register');
}   

public function login()
{
$this->load->library('form_validation');

$data['title'] = 'Log In';

 $this->load->view('user/view_login'); 

 if ($this->form_validation->run() == FALSE)
 { 
    $this->load->view('templates/header', $data);   
    $this->load->view('user/view_login');
    $this->load->view('templates/footer');
 {
 else
 {
     $user_id = $this->User_model->check_login($this->input->post('username',
                $this->input->post('password');

Here is my User_model:

class User_model extends Model {

function User_model()
{
    parent::Model();
}

function check_login($username, $password)
{

$sha1_password = sha1($password);

$query_str = "SELECT user_id FROM users WHERE username = ? and password = ?";

$result = $this->db->query($query_str, array($username, $sha1_password));

if ($result->num_rows() ==1)
{
    return $result->row(0)->user_id;
}
else
{
    return false;
}

Any help would be immensely appreciated :)

Ben J Shapiro
  • 40
  • 1
  • 8

3 Answers3

1

I know you're going to delete your question eventually, as you did with your previous three (that I know of), but hey.

You're doing a lot of things wrong. Let's do some cleanup:

<?php  
class User extends CI_controller
{

// constructor is useless here, let's remove it.   

function index()
{
    redirect('login','refresh');
    // this is useless too, but just in case someone navigates to the url 'user'
}

function login()
{
    // you are calling form_validation methods, and loading the library _after_. 
    // It's the other way around.
    // Also, no need to load the form helper, it's already loaded
    // by the form_validation library

    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'required|trim|max_length[50]|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'required|trim|max_length[200]|xss_clean');

     $this->load->view('pages/view_login');

     if ($this->form_validation->run() == FALSE) {
         $this->load->view('view_login');
     {
     else {
        //extract($_POST);
        // this is quite bad practice imho (it's like goring back to register_globals).
        // You should fetch the post content directly:
         $user_id = $this->user_model->check_login($this->input->post('username',
                    $this->input->post('password');
      }    
  }
?>

Now, as I said in your previous questions, which all dealt with the SAME EXACT 404 ERROR, I'm going to tell you again: FOLLOW THE MANUAL. Don't invent new ways of using the built-in framework functions. They're there for a purpose.

You should open the form like this:

<?php echo form_open('user/login'); ?>

It will build the correct url by itself. And don't tell "I already did that" again, because you didn't, in fact every question of yours still shows the same mistakes in url building all over again.

I'm sorry to be harsh, I hope something will get grasped eventually.

Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • Just fixing all that you have mentioned, and dont worry about being harsh i could do with some constructive criticism.. – Ben J Shapiro Mar 12 '13 at 20:51
  • I took the index.php part out and it has brought a different 404 not found error up. do i need to change my base url? – Ben J Shapiro Mar 12 '13 at 20:52
  • +1 for correct form_open. Don't reference a full URL, just form_open(*controller*/*method*) – Devin Young Mar 12 '13 at 21:29
  • i've changed my form_open and i have cleaned up my code but there is no change – Ben J Shapiro Mar 12 '13 at 21:39
  • Updated damien, i have added some code in (the $this statements) to see if it helps but it didnt – Ben J Shapiro Mar 13 '13 at 00:01
  • Copy/pasting Damien's code isn't going to help you if you don't change things to match your own code. Do you have a Users model? If so, is it called "User_model"? Because that is what you are calling. If so, have you loaded it in your controller? It doesn't look like it. You are also missing a parenthesis in your "$user_id = " statement that Damien missed, showing that all you did was copy his code. – Devin Young Mar 13 '13 at 14:29
  • I havent copied any ones code. Yes i have a user model and i have called it in my controller with $this->load->model('User_model');. I will add the user_model into my question – Ben J Shapiro Mar 13 '13 at 14:43
0
function user()
{
    parent::Controller();
}

should be

function user()
{
    parent::__construct();
}

also you are loading

$this->load->view('pages/view_login'); 
and
$this->load->view('view_login');

check your code, sure these views exists both? also i really think you can remove all this part:

function index()
{
    $this->login();
}

also, you are using index.php in form open, are you removing that from url with htacces? 404 is page not found

itsme
  • 48,972
  • 96
  • 224
  • 345
  • all other URLS are working fine (in answer to your comment above ) – Ben J Shapiro Mar 12 '13 at 20:18
  • thanks, i changed this to construct but still no change, I deleted the last $this->load as well and nothing happened :( this is so annoying. it must be something to do with my login function? – Ben J Shapiro Mar 12 '13 at 20:23
  • @benshapiro are you sure you have *views/pages/view_login.php* ?? Also check inside application/config/config.php search for *$config['index_page'] = "index.php"* (it must be as i shown), also did you changed any other config file? – itsme Mar 13 '13 at 08:24
  • ofcourse I am sure, and yes my config index is set to index.php – Ben J Shapiro Mar 13 '13 at 12:13
0

This is where your problem could be. I suspect you are confusing your controller name with a subdirectory inside view:

$this->load->view('user/view_login'); 

Currently, You are loading the view_login.php from a folder/directory known as user. You get a 404 Page Not Found Error if page is not in the referenced directory. It does not exist.

I think this is some error, because user is actually your controller and should be used only from the form as:

<?php echo form_open('user/login'); ?>

except your have view_login.php existing in a subdirectory known as user.

view/
    user/
        view_login.php

Try putting view_login.php directly into the view folder and use this to call it:

view/
    view_login.php

$this->load->view('view_login');

In same vein,

$this->load->view('templates/header', $data);   
$this->load->view('user/view_login');
$this->load->view('templates/footer');

Also means that header.php and footer.php are in subdirectories "templates". An error with any of these would give you the 404 error. You might want to try commenting out your load view and test by echoing out some string -- just to prove your codes are all fine.

Verify if you might be having interference from your htaccess file by testing if every other url works well.

[UPDATE]

If you have doubts whether your form action is correct, try using base_url() if that is configured correctly.

<?php echo form_open(base_url().'user/login') ?>

That should accurately point to site/controller/method(function)

Like I mentioned in my comment, I didn't see you use form_validation->rule() anywhere in your code. You could possible check to see if it was false.

Replace your login function with the one below and test:

        public function login() {
        $this->load->library('form_validation');

        $data['title'] = 'Log In';
        if ($this->input->post()) {
            print_r(); //test if you have values

            $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

            //You could set delimiter

            if ($this->form_validation->run() == FALSE) {
                //What so ever you which to do if validation fails. { else {
                        $username = $this->input->post('username'); //Make your code a bit readable
                        $password = $this->input->post('password'); //Make your code a bit readable

                        $user_id = $this->User_model->check_login($username, $password);
                        $data["user_id"] = $user_id; //you can access $user_id from your view
                    }
                }
                //Pass $data to views, you might need it in more than one view. Also change directory from user to pages to match what you stated in comment below. And please make sure these views are in the directories specified below...

                $this->load->view('templates/header', $data);
                $this->load->view('pages/view_login', $data);
                $this->load->view('templates/footer', $data);
            }
Steward Godwin Jornsen
  • 1,181
  • 1
  • 7
  • 13
  • Thanks, im not sure if this helps but my view_login.php is located under views\pages\view_login. the user controller should be able to call this page though which is why i called it through user\viewlogin. – Ben J Shapiro Mar 13 '13 at 12:06
  • I took out the roots though like you said above and still no change, could it possibly be to do with my user_model.php? My view_login does work because the page loads perfectly with the form that i have created, except when i submit the form, nothing is being echoed – Ben J Shapiro Mar 13 '13 at 12:07
  • Then you are calling the view wrong. If your view is in subdirectory pages, then you should be using pages in your code and not user. I've added an update to my answer. Secondly you have a few things wrong with your code if it's the submit that is not working. Please see the Edit to see modifications to your codes assuming you have the view in `views\pages\view_login` Also, I can't find where you used the form_validation->rule(); – Steward Godwin Jornsen Mar 13 '13 at 14:12
  • Thanks, every other URL works fine but these dont: 'CodeIgniter/index.php/user/login' or 'CodeIgniter/index.php/user/register' .. I have moved my view_login and view_register into the views directory so they are not hidden under any other folder. do i need a public function for view? – Ben J Shapiro Mar 13 '13 at 14:37
  • Did you see my last update? How did you configure your codeigniter config? I don't understand what you mean by codeigniter/index.php/user/login. What's codeigniter doing there? shouldn't it be `http://localhost/yoursite/index.php/user/login` or `http://localhost/yoursite/user/login`? To remove index.php, please read: http://stackoverflow.com/questions/1445385/how-to-remove-index-php-in-codeigniters-path or http://ellislab.com/forums/viewthread/155801/ or http://stackoverflow.com/questions/9667226/remove-index-php-in-codeigniter-2-1-0 or http://net.tutsplus.com/tutorials/php/codeigniter-basics/ – Steward Godwin Jornsen Mar 13 '13 at 14:58
  • for now try limiting your Login function to just "$this->load->view('view_login');". If the view gets loaded, your error is not in the form path, but either the controller or model. – Devin Young Mar 13 '13 at 15:04
  • thanks @DevinYoung I have tried this numerous times but the view doesnt get loaded. my model is printed above in my question – Ben J Shapiro Mar 13 '13 at 15:14
  • @StewardGodwinJornsen when i remove index.php the problem still occurs 'The requested URL /CodeIgniter/user/register was not found on this server.' – Ben J Shapiro Mar 13 '13 at 15:15
  • Do i need to create a $route? in my routes.php file? – Ben J Shapiro Mar 13 '13 at 15:22
  • You shouldn't need to create a route for this. On an unrelated note, you are loading "user_model" but then calling it later as "User_model". Likely not the cause for this error, but will be the cause for a different one in the future. – Devin Young Mar 13 '13 at 15:30
  • I like to get things in steps. I want to know how you configured index.php in config. Are you using htaccess & is it working? Did you read responses from the links I provided above? From my answer, there is no obvious reason why your page is not showing up. Did you replace the function code with one I have above? Did you follow the advice of trying to echo or print_r? Does your user controller load without referencing login like `localhost/site/user`. Does that work? you must have `function index(){` For that one. Lastly, give me a pastebin of your codes to help spot the error after doing all – Steward Godwin Jornsen Mar 13 '13 at 16:06