i have this problem that i need to change the directory address of href in anchor, i'm hoping to use php to make it work.. for example if i'm inside the home link my href value would be "index.php" but if im inside the folder of products link home href value will be change to "../index.php" because the header is just included to every page.. and also if you could suggest any alternatives for it, i would be happy to hear it.. thanks!
<div class="header_link">
<div class="links"><p>Home</p></div>
<div class="links"><p>Products</p></div>
<div class="links"><p>Packages</p></div>
<div class="links"><p>Price List</p></div>
</div>
You should use absolute paths instead of relative ones. Relative paths will become difficult to manage down the road, and as you've discovered they have to change depending on which page you're viewing.
Instead of linking to ../index.php, just link to /index.php. This will work regardless of which page you're on.
Try using the base tag within the <head> of your html.
<base href="http://www.mysite.com">
or
Make all href values absolute from the domain.
Home
Related
Good Day! I've got some trouble on PHP. If I click an href which is this <a href = 'members/loans/loan-application-form.php'> it goes to the loan-applicaton-form.php, but if i click it again inside the loan-application-form.php (cuz it is located at the navbar that exist in all pages) the href of the anchor concatenates on the existing url and it creates an error telling that address is not existing. I already knew that the problem is it searches the address on the current directory and probably it will return an error because there is no existing address on the directory because it is the origin or the parent. What is the best way to avoid this?
Add a base tag to your header. This will prepend all HTML URL's with the given base URL. (Including images, CSS and JS calls)
<head>
<base href="http://www.yourdefaulturl.com/">
</head>
Your URL will then be http://www.yourdefaulturl.com/members/loans/loan-application-form.php
You can use an absolute path
<a href = 'http//your.site.cpm/members/loans/loan-application-form.php'>
Advice : you can read this article
or this stack question
Or you can use ../ to navigate inside your relative path
Lets imagine your nav bar is located at /navigation/navbar.html
Then you can have a relative path like
<a href = '../members/loans/loan-application-form.php'>
By specifying relative Urls like so:
<a href = 'members/loans/loan-application-form.php'>
You are simply stating you wish to go form the current page to this page, hence why it is being added to the end of the current URL. The best way to avoid this quite simply is to set an absolute URL like so:
<a href = 'http://projectname/members/loans/loan-application-form.php'>
Even when not using http:// it is often still considered relative so be sure to include this.
Another way to do the same but slightly quicker would be to add a variable in say your header file for example:
$url = 'http://example.com';
Then when specifying the URLs you can do say:
<a href = '<?php echo $url;?>/members/loans/loan-application-form.php'>
This just means that should you say change your domain, rather than editing every URL you can simply edit the variable value and be done with it.
I am trying to link an action within a form to a php script:
<form action='../includes/RC_APP/process_login.php' method="post" name="login_form">
However it comes back with the error:
The requested URL /includes/RC_APP/process_login.php was not found on this server.
The PHP includes work fine:
<?php
include_once '../includes/RC_APP/db_connect.php';
include_once '../includes/RC_APP/functions.php';
My only thoughts are that the HTML will not process access above the web server document root directory while the PHP will; but this contradicts the security advice on keeping PHP include files outside of the web server's document root.
Any advice would be greatly appreciated.
you need to set base path in the head section.
like this:
<head>
<base href="your_site/sub_directory/">
</head>
The base tag makes everything link differently, including same-page anchor links to the base tag's url instead, e.g:
<a href='?update=1'>A link to this page</a>
becomes
<a href='your_site/sub_directory/?update=1' title='Some title'>A link to the base tag's page instead</a>
if you need to go another folder in same directory in base path e.g a folder includes in the basepath directory link that in this way :
<a href='includes/'>A link to this page</a>
if you need to go another parent folder of base path e.g a folder includes :
<a href='..includes/'>A link to this page</a>
see more details: Is it recommended to use the <base> html tag?
I think I have found the answer to my question, in that a form action cannot access anything above the document root directory:
HTML form action using a php file outside of root directory?
OK, so I've run into an issue with nested php includes, and I want to know what is a good compromise of best practices and ease-of-use.
Here's my website structure:
root
- index.php
-/include
- header.php
- footer.php
-/articles
-article-1.php
-/css
-style.css
So here's the issue: Inside index.php I have an include "include/header.php". Inside header.php I have many relative paths such as <link href="css/style.css>. And inside article-1.php I also have include "include/header.php".
So the index file works. But the article-1 file can't see the css file because the relative link is now looking for /articles/css/style.css. I found out about the <base> tag, and have set that in header.php, and it's fixed all my problems except for anchor links (which I can work around with javascript if I HAVE to), but I'm still concerned about what best practice is. How should I go about doing this correctly without having to prepend every single relative link with a huge php line and also without having to use a javascript hack to make anchor links work?
Thanks!
I ended up using a <base> tag in the header, and then wherever I needed anchor links I used php like so: <a href="http://<?php echo $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];?>#">
This makes the link go to the current page with a # added to the end, so it's the same as using <a href="#">
Let me know if you think I could have done this a better way! Thanks!
I think you can go up a directory with "../", current directory is "./"
So from root/articles/article-1.php, you would get to your stylesheet with ../css/style.css
<link href="../css/style.css">
In my opinion the best practice for this sitution is to go back to the root directory, provided that your project is in the root of your server/webspace.
Use a "/" in the beginning of the links, which is an absolute path.
Example: /css/style.css IS ACTUALLY root->css folder->style.css file
I created a CSS menu, to use on a PHP site, but now I realised that for example:
From the Home tab (URL localhost/site), all the item links are correct if you hover over them, but now i navigate to "Stock" for example, who's URL is localhost/site/stock... it opens correctly. So from Stock i want to navigate to Sales (localhost/site/sales), the URL of sales become localhost/site/stock/sales and not just localhost/site/sales.
I added this menu only recently, the previous one was working fine, so for a test, i replaced the css menu with the previous one, but to no avail. the problem still persist, so I assume that something else must have changed the behaviour of the links...
I can probably fix this by added " ../ " in front of the menu's URL, but on the other hand. not all items are just one step back.
Your help would be greatly appreciated.
Try using absolute URLs.
When your link is on a page located at localhost/site/stock:
Link <!-- goes to localhost/site/stock/sales -->
But if you prepend a forward slash, the destination will be absolute, relative to your document root:
Link <!-- goes to localhost/site/sales -->
Assuming of course that your document root is localhost/site
More likely your links will have to look something like:
Link
You're correct in saying you need to add slashes. My suggestion would be to provide absolute links from / to your page:
/site
/site/sales
/site/sales/sale24
/site/stock
/site/stock/secondstockpage
/site/stock/thirdpage
I have changed the menu as you suggested, but now, it works as follow:
if i for example set my menu link to direct to: /site/sales/sold.php
so the menu.php file link would look like this:
<li>Sales</li>
<ul>
<li>Sold Items</li>
</ul>
it would sometime direct to /site/sales/sold.php and the next moment, it repeats the menu's directory like: /site/sales/sales/sold.php
so i removed the sales directory, as its directing there by itself, worked for a little while and now it directs to /site/sold.php which does not exist so it ends up with a 404.
should i specify a siteroute somewhere else?
this is very weird to me. first time i encounter something like this!
Hope my examples make sense!
I'm developing a PHP website that uses url routing. I'd like the site to be directory independent, so that it could be moved from http://site.example.com/ to http://example.com/site/ without having to change every path in the HTML. The problem comes up when I'm linking to files which are not subject to routing, like css files, images and so on.
For example, let's assume that the view for the action index of the controller welcome contains the image img/banner.jpg. If the page is requested with the url http://site.example.com/welcome, the browser will request the image as http://site.example.com/img/banner.jpg, which is perfectly fine. But if the page is requested with the url http://site.example.com/welcome/index, the browser will think that welcome is a directory and will try to fetch the image as http://site.example.com/welcome/img/banner.jpg, which is obviously wrong.
I've already considered some options, but they all seem imperfect to me:
Use url rewriting to redirect requests from (*.css|*.js|...) or (css/*|js/*|...) to the right path.
Problems: Every extension would have to be named in the rewrite rules. If someone would add a new filetype (e.g. an mp3 file), it wouldn't be rewritten.
Prepend the base path to each relative path with a php function. For example:
<img src="<?php echo url::base(); ?>img/banner.jpg" />
Problems: Looks messy; css- and js-files containing paths would have to be processed by PHP.
So, how do you keep a website directory independent? Is there a better/cleaner way than the ones I came up with?
You could put in the head
<base href="<?php echo url::base(); ?>" />
This will mean the browser will request any non-absolute URLs relative to that path. However I am not sure how this would affect URLs embedded in CSS files etc. This does not affect paths defined in CSS files. (thanks mooware)
The <base> thing will work but you need to remember it's going to affect your <a> tags too. Consider this example.:
<!-- this page is http://oursite.com/index.html -->
<html>
<head>
<base href="http://static.oursite.com/" />
</head>
<body>
<img src="logo.gif" alt="this is http://static.oursite.com/logo.gif" />
this links to http://static.oursite.com/login which is not what we wanted. we wanted http://oursite.com/login
</body>
</html>
If you use a PHP function call for creating your links, that won't be a problem as you can just make sure it spits out absolute URL. But if you (or your designers) hand-code the <a> tags then you're stuck with the same problem again, just now with <a> instead of <img>.
EDIT: I should add the above paragraph is assuming you serve images from a different host name like we do. If you don't then obviously that won't be a problem.
tomhaigh has a good point, and would be worthwhile to investigate it further.
According to MSDN, the base tag works for all external sources, including style sheets, images, etc.
Perhaps I'm missing something, but can't you just do what I (and I thought everybody else) do/es? Namely put all your images, css, javascripts, etc in a common directory i.e.:
/inc/images/
/inc/css/
/inc/javascript/
etc
And then reference them with base-relative URLs, i.e.:
<img src="/inc/images/foo.jpg" />
etc
?