Loading a page without changing URL and keep relative paths working - php

My first post here...I hope that I don't brake any rules and if I do please go easy on me :-)
I have a page a.php with a simple form with one field: name
User puts his/her name and submits. File b.php is in the action field of the form and so it gets the form.
b.php needs to load a slideshow depending on the user input. Let's say there is a folder "clients" and it has folders John and Mary. Each of these folders will have an index file and other sub folders of images, CSS, JS, etc. The index files have relative paths to the subfolders like: ./images/pic1.jpg ./display.css etc.
Here is my problem:
I want to load, say John/index.html without changing the URL that would be xyz.com/b.php
If I use an iFrame the source code will show that xyz.com/clients/John/index.html is loaded and if I use PHP include("xyz.com/clients/John/index.html") then the relative paths that are in the John/index.html won't work.
I am trying to hide the physical location of the slideshow so I can always serve them under the same URL as far as the user can see. Changing relative paths to absolute won't work because that will give away everything that I am trying to hide!
I have simplified the form, folder name, etc. to explain the problem. Could anybody suggest any solutions please.

I don't fully understand the question but <base> may help. It tells the browser to make all links from a page relative to another URL instead of where the page was loaded from
<base href="http://mydomain.com/mybaseurl/" />

Related

PHP Includes not working after moving pages into subfolders

The problem is as follows:
Originally, all of the individual pages on my website were in the root folder of the site.
I have now moved the majority of the pages into various sub-folders.
For example: The main page for our Apache Totem Freestanding Light Box used to be in the root folder, but is now in subfolders: /lightboxes/apache-totem-freestanding-lightbox/index.php
The problem is that various elements of the page have “PHP Modules” included for common elements such as the header, the breadcrumbs, the footer, the css file etc.
For example, for the header we have as a link on each page.
The files that these “PHP includes” point to are situated in their own folder on the site root, called ‘modules’.
The trouble with this, is that the ‘PHP Includes’ can’t seem to find the modules because I guess they are expecting to see them in a folder called ‘modules’ that is situated in the same subfolder as the current page.
I’ve partially got around this by changing the link to the module to have ../../ in front to tell it that it has to look two folders up from the current folder, but the page is still having difficulties displaying all of the information.
To give you an idea of this, I have uploaded the Apache Totem Index Page in its subfolder /lightboxes/apache-totem-freestanding-lightbox/
https://www.w-co.co.uk/lightboxes/apache-totem-freestanding-lightbox/
As you can see, the main content of the page is displayed ok and the css file is doing its job with styling etc.
But there are various other problems, for example:
1. There is an image missing from the header
2. The quote button in the header and the content is pointing to the wrong place (quote.php is on the root folder but the button is trying to link to the current subfolder)
3. All of the items on the breadcrumb trail link to the current page
4. All of the links in the footer are also trying to point to the subfolder lightboxes
Is there anything you can think of to fix these issues?
Thanks,
Tony
The URL for the images should be something like this https://www.w-co.co.uk/images/worldpay/visa.gif
You have to use base URL https://www.w-co.co.uk and then image path. You can define("BASE_URL", "https://www.w-co.co.uk") and use it like in the following.
<?php
define("BASE_URL", "https://www.w-co.co.uk/")
?>
<img src="<?php echo BASE_URL;?>images/worldpay/visa.gif"

Links not working from subdirectories

I want to make multiple pages on my website, but to keep everything clean I want to make different directoriess with the different pages. However, I use php to make a different file with my header that is included in all my pages, so I only have to change the code of my header once and it will be the same on all pages.
The problem is that the links I use in my menu items (like home, contact, about, etc.) will not work anymore when you're on a page inside a directory (I'll make an example below).
So my question:
Is there a home folder on a website (like ~/ on unix) or is there another way to make it work?
Example of my directory structure:
htdocs
index.php
header.php
menus
contact.php
about.php
(a link to index.php won't work anymore if you're on the contact.php page)
Sounds like you're using relative paths in your menu links. Use an absolute path instead by starting with a "/":
Home
About
or a complete URL:
Home
About
The home directory of a website can be accessed with a simple '/' at the start of the link you want to add. From there you can enter subfolders by appending the folder name.
Example:
'example.com/subfolder/subsubfolder/page.html'

