PHP Link problem - php

I have a head file which I am using for a few different pages. The problem is when I go into a folder, the links in the head file point to say index.php instead of ../index.php
Is there any function to fix this or any work arounds that I'm missing?.
Thanks!
~ Kyle G

The links in the HTML? if so you can use absolute paths in your links. eg linking like so: <a href="/index.php">
If you're talking about include()/require() then one solution is to set your include path.
Or you could never serve html from subdirectories :) that's worked for me so far.

Related

Best way to deal with relative paths/base tag

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

Absolute paths in CakePHP project

i was given a pretty big cakePHP (built on v. 1.3.10) project to maintain. The problem is that the majority of the paths are absolute (which on my opinion is a bad habit).
Eg. in default.ctp there is:
<link rel="stylesheet" href="/css/public_new.css" />
but then at the bottom of the same file there is:
<?php echo $html->script('jquery-ui-1.8.16.custom.min.js'); ?>
which prints the correct paths.
It's like the original developers made the site to put in a server's root (not in a subdirectory).
Things ive tried to solve this problem without success:
modified the .htaccess files on /, app/, and app/webroot
adding a tag
I know i can add a $this->base to the beginning of every path, but this is not a solution since there are thousands of files to modifiy :(
So my question: is there any solution using mod_rewrite or such?
Thanks in advance.
$html->script() will automatically prepend /js/ in the HTML output. Cake's .htaccess will then point these file calls to /webroot/js/.
So <?php echo $html->script('jquery-ui-1.8.16.custom.min.js'); ?> would output
/js/jquery-ui-1.8.16.custom.min.js in the HTML.
To replace all those urls / links you could actually use a program like Actual Search and Replace ;-) I use it a lot.
Solution:
on app/config/boostrap.php, add:
Configure::write('App.base', '/teka_new/');
I take this out from Ho do I change the base path of routes in CakePHP?

common elements php html navbar

I am trying to make a template for my website.
Basically taking out all the common stuff out using php's include function.
I have made a navigationbar.php and samplepage.php.
navigationbar has all the links to stylesheets etc.
When they are in the same folder and I include navigationbar.php in samplepage it works just fine.
However when i move samplepage.php to a subdirectory (leaving navigationbar.php in the same folder) and
link navigationbar.php with the menu doesn't come formatted.
it seems samplepage is getting contents from navigationbar.php but navigationbar is not linking to the css files.
Can anyone tell what I'm doing wrong here?
i'm using xampp and have tried with both relative and absolute paths- (include '../navigation.php' and 'localhost/folder/navigation.php')
Make the paths of your css files absolute (i.e. start with / and specify the full path). This will allow them to work correctly from any path.
When including a file in PHP, the url's are relative to the file you are including into. Not to the file you are including.
So you need to change your paths 'navigationbar.php'
Try using something along the lines of this to link to your header/footer files:
include($_SERVER['DOCUMENT_ROOT'].'/path_to/header.php');
and then appropriately link to your .js/.css files within header.
Where your PHP files are has no direct influence. You have to make sure that the resulting HTML has the correct paths to all CSS/JS/img files etc. You may want to post your file structure here and show the code that calls the relevant CSS files.

Location of "assets" (php, css, jss, ect)

In my root directory I have a bunch of single pages and then the folder "blog" and "assets." For the pages I have a header.php/nav.php/footer.php to call for various css and js.
for example: within the header.php:
<link href="http://beta.rfahaiti.org/assets/css/bootstrap.css" rel="stylesheet">
Then, in the pages I call for: <?php include 'assets/header.php'; ?>
However, this does not seem to be working for any pages within the blog folder -- such as the index.php file in /blog/news/. I assume it's a relative vs absolute link issue but I'm not sure how to fix. Question: what does the php include call need to be for to call for the header.php file?
Thanks!
Try:
<?php include '../assets/header.php'; ?>
or
<?php include '../../assets/header.php'; ?>
depending on your folder structure.
Include paths are relative, try:
<?php include '../assets/header.php'; ?>
You will find the same with HTML document referring to resources e.g CSS.
It is a relative link issue, as you say. For pages two levels deep in /blog/news, you need to go two levels back:
../../assets/header.php
Edit thanks to Juan Sosa for pointing out that what follows is completely wrong.
Alternatively, you could write this:
/assets/header.php
The second approach is cleaner in one sense; however, beware it assumes that your site will always be located at the root of the domain (ie, if it ever got moved to http://beta.rfahaiti.org/theapplication/ or something, then all those type of links would break).

How to use images and php includes across multiple directories?

I am brand new to PHP. I want to use it to include a universal header and footer in an html/jquery site. Currently I am using includes to do this:
<?php include('../includes/footer.php'); ?>
This works fine. Where I encounter a problem is with any images in the header or footer.
An explanation of my file structure:
Root folder: contains index.php and the folders "includes", "img", "php" etc.
php folder: contains gallery.php
includes folder: contains header.php, footer.php
When viewing the index.php all images in the header and footer show properly, but, because they are linked relatively (ex "img/facebook.png"), the do not show in gallery.php. To work they would need a ../ included. But then this would defeat the purpose of a universal header.
Thus I am trying to figure out how to link the images in the includes files in way that is doesn't matter where the php file is located. I have read this thread (which sounds like my problem) but I do not understand any of the answers. I have also read things that suggest $_SERVER['DOCUMENT_ROOT'] .'/folder/';, in conjunction with an echo to display the image. I tried this in my footer.php with this code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'] . '/img/';
$image = ($path . "facebook.png");
echo "<img src=\"$image\" />"; ?>
When I view the page though, I end up with a little torn paper icon instead of my image. I assume this means that the path is incorrect, but I do not know why. facebook.png resides in the img folder. This problem occurs on both index.php and gallery.php.
After this rather long winded explanation (sorry), my mains questions are:
1) How do I get images to show up properly in my includes across multiple directories?
2) If I am going about this in the right way with the echo, what are the possible reasons why it is not working?
Once again, I know nothing about php, so if you could try to make your answers as basic as possible, it would be much appreciated.
Thank you!
Instead of img/facebook.png, add a / before the img/ like this: /img/facebook.png
This says "go to the root, and look for the img folder there. All your images should work fine then. The path of the images are absolute or relative based on the HTML page you're viewing, not which files you use to create it.
Though there's probably not much of reason for a "php" folder - just keep all your pages in the root directory.

Categories