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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23087195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:29:10  来源:igfitidea点击:

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

phpmysqlcodeignitermamphttp-status-code-403

提问by Rambo8000

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. I'm using codeigniter to try to create a simple login program. 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. It must be some setting somewhere but I simply don't know where to look. Any ideas?

我是数据库工作的新手,我想知道是否有人可以帮助我理解为什么我只是在本地开发时不断收到 403 Forbidden 错误。我正在使用 codeigniter 尝试创建一个简单的登录程序。前几天我让程序在另一台计算机上运行,​​但现在我在这台机器上得到的只是 403 错误,当我尝试在浏览器中打开“视图”或“控制器”文件时。它一定是某个地方的一些设置,但我根本不知道去哪里找。有任何想法吗?

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. It's local so there shouldn't even be a problem with that right? I must be missing a setting somewhere. Any ideas?

我在 MAMP 上的 phpMyAdmin 中创建了一个数据库,以拥有代码指定的数据库和表,所以我很困惑为什么当我在浏览器中运行welcome.php 文件时,我收到 403 错误。它是本地的,所以应该没有问题吧?我一定是在某处遗漏了一个设置。有任何想法吗?

回答by Pattle

As mentioned in the comments you shouldn't be trying to directly access the .phpfile 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. So rather than using this URL

正如评论中提到的,您不应该尝试直接访问.php控制器所在的文件。不允许直接访问脚本,这就是您获得 403 的原因。您需要通过您的index.php. 所以而不是使用这个 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

回答by Atul Kumar

If there are htaccess files change deniedto grantedor just remove them entirely

如果有htaccess中改变拒绝授予或只是删除它们完全

回答by Memristor

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.

我没有再试过。希望能帮助到你。