subdomain changes image path - php

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.

Related

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.

Why page showing content when browse to site.com/file.php/

I got notifications from Google webmaster tool that I have duplicated page titles.
It was properly to browse mysite.com/file.php but I do not know why mysite.com/file.php/ is showing some of content by file.php.
Please advise how to remove/disable content showing when browse mysite.com/file.php/
1st: You might have a filename file.php and directory file.php. Check those out.
2nd: You might have .htaccess rewrite rule, that messes stuff up, if you do check out the syntax on that.
When you hit /file.php, or /file.php/ what changes is pathname that the browser "sees".
In first case, it is /, in second case it's /file.php.
Hence, when you use relative path (to images, stylesheets, etc), in second case the browser will send requests to wrong URIs.
To avoid that, use <base url="http://example.com/path/to/site/root/"> in your HTML

Loading a page without changing URL and keep relative paths working

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/" />

Hiding img src paths from code?

I suspect that this is impossible, but I'm trying to be optimistic...
I'm running a site that uses jquery & php. Via ajax, I'm dynamically loading images to a page. The problem is that the paths to the images are visible when you view the source of the page. I'm trying to figure out if it's possible to prevent the path from displaying? I just don't want anyone seeing those paths or being able to access them. So, I don't know if there's some sort of magical programming solution or something to do with htaccess, but if anyone knows how to accomplish this, I'd appreciate pointers / links / or brilliance...
As the browser needs to know where to fetch the data from, it cannot be hidden easily.
But you can it make the "attacker" a little bit harder. E.g., you can give URLs with a script behind loading the given images. The script does so only if called with the correct Referer: header.
Are Data URIs an option for you?
If not, preventing hotlinking (via Referer) would be the way I would do it, possibly with some obfuscation (depending on your user base).
Everything that's visible to the browser is visible to the clever user in one way or the other
You can hide them from lamers, but not from programmers
Anti-lamer techniques can include:
1) setting data-mangled-src attribute to something like ROT13(real-src), then doing
$('img').each(function () { $(this).attr('src', ROT13($(this).data('mangled-src')))})
2) setting some data-id attr, then AJAX-requesting the src from the server
But anyway, the URL goes to the browser, then it's visible to the user
3) And, of course, you can load your images into flash 'viewer', which is probably the most advanced way of hiding them
So... If you make the load path an internal link in php Requested through Ajax that should hide the path in network inspector... By this I mean maybe query a db that generates a temp path to the image that expires...
Then with the loaded image you could save it a canvas element... Thus no src
There are a few ways you can go about hiding image URLs.
Use a plugin: you can use flash, a java applet, etc to load the images
Use a traffic cop: you can set the source of all the images to a single PHP script, with some kind of parameter to specify which image you want. That script can check sessions variables / referer / whatever you want; if everything checks out, you can do a file_get_contents() to load the actual url of the image, then echo that out to the browser.
I've created a website to help you hide the image src of your precious photos and images.
My website will hide the source path of your image, and it also allows you to 'Lock' your photo to a specific domain eg. example.com so that the photo will only display example.com and not on facebook.com or wherever the link to the photo is reposted.
http://hideimgsrc.com
It's easier and faster than a java script based solution.

The url of the images if I use a subdomain

I have a problem that is related to the URL of the images if I use a subdomain.
For example, if the image is located at address
http://www.mydomain.com/images/photo.jpg
And if I want to use subdomain to speed up page load, in this case subdomain is:
http://img.mydomain.com
How should look like the url to the image foto.jpg if I use subdomain?
The path for subdomain:
/home/mydomain/public_html/img/
The path for images folder:
/home/mydomain/public_html/images/
just add an alias to your img subdomain and keep the same structure. This way if you need to change in the future it will be transparent
edit:
is also good to have more than one subdomain for assets. like img01 img02 and so on. But be these subdomains consistent.
like if you have the image image1.jpg that targets to
http://img01.domain.com/images/image1.jpg
the next time the imagen appears in the code should appear as
http://img01.domain.com/images/image1.jpg
AND NOT as
http://img02.domain.com/images/image1.jpg
so the cache can optimize the calls.
I usually use this formula
return sprintf("http://img%02s.domain.com", abs(crc32($imagename) % (9)));
this way the subdomain will be always consistent with the image name
edit 2:
Browsers limit the amount of connection opens peer server. So by having multiple subdomains you are faking this and therefore improving the load speed of the page.
But in the other hand if for a same image you load it from more than one different server the browser's cache can't operate because it doesn't know that you want to load the same image.
So by hashing the image name you are obtaining always the same server and then obtaining the best of both worlds: more connections opens AND browser caching
If your subdomain's document root is:
/home/mydomain/public_html/img/
And your images at are:
/home/mydomain/public_html/images/
Then you will not be able to access them. You'd have to navigate to http://img.mydomain.com/../images/photo.jpg which, for very obvious security reasons, is not possible.
Your subdomain's document root should instead be:
/home/mydomain/public_html/images/
And then your URL is:
http://img.mydomain.com/photo.jpg

Categories