从非公共目录加载 css 或 javascript

发布于 2024-09-02 22:29:24 字数 173 浏览 12 评论 0原文

有没有办法从公共 Web 目录之外加载 css 和/或 javascript 文件?

例如,在我的托管服务上,我有 /public_html 但不希望这些文件存在于公共目录中,并希望它们位于 /system 内同级目录 /system 中公共目录之外的目录中(我正在使用 codeigniter) /应用程序/视图/

is there a way to load css and/or javascript files from outside of the public web directory?

for example on my hosting service i have /public_html but don't want these files to exist in the public directory and want them in a directory outside of the public directory in a sibling directory /system (i am using codeigniter) within the /system/application/view/

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

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

发布评论

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

评论(5

短叹 2024-09-09 22:29:24

最终,Javascript 和样式表在客户端进行处理。因此,没有任何解决方案可以真正向公众隐藏您的 javascript 或 CSS。

一种可能的解决方案是使用 file_get_contents() 之类的方法通过 PHP 加载所需的 CSS/Javascript 文件,然后使用内联样式/脚本将其直接输出到页面。

但这并不能真正解决向公众隐藏代码/样式的问题。它可以让您选择通过某种打包程序或混淆程序过滤所有代码和样式,尽管您没有理由不能对静态文件执行此操作(并且处理费用要少得多)

Ultimately, Javascript and Stylesheets are processed on the client side. For that reason, there is no solution that would truly hide your javascript or CSS from the public.

One possible solution is to load the required CSS/ Javascript file via PHP using something like file_get_contents() and then outputting that directly to the page using inline styles / scripts.

This doesn't really solve your problem of hiding the code / styles from the public though. It would give you the option of filtering all code and styles through some kind of packer or obfuscatory, although there's no reason you couldn't do that with your static files (and at much less of a processing expense)

妖妓 2024-09-09 22:29:24

是的——在某种程度上——还有Minify [http:// code.google.com/p/minify/] 是一种方法。

查看配置文件的第 39 行 [http: //code.google.com/p/minify/source/browse/trunk/min/config.php]。在这里,您将看到缩小的缓存位于网络根目录之外的位置。现在,我不知道源JS和CSS是否可以与缓存放在同一目录中。

Yes -- in a way -- and Minify [http://code.google.com/p/minify/] is one approach.

Look at line 39 of the config file [http://code.google.com/p/minify/source/browse/trunk/min/config.php]. Here you will see where your minified cache sits outside of the web root. Now, I do not know if the source JS and CSS can sit in the same directory as the cache.

删除→记忆 2024-09-09 22:29:24

没有面向公众的代理就不行。

您需要file_get_contents()include它们,然后将它们提供给您的页面。

您不能只执行 ../../system 并获得 DOCROOT 之上的权限。

它们需要由浏览器处理,因此需要可访问。

如果您想阻止人们以人类可读的方式查看您的源代码,请查看 CSS minifyJS 打包器。这些当然只是混淆代码。任何有决心的人都能够阅读您的 JavaScript 并了解它的作用。

为什么你不希望人们阅读你的 CSS 或 JavaScript?

Not without a public facing proxy.

You will need to file_get_contents() or include them and then serve them to your page.

You can not just do ../../system and get above the DOCROOT.

They need to be processed by the browser, so they need to be accessible.

If you want to hinder people viewing your source in a human readable way, check out CSS minify and JS packer. These of course are only obfuscating the code. Anyone determined will be able to read your JavaScript and see what it does.

Why don't you want people to read your CSS or JavaScript?

梦在深巷 2024-09-09 22:29:24

我知道你的意思twmulloy,在不同的地方“查看”相关信息似乎不一致。但是,请考虑 JS 和 CSS 文件是支持视图的资源,而不是视图本身的一部分。

也就是说,您可以通过多种方式实现您想要的目标。一种可能是编写一个控制器,它接受对 JS/CSS 资源的请求,并从相关位置(视图文件、数据库,实际上任何地方)输出标头和数据。然而,与仅仅接受在根级别 public_html 的子文件夹中弹出文件的“不整洁”相比,这是低效的。我和上面的许多评论者一样,认为这是最好的解决方案,因为它的速度和适当性;只是有一个与“系统”目录同一级别的“资产”目录,里面有图像、CSS、JS 等。您可以使用别名或虚拟文件夹来让您感觉更好......

但是,还有第三种方法。有些库可以做你想要的事情,并具有 Minify (来自已接受的答案)和压缩的额外好处,或者任何你喜欢的东西。我知道的两个库称为 AssetLibPro 和 Carabiner,它们允许您指定资产路径(根据您的需要),然后加载 JS 和 CSS 文件(如果需要,可以使用屏幕、打印等组)。然后他们将所有相关的 CSS/JS 等作为一个文件提供;压缩、缩小、缓存...无论您需要什么。

I know what you mean twmulloy, it seems inconsistent to have 'view' related information in different places. However, consider that the JS and CSS files are resources that support the views, rather than parts of the view themselves.

That said, you can achieve what you want in a number of ways. One might be to write a controller that accepts requests for your JS/CSS assets and outputs a header and data from the relevant place (a view file, the database, anywhere in fact). However, this is inefficienty compared to just accepting the 'untidiness' of popping the files in a subfolder of the root level public_html. I, like many commentors above, feel this is the best solution for its speed and appropriateness; just having an 'assets' directory at the same level as the 'system' one, with images, css, js etc inside. You could use an alias or virtual folder to make things feel better for you...

However, there is a third way. There are libraries that do something JUST like what you want, with the added benefit of Minify (from the accepted answer) and compression, or whatever you fancy. The two libraries I know of are called AssetLibPro and Carabiner, and these allow you to specify an asset path (as you want), and then you load your JS and CSS files (with groups e.g. screen, print if needed). They then serve up all related CSS/JS etc as one file; compressed, minified, cached... whatever you need.

执妄 2024-09-09 22:29:24

我知道在一种情况下这是必要的。如果我有多个版本的文件,并且不需要所有实例中的所有版本,那么最好将这些版本放在某个隐藏文件夹中并仅加载我想要的版本。类似于 CDN,但位于自己的服务器上。

I know of one situation where this is necessary. If I have several versions of files and I don't want all versions in all instances, it would be ideal to put these versions in some hidden folder and load only the version I want. Something like a CDN, but on its own server.

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