1

For the last couple of days my frustration has grown to new heights. Im trying to do a simple login system in codeigniter and I cant get it to work properly.

when submitting the login form I get the following error:

The requested URL /wishlist/login_controller/login was not found on this server.

Here is the form:

<form method="post" action="<?php echo base_url()?>login_controller/login">
<input type="text"placeholder="E-mail" id="email" name="email">
<input type="password" placeholder="Password" id="password" name="password">
<input class="btn btn-primary span2" type="submit" id="sign-in" value="Sign In">
</form>

Here is my login function in login_controller:

class Login extends CI_Controller {


function login() {

    $data['error'] = 0;

    if ($_POST) {

        $this->load->model('user_model');
        $email = $this->input->post('email', true);
        $password = $this->input->post('password', true);
        $user = $this->user_model->login($email, $password);

        if (!$user) {
            $data['error'] = 1;
        } else {
            $this->session->set_userdata('userID', $user['userID']);
            $this->session->set_userdata('firstname',    $user['firstname']);

            redirect(base_url().'admin_controller');

        }
    }

    $this->load->view('home_view');
}

I'm using wampserver on localhost. Here is the url that it tries to access: localhost/wishlist/login_controller/login

Am I correct in assuming that the first part after base_url() is the controller and the second part is the function in that controller?

A couple of settings from the config folder.

$config['base_url'] = '';
$config['index_page'] = 'index.php';
$route['default_controller'] = "site_controller";
j0k
  • 22,600
  • 28
  • 79
  • 90
user927917
  • 23
  • 1
  • 2
  • 7
  • Do other pages work? How is your .htaccess set? – Green Black Nov 24 '12 at 17:41
  • the view loaded by the default controller works just fine. I havent edited my .htaccess file at all so its set to: Deny from all – user927917 Nov 24 '12 at 17:54
  • What 404 are you getting the Apache one, not styled, nothing or one that looks better (the CI one)? Here, it looks like it's CI's. If it were Apache's then the problem is with the server not CI, i.e. htaccess not configured correctly, index.php missing in the URL, etc. – jadkik94 Nov 24 '12 at 18:21
  • I can only see the problem of accessing wrong url, please review my answer below. – Pankaj Khairnar Nov 25 '12 at 07:50

4 Answers4

3

please rename your controller from

class Login extends CI_Controller {

to

class Login_Controller extends CI_Controller {

then you can do

<form method="post" action="<?php echo site_url('login_controller/login'); ?>">
<input type="text"placeholder="E-mail" id="email" name="email">
<input type="password" placeholder="Password" id="password" name="password">
<input class="btn btn-primary span2" type="submit" id="sign-in" value="Sign In">
</form>

then use this:

$config['base_url'] = 'http://localhost/wishlist/';
$config['index_page'] = '';

and htacees:

RewriteEngine On
RewriteBase /wishlist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wishlist/index.php/$1 [L]

if it's not enought switch config.php file at line:

| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string.  The default setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO'            Default - auto detects
| 'PATH_INFO'       Uses the PATH_INFO
| 'QUERY_STRING'    Uses the QUERY_STRING
| 'REQUEST_URI'     Uses the REQUEST_URI
| 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = 'AUTO';

i use AUTO maybe you need QUERY_STRING or REQUEST_URI option

itsme
  • 48,972
  • 96
  • 224
  • 345
  • @user927917 you didn't setted the base_url in your config file, i updated my answer, can you try that please – itsme Nov 24 '12 at 17:57
  • updated base_url in my config file to: http://localhost/wishlist/ but im still getting the requested URL was not found on this server – user927917 Nov 24 '12 at 18:13
  • @user927917 you should check your htaccess and remove the index.php from your config file i'll post it to the answer check – itsme Nov 24 '12 at 18:15
  • Tried removing the index.php file and edit the .htaccess file. Still no luck. I find it hard to believe that using a framework is suppose to be this complicated. With the amount of time and energy I have used on CI so far, it would have been faster to just write my code using "normal" php. – user927917 Nov 25 '12 at 15:50
  • @user927917 well this is strange, i'm sorry i tryed doing all was in my possibility to help you, and i'm sorry you still not fixing , but i think if you are newbie on CI you just have to keep a clean copy of it and just follow the user guide, you'll not have any problems, maybe it's a host or server problem ;) – itsme Nov 25 '12 at 16:06
  • I certainly appreciate your time and effort. Thank you very much – user927917 Nov 25 '12 at 18:11
1

Change your login() method to something like user_login as using the same name for class and method is not right if you are not using that method as a constructor .

Try to access your url like this

http://localhost/wishlist/index.php/login/user_login

as you have not change any stting and not added htaccess file your code should be accesible normally so codeiginter has a pattern of accessing url like

http://localhost/folder_name/index.php/controller_name/method_name

and it seems your accessing URL is not correct.

Pankaj Khairnar
  • 3,028
  • 3
  • 25
  • 34
  • Changed some things: form is now submitted to base_url()user_controller/login That controller has an index function which loads the main view. user_controller is also loaded via the routes.php file. However, the problem still persists... :( – user927917 Nov 25 '12 at 16:01
  • does your initial problem solved or not, which was accessing url and showing form in it – Pankaj Khairnar Nov 25 '12 at 16:13
  • Unfortunately my initial problem is remain. Getting the same error – user927917 Nov 25 '12 at 18:42
  • if you don't give action to your form it will get submitted to same method, you can test it by making
    this will work properly, change your method's name to something else than login lets assume your method name is user_login then try to access your url like http://localhost/wishlist/index.php/login/user_login and submit your form it will get submitted with the same method
    – Pankaj Khairnar Nov 25 '12 at 19:16
1

You have 2 problems at the same time:

  1. Your form points at 'login_controller', while your controller is called 'login' (this has already been answered).

  2. You must not have a method named in the same way as your controller.

    Such method (unless your class is within a namespace, but that's not the case with CodeIgniter) is executed as the class constructor.

Community
  • 1
  • 1
Narf
  • 14,600
  • 3
  • 37
  • 66
  • having method name login is not a problem, it will be consider as constructor, yes but this method will be called two times first while creating class, and once method is called using uri routing – Pankaj Khairnar Nov 24 '12 at 19:40
  • And that ain't a problem? Plus, at least the latest CodeIgniter version will call `Login::index()` instead of `Login::login()` if you try to do that. – Narf Nov 25 '12 at 00:35
  • Have renamed controller to user_controller and kept the name of the login function. That didnt solve the problem :( – user927917 Nov 25 '12 at 15:45
0

You call action="<?php echo base_url()?>login_controller/login"> when controller's name is "login" and method "login".

You need to call base_url('login/login'); and sure, set base_url in config.php, you have index_page setted.

Maybe you dont have .htaccess, then you need to call mysite.com/index.php/login/login/

itsme
  • 48,972
  • 96
  • 224
  • 345
kaseOga
  • 771
  • 10
  • 24