PHP global path setting - php

I have this setting.
root dir|
index.php
config.php
file.php |
|
|scripts|a.js
|
|account
|index.php
| |
|member |index.php
Now, I've included index.php of member dir into index.php of account dir. Also , the account index.php includes the config.php which contains,
define( 'PATH', (__DIR__) );
Now , for all includes in account index.php I use,
require_once( PATH . '\file.php' );
and is working properly. But when I try to add the path for script src such as,
<script type="text/javascript" src="<?php '.PATH.'scripts/livevalidation.js ?>"></script>
I get an error, so how can i include the a.js in scripts folder into index.php of account using the globally defined path.
Thanks.

The PHP "__DIR__" and "__FILE__" are absolute to the server. You shouldn't need to use either for your script.
<script src="/scripts/livevalidation.js"></script>
Also, your PHP looks like it has some syntax errors, this would be correct (although still wouldn't work:
<script src="<?php echo PATH.'/scripts/livevalidation.js'; ?>"></script>

You're missing a print or echo statement in the PHP statement in your script tag. You're also placing the concatination periods in the wrong place. On top of that, however, the JavaScript you're trying to include doesn't need to be in the PHP statement.
All that said, the final line should read like the following:
<script type="text/javascript" src="<?php echo PATH ?>scripts/livevalidation.js"></script>
On top of that, however, I don't think the above will work as you expect. __DIR__ outputs a server-side filesystem path, which wouldn't make sense when importing JavaScript over HTTP. I'd recommend something more along the lines of the following:
<?php define('URL_ROOT', '/'); ?>
<script type="text/javascript" src="<?php echo URL_ROOT ?>scripts/livevalidation.js"></script>
In the above example URL_ROOT would point an absolute URL under which your static media (CSS, JavaScript, and so on) is served.

Related

How can I use relative path in CodeIgniter?

I know a little bit about relative path. Suppose, I have a project on codeigniter. There are 4 controller such as with function name:
1. Admin->index,profile,add_student,edit_marks,make_admin,
2. Teacher->index,add_marks,update_marks
3. Student->index,view_teacher,view_result
4. Guest->index,view_Student, view_marks,view_result
From these, How can I use them as relative path ?Remind it,some times I go one controller to another controller, such as profile,view_marks is a common function for all, so every one access it.
For images or file can I use relative path ? Then,Suppose I have a image at root folder as
->root_folder/template/images/a.png. How can I use it as relative path ?
Really, I need the core knowledge of relative path with application in codeigniter.
You should use codeigniter constants defined in root/index.php
To get Root path use
FCPATH
It returns
Path upto root folder where your codeigniter is installed.
So in your case , it will be
FCPATH . 'template/images/a.png';
I assume you're looking for path not URL.
At last I have found the answer that is:
It is not necessary to use base_url or site_url every times for link.
All time use:
/controller_name/function_name
Such as for a anchor link on viewing admins's profile code as(I hope, this technique is called relative path). It decrease time to route it.
Admin Profile
What's wrong with using this for the image path?
base_url('path/to/image')
If having senario like
ROOT
|____APPLICATION
|____ASSETS
| |________ CSS
| |________ JS
| |________ IMG
|____SYSTEM
then you can access the css as follows:-
<script type="text/javascript" src="<?php echo base_url();?>assets/css/style.css"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/getinfo.css"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/img/image.png"></script>
The current accepted answer works fine, but it is not relative. For instance: if the website would be replaced from the root to a subdirectory, the url wouldn't work anymore.
More on this same topic I found here: How can I display images with the Codeigniter php framework?
The answer in that topic recommends not working with relative paths, but personally I would like to see this possible within modules (the MX-plugin for CodeIgniter).
So here's a possible way of working with relative paths I found:
<?php $path = base_url(str_replace(FCPATH,"", __DIR__ )).'/'; ?>
Example usage:
<img src="<?php echo $path;?>../assets/img/myImage.png" />
for instance: if the this html would be used in /modules/mymodule/views/someviewfile.php the output will be yourdomain.com/modules/mymodule/assets/img/myImage.png

How does one specify paths when working with PHP and js?

To give you more context, my file structure look like: includes/overall/footer.php and in this file I am referencing js/nav-header.js note that both includes and js folders are in the base folder. Am I doing this correctly?
includes/overall/footer.php:
<?php include 'includes/footer.php'; ?>
<script type="text/javascript" src="libs/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="libs/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" scr="js/nav-header.js"></script>
</body>
</html>
This file itself is included in another file, does it have something to do with the order in which things are being loaded perhaps? Reason being is that I cannot seem to get anything in the javascript file to fire. Any help would be appreciated.
What dose you nav-header.js contain?
Assets are requested from the server in the order they appear in the html. So nav-header.js will be called after jquery however that doesn't mean it will finish loading before it.
Also to load files use absolute paths:
/js/nav-header.js instead of js/nav-header.js
that way you know the js/nav-header.js will be loaded from the root folder.
if u have code in js/nav-header.js that fires on load(not on document ready) i would sugest moving it to document ready.
I recommend against using hardcoded absolute paths, as that will make the application less portable.
Instead, set a $base_url variable that you can echo in your html
<?php $base_url = "/"; ?>
In your html
<script type="text/javascript" scr="<?php echo $base_url; ?>js/nav-header.js"></script>
You can use this throughout your site for js, css, anchors, etc.
Use the BASE tag in HTML with all relative paths :D
Phil Sturgeon wrote a blog post about its use in CodeIgniter, but essentially you don't need to use a framework to make use of the same principle.
You should store the document root in a variable somewhere in your application and have a function that references it, then echo that out into the base tag. This way when you move your application to a different environment you only have to change the root in one place and you are good to go.
Also, read this before using the base tag.

CodeIgniter relative path or base URL function?

All these days I thought that CodeIgniter did not allow direct access to file(s) in the application (I mean the application itself and not the application folder).
So, if I have the following structure under the folder, www:
ROOT
|____APPLICATION
|___________JS/mine.js
|___________VIEWS/my_view.php
And if I want to include mine.js in my_view.php, I would need to refer the JS file using the base_url() function as follows:
<script type="text/javascript" src="<?php echo base_url();?>js/mine.js"></script>
Looks like I was wrong, I can refer to it relatively as well. Like:
<script type="text/javascript" src="../js/mine.js"></script>
Any thoughts/opinions?
Which one is a good practice? And why?
It's always a smart idea to use base_url() so if your URL changes, all your links don't break.
base_url() is going to return the full TLD, plus any folders your site is in.
Best practice is to keep your assets out of the application, or any other system folders. Most CodeIgniter sites will structure their assets as follows:
ROOT
|____APPLICATION
|____ASSETS
| |________ CSS
| |________ JS
| |________ IMG
|____SYSTEM
And then referenced like so:
<script type="text/javascript" src="<?php echo base_url('assets/js/mine.js')"></script>
It is always a good idea while using MVC to keep all assets folders (css, js and images) in root directory rather than keeping inside application or any other folder and accessing them using base_url.
<script type="text/javascript" src="<?php echo base_url();?>js/mine.js"></script>
So I think above line you mentioned is the proper way.

Absolute paths within includes with localhost

all.
My question is in regards to a problem I'm encountering when trying to add a universal php template for my DOCTYPE section. My DOCTYPE include (aptly entitled doctype.php) lies within the /template directory, and also includes calls pointing to my CSS and JS files that I want to be accessible to all pages.
So the problem is encountered when I try to write the absolute path to these files (the CSS and JS files). Currently, I am trying:
<script type="text/javascript" src="<?php echo $_SERVER['DOCUMENT_ROOT'] . '/file/extension/to/javascript/file.js'; ?>"></script>
and
<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER['DOCUMENT_ROOT'] . 'file/extension/to/css/file.css'; ?>"
I am running the application through WAMP on my localhost.
Taking a look a look at the source code, it appears as though the links are pointing to the appropriate files (c:/wamp/www/examplesite/path/to/file/file.ext), and all should be well. BUT it is not...
The JavaScript is not accessible and the Stylesheet is not functioning. I'm at a loss for what to do.
I've also tried:
-writing the absolute path without the use of PHP
-creating PHP variables to hold the document root and then concatenating the appropriate directory path to access the files.
Any suggestions? And how will this change when I upload the directory structure to my online server vs. my current localhost?
You might want to try $_SERVER['HTTP_REFERER'] instead. It gives you the path you are looking for.
On my local machine, which uses WAMP, I used <?php print_r($_SERVER); ?> to see what values it gives.
Also, there may be some typos in the snippets. For example, you don't need the leading / in the first example you gave.
For example:
<script type="text/javascript" src="<?php echo $_SERVER['HTTP_REFERER'] . 'file/extension/to/javascript/file.js'; ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER['HTTP_REFERER'] . 'file/extension/to/css/file.css'; ?>"></link>
Or since HTTP_REFERER can't be trusted in some case, you may want to create a function that builds the base part of the absolute path.
<?php
function getAddress() {
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
$filenamepattern = '/'.basename(__FILE__).'/';
return $protocol.'://'.$_SERVER['HTTP_HOST'].preg_replace($filenamepattern,"",$_SERVER['REQUEST_URI']);
}
?>
Then call it like so:
<script type="text/javascript" src="<?php echo getAddress() . 'file/extension/to/javascript/file.js'; ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo getAddress() . 'file/extension/to/css/file.css'; ?>"></link>
You're pointing to files on your local machine using the file path (ex: C:\some\path\to\file) when you should be using a URL (ex: http://localhost/some/path/to/file). The HTML is parsed by the client's browser so when it attempts to access a path that isn't a URL it can't.
Instead of using $_SERVER['DOCUMENT_ROOT'] you can either use absolute URLs such as
<script type="text/javascript" src="/path/to/my/file.js"></script>
where the "/path" folder is your base web directory, or you can use relative URLs based on whatever the current directory the file is in to which you're including the doctype.php file. I'd recommend not doing the latter as it's a pain in the butt to keep track of.
If you use relative URLs you should have no problems when moving your code to a new host, provided the directory structure remains the same.
Of course any kind of resource (JS, CSS, Images, etc.) has to be accessed via http(s) request and therefore it is impossible to directly access them with an absolute path. Think of the security implications of such an approach. So you always have to use web paths relative to your web root directory.
For example:
<script type="text/javascript" src="http://localhost/myproject/media/ja/file.js"></script>
You're going about the wrong way of including files in your page, you should be using HTTP URLS instead:
<script type="text/javascript" src="/file/extension/to/javascript/file.js"></script>
Or, if you prefer to use a variable with the host name:
<script type="text/javascript" src="http://<?php echo $_SERVER['HTTP_HOST']; ?>/file/extension/to/javascript/file.js"></script>

CodeIgniter: How do I include a .js file in view?

Where and how do I include .js files in Views in CodeIgniter?
I have tried this:
<script type="text/javascript" src="system/application/libraries/jquery.js"></script>
As I figured that the index.php is the one that is loading the views, so I'm guessing that every time a page is loaded the current dir path is at root because index.php is located at root. Is this true?
The above line doesn't so I am doing something wrong. I have a CodeIgniter project. The path is like this:
localhost/CodeIgniter/system/application
so which path should I use to include my jquery.js file which is located in
localhost/CodeIgniter/system/application/libraries
when I want to load the jquery.js file in a View located here:
localhost/codeIgniter/system/application/views/till_view.php
There is a lesser-known solution that works really well with clean syntax and much more portability than hard-coding URL's or relative files.
<base href="<?=base_url();?>">
<img src="images/logo.gif" alt="Logo" />
Read more about how, what, why on my article "Asset handling in CodeIgniter with the BASE tag".
It's not the "index.php" file that is the view. The view is whatever is loaded in your controller when you do a
$this->load->view("viewname");
The file "viewname.php" can then include the .js files, just as a normal .html (or .php) file would:
<script src="/url/to/javascript.js" />
You may want to create a default view or a "header" view that includes some or all of the (common) .js files for your project.
-CF
First question:
It depends if you use absolute or relative urls.
Absolute urls go from the root of your domain. Relative urls are loaded relative from the current directory (including the url segments).
Second question:
It's best to use an absolute URL. Because of the pretty urls, it's not recommended to use relative urls.
The easiest way is to use the url helper and then use the site url function like this:
$this->load->helper('url');
echo site_url('system/application/libraries/jquery.js');
Ps. I recommend to put things like javascript and images outside of the CodeIgniter directory.
base_url() always works fine for me
Here is a solution specifically relevant to the OP which I didn't think anyone else provided.
<script type="text/javascript" src="<?= base_url() ?>libraries/jquery.js"></script>
So, base_url() will provide a path direct to the root of your site.
<script type="text/javascript" src="<?php base_url() ?>libraries/jquery.js"></script>
base_url() will provide the url of the site
The easiest way you can directly access the file:
<img src="http://localhost/ci/you_dir/your_img/your_image.png" />
where ci is for codeigniter.

Categories