How to point a link retrieved form a database to an absolute path in PHP so it work?

I have a database that stores the link. This value "link" is later read by my script and a user can click on a link to go to that link.
The links work with no issue when the user is on any page that is saved/hosted in the root directory /myroot/
But when a user clicks on the link while on a page the is saved/hosted on a different path saved on (ie. /myroot/news/feed.php) the link does not work.. "it seems to me that the script is not ale to tell where it is currently located so it does not know where to take you)
I tried to provide the root path of the site (ie. /myroot/{the link} but that did not work either.
How can I fix the URL so that the script knows where is the true location of the file?
thanks
You need to specify a starting point so you can give absolute links instead of relative links. Look through the $_SERVER[] array, there are some elements that tell you where your directory location is on your site.

How to make links work, regardless of location in directory structure?

So in the included image, you can see a copy of the website I'm currently working on. The site is going to have separate pages for each of the company's machines. Obviously the navigation bar and menu bar are going to be the same throughout the whole website so I thought I'd make use of a single line of PHP script to just load the menu in each time.
http://i.imgur.com/spVS3a4.png
(Attached image as I do not have +10 rep yet)
Though, from what I understand, the pages within folders (Not within root) wont be able to link to the rest of the site without the use of ../ within the href.
So if I load the menu part of the site containing the navbar, etc, it won't work on all the pages because they'll all be within different folders, meaning every folder will need it's own version of the navbar PHP import.
Am I going about this the wrong way? I'm just trying to minimize the amount of code I'm using on each page. Not only that, but once I finish the site if I have to make a change to the menu-bar, I don't want to have to update 30-40+ pages. I figured the PHP import would save me that sort of trouble? Is there a better way of doing this that isn't terribly complicated?
It should also be noted that I'm doing this in HTML5 with Twitter-Bootstrap as my main CSS.
I would also be making a similar PHP call that would import a footer as well.
If I missed anything that could be of help, please do ask.
TL;DR - How do I make links work throughout the entirety of the site, regardless of where they are in the folder hierarchy. Thought about using a tiny PHP script that would contain the menubar, etc and would be loaded on every single page. Though I'm not sure if that will work due to folders?
Try to start your links from /. This means that url is started from site root.
For example:
Patch Kettles
Just define a constant with the root path and echo it within all the links inside the pages that are contained in a folder like this:
[Put this in a file that you include through out your whole website (e.g. like the file with your database connection if you have one)]
define('BASE_URL' , 'http://'.$_SERVER['HTTP_HOST'].'/');
Than just add an echo in the start of each link:
Home
And for images the same thing:
<img src="<?php echo BASE_URL; ?>images/img_01.jpg"/>
You can just use /path/to/image.jpg to get absolute paths, while still being fine if your domain name ever changes.

subdomain changes image path

I have one dynamic website with back end control panel which contains ckEditor for data entry. ckEditor inserts images with path like: /userfiles/image1.jpg and when I call page to display on user interface it shows perfect if I access it from main url but due to some reasons few section we have to move on a sub folder and when I access same page from that subdomain it fails in displaying image.
when I checked url of the image, it shows like:
app1.mysite.com/userFiles/image1.jpg But the actual path is:mysite.com/userFiles/image1.jpg.
Is there any trick or technique to solve this?
I am using PHP.
You can define a <base> in your <head>:
http://www.w3schools.com/tags/tag_base.asp
To do ist with PHP is hard. Changing it beforehand to an absolute URL might be the best way, changing it afterwards means HTML scanning, finding the images, check wether the url is absolute or relative and change it, if needed.

Categories