I have a config.ini file with some values. One of them is the path to the root of my script. So in my js file i get the content from the config.ini file, but i have one big mistake. To load the data from the config file i already need one value from the config file, namely the path to the config file.
Any idea how to handle that?
Regards
Sylnois
Edit:
This is my htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^([^/]+)/$ index.php?token=$1 [L]
This rewrites my link from http://domain.com/fun/bla/index.php?token?123 to http://domain.com/fun/bla/123/ ..
So if someone access the second link, my js script would not be run anymore, cause i work with relative paths. I have now a value in my config which points to the root directory of my applicatoin: "./fun/bla/". Everything works so fine. But my requirement are, that no paths should be implemented in my code.
Yes. Store the path to your config file in code. The rest can be loaded from the config file.
You can't possibly have everything in a config file. I once worked with someone who tried to store database configuration in the database. And then realized their mistake when they tried to make the application, you know, work.
What I've always done is to statically define the name of the config file in my code, so in your JS:
config_file = '/path/to/myconfig.ini'
This is a chicken and egg problem. The config file cannot contain the path to the config file, its path needs to be known to all parts of the program that need to know the settings. Perhaps have the path as a global variable in your program somewhere?
Related
How do I point a URL to a file so when I go to the URL it points to that file but doesn't change the URL. For example:
mydomain.com/orders/create should point to /myfiles/orders-create.php
then when I go to mydomain.com/orders/create it will display all the contents of orders-create.php.
Any ideas?
If you want to do it on the server-side you could edit the .htaccess file similar to this question's answer.
The main changes you're looking at are:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)$ $1.php [NC]
This would let you create files without an extension and it would run force run the PHP interpreter.
Here's a good read for pretty URLs to find out more ways you could do this, and it'll explain more about what they actually are.
If you can change create-order.php, then you can make it so that it does not rely on relative paths. One way to do this is to create a bootstrap.php file that has all of the includes and then your "leaf" PHP files only have to find this bootstrap.php file.
Another option is to set the PHP path. If you change the include path to include the root of the directory, then when including files in create-order.php will look in the root and it will find them. You can set the include path in your PHP files or in the PHP configuration.
If you don't want or can't change create-order.php then one way to do this would be via the webserver. For example, in apache, you can use mod_rewrite to do just that, have a public URL actually invoke a specific local file. The rewrite configuration might look like this (example for just this one file):
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^orders/create to create-order.php$ create-order.php [L]
or a catch-all rule with this (untested):
RewriteRule ^orders/(.*)$ $1?%{QUERY_STRING} [L]
I want to change the URL path to a folder using .htaccess like the format below:
http://example.com/src/home/
To
http://example.com/home/
Any Help ?
Using .htaccess:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^home/(.*)$ src/home/$1 [QSA,L]
If I should design the structure, I would do it like this:
/
/src/
/src/public/assets/css/all.css
/src/public/assets/js/all.js
/src/public/index.php
/src/public/.htaccess
/src/app/ <= All you classes, php files and functions
Set documentRoot to /src/public
Your index.php is now your entry point and you can load all your classes, setup a MVC pattern or anything you like.
All files that you use in your project in referenced by the server root / all assets that the browser is supposed to use, like css and js you put those in the public.
If you want you images to be secure, you could load them via a php file that takes them from outside the public folder.
Hope this all makes sense?
I've been trying to change my CodeIgniter file structure to make it safer and cleaner but I can't get it to work. I want to separate the application/system and the public files that are going to be visible to users.
- application
- system
- public
It works but I have to enter
example.com/public/index.php or example.com/public/controller
I want to be able to just type the base URL like example.com/controller.
How can I do it?
For CodeIgniter 3.x, the correct way to approach this (at this point in 2018 and after) is as follows:
Make a public folder in your root folder
MOVE your index.php file into this folder (to prevent ambiguity)
inside the moved index.php file, change the following:
change $system_path to ../system
change $application_folder to ../application
Now, we need an .htaccess file, but CI 3.x doesn't have a .htaccess file by default, soo.. how about stealing it from CI 4.x which has it by default? You can find it here:
https://github.com/bcit-ci/CodeIgniter4/blob/develop/public/.htaccess
I recommend you NOT include the line SetEnv CI_ENVIRONMENT development - instead, define this in your apache or NGinx .conf file so the .htaccess file can work anywhere.
Finally, you'll meed to update your .conf file so that DOCUMENT_ROOT is set to the subfolder named public (or whatever you chose to name it). Then restart Apache/Nginx.
That should give you the same results, and be much more secure.
-- UPDATE --
I found that I had problems with the .htaccess file from CI 4, however suspect it's my system that's the problem. This simple version did work however:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
As per the CodeIgniter Installation Instructions...
/var/application/
/var/system/
/var/www/index.php
For the best security, both the system and any application folders should be placed above web root so that they are not directly accessible via a browser. By default, .htaccess files are included in each folder to help prevent direct access, but it is best to remove them from public access entirely in case the web server configuration changes or doesn’t abide by the .htaccess.
Alright, so pretty much I have this on Htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index2.php?url=$1
And I'm trying to execute in a php file the following
mime_content_type('Doggy.png')
and it returns an error saying path of file not found, which I'm pretty sure it does exist,
trying to find what the error could be I ended thinking it's htaccess.
Can anybody help me solve this?
Thank you in advanced.
No, it's not your .htaccess rules. Those only rewrite HTTP requests into file execution rules. Once your PHP executes, they have no influence anymore whatsoever.
The problem is simply that the file Doggy.png does not exist relative to the file where this command is executed. The file must be in the same directory as index2.php, assuming that's the file that contains the line mime_content_type('Doggy.png'). Otherwise you need to use relative paths like mime_content_type('../Doggy.png') or mime_content_type('img/Doggy.png').
As we've established already, the problem is not in rewrite rules.
What actually matters is not he relative location of the files, but rather location of your image file relative to a working directory.
For example, if your directory structure is:
/
/index.php
/inc/imagelib.php
/inc/Doggy.png
and you're doing manipulation on Doggy from the imagelib.php, but it is actually included from index.php and your working directory is /, then, despite being in the same folder, you'll need to address the file as inc/Doggy.png.
The working directory may change, depending on what your entry point in the program will be (in case of a web application - where was the file that originally got the request), or (in case of command line), where were you in shell when executing the command.
To avoid problems with relative paths, i suggest using absolute paths.
So (assuming the same scenario as in the previous example), in imagelib.php you would need to construct the path to image path like this:
`$absolute_path = dirname(__FILE__)."/Doggy.png"`
I have a list of PDF [myfile.pdf] files on a folder/dir called example.com/uploaded/downloads/
I need a user to download or view on the page a pdf file, however I do not want them to see the uploaded on the url, which is the best way to do it?
so users can click a link example.com/downloads/
I had a .htaccess code (since im not good at it) it got me no where
RewriteRule ^downloads/([A-Za-z0-9-]+) /uploaded/downloads/$1 [L]
I have tried PHP header('... application/pdf'); not good either.
PLEASE NOT: example.com could change directory aswell e.g. example.com/new_design/ therefore the dir would be example.com/new_design/uploaded/downloads/ and users should be able to see example.com/new_design/downloads/
TIA
Make sure you have the RewriteEngine turned on and try this slightly modified pattern.
RewriteEngine on
RewriteBase /
RewriteRule ^downloads/(.*)$ uploaded/downloads/$1 [L]
If you are still having trouble, try looking at the Apache log files. They may indicate a problem with permissions or the file path. The log file is usually located in /var/log/apache2.