简体   繁体   English

为什么我在本地使用 codeigniter 会收到 403 Forbidden 错误?

[英]Why am I getting a 403 Forbidden error using codeigniter locally?

I'm new to Database work and I'm wondering if anyone can help me understand why I keep getting a 403 Forbidden error when I'm simply developing locally.我是数据库工作的新手,我想知道是否有人可以帮助我理解为什么我只是在本地开发时不断收到 403 Forbidden 错误。 I'm using codeigniter to try to create a simple login program.我正在使用 codeigniter 尝试创建一个简单的登录程序。 The other day I had the program working on a different computer but now all I get on this machine is 403 errors when I try to open the "view" or "controller" files in the browser.前几天我让程序在另一台计算机上运行,​​但现在我在这台机器上得到的只是 403 错误,当我尝试在浏览器中打开“视图”或“控制器”文件时。 It must be some setting somewhere but I simply don't know where to look.它一定是某个地方的一些设置,但我根本不知道去哪里找。 Any ideas?有任何想法吗?

Here is my database.php file:这是我的 database.php 文件:

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'tester';
$db['default']['password'] = 'tester';
$db['default']['database'] = 'intranet';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Here is the controller file welcome.php:这是控制器文件welcome.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->view('welcome_message');
    }

    public function login_form()
    {
        $this->load->view('login_form');
    }

    public function login_submit()
    {
        print_r( $_POST );
        $this->load->model('Usermodel', 'users');
        $match = $this->users->authenticate_user( $_POST['email'], $_POST['password'] );
        if( $match )
        {
            echo "User exists in database!";
        }
        else
        {
            echo "Email or password is wrong, bretheren!";
        }
    }
}

And I added the line $autoload['libraries'] = array('database');我添加了行 $autoload['libraries'] = array('database');

I created a database in phpMyAdmin on MAMP to have the database and table as specified by the code so I'm confused why when I run the welcome.php file in the browser, I get a 403 error.我在 MAMP 上的 phpMyAdmin 中创建了一个数据库,以拥有代码指定的数据库和表,所以我很困惑为什么当我在浏览器中运行welcome.php 文件时,我收到 403 错误。 It's local so there shouldn't even be a problem with that right?它是本地的,所以应该没有问题吧? I must be missing a setting somewhere.我一定是在某处遗漏了一个设置。 Any ideas?有任何想法吗?

As mentioned in the comments you shouldn't be trying to directly access the .php file your controller resides in. Direct script access isn't allow which is why you are getting the 403. You need to send request through your index.php .正如评论中提到的,您不应该尝试直接访问控制器所在的.php文件。不允许直接访问脚本,这就是您收到 403 的原因。您需要通过index.php发送请求。 So rather than using this URL所以而不是使用这个 URL

localhost/intranet/CodeIgniter_2.1.4/application/controllers/welcome.php

You need to use你需要使用

localhost/intranet/CodeIgniter_2.1.4/welcome //Use this if your .htaccess removes index.php
localhost/intranet/CodeIgniter_2.1.4/index.php/welcome

Or to access your login form或访问您的登录表单

localhost/intranet/CodeIgniter_2.1.4/welcome/login_form //Use this if your .htaccess removes index.php
localhost/intranet/CodeIgniter_2.1.4/index.php/welcome/login_form

如果有 htaccess 文件更改被拒绝授予或完全删除它们

This happened to me.这发生在我身上。 And i couldn't solve it with above answers.我无法用上面的答案解决它。

I tried to change the folder CodeIgniter-3.1.4 Access (Others) to Access files and now i can see the index.php file.我尝试将文件夹 CodeIgniter-3.1.4 Access (Others) 更改为 Access 文件,现在我可以看到 index.php 文件。

I haven't tried anything more.我没有再试过。 Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM