使用 SSL 的 codeigniter 文件夹结构

发布于 2024-11-02 22:14:44 字数 266 浏览 1 评论 0原文

大家好,正在开发一个提供公共广告和私人(安全)访问的 CI 应用程序。我之前没有实现过 SSL,但我知道设置的一部分是指定应使用 https 访问哪些文件夹。

我想要一些关于如何构建文件夹以促进这一点的建议。

设置仅影响控制器文件夹吗?换句话说,我应该将我的应用程序控制器拆分为标准 CI 控制器文件夹下的公共子文件夹和安全子文件夹吗?

我需要对我的视图和模型文件夹执行任何操作吗?还有什么我应该注意的吗?

我们将不胜感激您的帮助。

谢谢

Hey folks, working on a CI app that provides public ad well as private (secure) access. I have not implemented SSL before but i understand that part of the setup is specifying which folders should be accessed using https.

I would like some advice with regards to how i should structure my folders to facilitate that.

Does the setup only affect the controllers folder? in other words should I split my app controllers between a public and secure subfolders under the standard CI controllers folder?

Do I need to do do anything to my views and models folders? anything else I should be paying attention to?

Your help would be appreciated.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想挽留 2024-11-09 22:14:44

好的,按照您想要的方式拆分内容的最佳方法是:

  1. 在文件夹下设置您的 CodeIgniter 应用程序,例如 /var/www 并确保一切都按您想要的方式工作。
  2. 将 CodeIgniter 的 config.php 下的站点的基本 url 设置为“/”。
  3. 为站点的安全部分创建一个 Apache 虚拟主机,监听端口 443 或其他端口上的请求。安装您的证书等。 http://www.namecheap.com 对于证书很有用。将 Web 根目录设置为 CodeIgniter 文件夹,例如 /var/www。
  4. 创建另一个 Apache 虚拟主机,指向相同的目录,例如,对于网站的不安全版本。

现在,一切顺利,在此阶段您将能够使用 https 或标准 http 访问整个站点。我认为您提到能够更进一步,只允许通过 HTTPS 和某些不安全的方式访问某些控制器。我为此要做的事情如下。

  1. 在 application/libraries 文件夹下创建一个 CodeIgniter 库,将其命名为 Ssl.php。输入以下代码:
class Ssl {

    public function require()
    {
        // Is the current request method secure, via SSL?
        if ( ! isset($_SERVER['https']) )
        {
            // No. Do something here, display an error, redirect... up to you
            show_error("This resource must be accessed through an SSL encrypted connection.");
        }
    }

}

现在,在您的应用程序控制器中,只需以通常的方式加载库 $this->load->library('ssl') 以及您希望需要<的任何控制器方法对于 SSL 连接,只需在任何执行开始之前调用 $this->ssl->require() 方法即可。

您甚至可以更进一步,将该方法调用放到类控制器 __construct() 函数中的 require() 中,甚至是您可能希望的整个新控制器延伸自.

我希望这能在某种程度上有所帮助。

OK the best way to split things up the way you want, would be to:

  1. Set up your CodeIgniter app under a folder, say /var/www and ensure everything is working as you want.
  2. Set the base url for the site under the config.php of CodeIgniter to just "/".
  3. Create an Apache virtual host for the secure portion of the site, listening to requests on port 443 or whatever. Install your certificate and so on. http://www.namecheap.com are good for certs. Set up the web root as the CodeIgniter folder, e.g. /var/www.
  4. Create a further Apache virtual host, pointing to the same directory e.g. /var/www for the unsecure version of the website.

You will now, all being well, at this stage be able to access the entire site using either https or standard http. I think you mentioned being able to take things a step further by only allowing access to certain controllers via HTTPS and certain unsecure. What I would do for this is the following.

  1. Create a CodeIgniter library, call it say Ssl.php, under your application/libraries folder. Put in the following code:

class Ssl {

