Codeigniter - the requested URL was not found on this server

依然范特西╮ 提交于 2019-12-11 11:27:30

问题


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";

回答1:


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




回答2:


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.




回答3:


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.




回答4:


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/



来源:https://stackoverflow.com/questions/13543779/codeigniter-the-requested-url-was-not-found-on-this-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!