    public function require()
    {
        // Is the current request method secure, via SSL?
        if ( ! isset($_SERVER['https']) )
        {
            // No. Do something here, display an error, redirect... up to you
            show_error("This resource must be accessed through an SSL encrypted connection.");
        }
    }

}

Now, in your application controllers, simply load the library the usual way $this->load->library('ssl') and for any controller method that you wish to require an SSL connection for, simply call the $this->ssl->require() method before any execution starts.

You could even go a step further and drop that method call to require() in a class controller __construct() function, or even an entire new controller that you may wish to extend from.

I hope this helps in some way.

吃素的狼 2024-11-09 22:14:44

嘿,我正在开发一个可以通过 HTTPS/SSL 成功运行的 CI 应用程序。

我觉得你有点困惑。据我所知,如果您使用的是 Apache,则只能通过创建新站点或“虚拟主机”来设置启用 SSL 的站点本身。

因此,本质上,如果您使用 Apache,您将创建一个虚拟主机来处理端口 443 上的请求,例如 https://example.com 然后将 Web 根目录设置为 /var/www 或 CI 应用程序所在的位置。一旦您购买了证书并在生成证书请求后下载了一些信息,您还必须将 Apache 配置为使用您的证书文件。这比听起来容易。

有什么理由不能让整个应用程序通过 SSL 运行?而不是加密和非加密部分? SSL 的 CPU 开销很小,但很小。

我希望这能以某种方式有所帮助。

编辑评论:

不客气。这是一个最小的开销。为了避免麻烦,我只是简单地将其全部放在 SSL 虚拟主机下。另外,如果您要在 SSL/非 SSL 之间拆分内容,您可能会注意到,如果您在 SSL 页面上包含基于非 SSL 的内容,用户将在浏览器中收到有关“不安全内容”等的讨厌消息,这可能推迟他们并制造一些不必要的怀疑。

根据您的需要进行拆分可能非常困难 - 因为您需要为每个虚拟主机提供单独的 root index.php CI 文件,以允许 CI 正确路由它。您不能只设置一个为诸如 application/controllers/private/ 之类的目录提供服务的虚拟主机,因为如果不对其核心路由进行一些严格的修改,CodeIgniter 将不知道如何处理请求。

老实说,我会把所有东西都放在 SSL 虚拟主机下。或者,另一种选择是设置两个从同一系统/核心 CI 文件夹运行的 CI 应用程序……如果这有意义的话,但共享库和模型等内容将变得乏味。

Hey, I am in the middle of developing a CI app that is successfully running with HTTPS/SSL.

I think you are a bit confused. As far as I know, you can only set up an SSL enabled site per se by creating a new site or "virtual host" if you are using Apache for example.

So essentially if you were using Apache, you would create a virtual host to handle requests on port 443 for say https://example.com and then set the web root to say /var/www or wherever your CI app sits. You would also have to configure Apache to use your certificate file, once you have bought the cert and downloaded the bits and bobs after generating the cert request. It's easier than it sounds.

Is there any reason why you can't just have your entire app running through SSL? Rather than an encrypted and non-encrypted section? There is a small CPU overhead for SSL but it is minimal.

I hope this helps in one way or another.

EDIT IN RESPONSE TO COMMENT:

You're welcome. It's a minimal overhead. For the hassle, I would just simple put it all under an SSL vhost. Plus, if you were to split content between SSL/non-SSL, you may notice that if you include non-SSL based content on an SSL page, users will get a pesky message in their browser about "insecure content" etc, which may put them off and create some needless doubt.

It may be quite difficult to split as you want - as you would need seperate root index.php CI files for each vhost to allow CI to route it correctly. You couldn't just set a vhost serving a directory such as application/controllers/private/ because CodeIgniter wouldn't know how to handle the request without some severe modification to it's core routing.

I would honestly just stick everything under an SSL vhost. Or, another option would be to set up two CI apps running from the same system/core CI folder... if that makes sense, but then sharing content such as libraries and models will become tedious.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文