Find and Append String Regex Php - php

I'm very new to php, but this problem may require regex manipulation. Basically all "" in my website points to localhost (i.e. img src="http://localhost/folder/img.png" ).
When viewed under source, the "localhost" doesn't show up, but if clicked to get image address, it'll show "localhost". What i need is then to add the full path to that img src (i.e. img src="http://mywebsite.com/folder/img.png" ).
How do i go about doing this?
Any help would be greatly appreciated.
Thanks a lot

Use absolute URLs in your project to reduce conflicts and messy overheads. To use absolute URLs, just define a global constant BASE_URL in your common file like:
define("BASE_URL", "http://localhost/project_folder", true);
Now use it everywhere, like:
<img src="<?php echo BASE_URL.'/img/image.png' ?>" />
In source code, generated html will look like: <img src="http://localhost/project_folder/img/image.png" />
Similary you can use this like:
<script type="text/javascript" src="<?php echo BASE_URL ?>/js/my_script.js"></script>

Related

Dynamic header filepath

I am writing a website and I decided to separate my header into it's own php file and then use the include statement to insert it back into my webpage. The issue I'm having is on my index.php I'm sourcing my logo as such:
<img class='logo' src='images/logo.png'>
and on my meats/sausage.php page I source it as such:
<img class='logo' src='../images/logo.png'>
Is there a superglobal or something I can use instead to direct both pages to the correct location or do I need to make separate header versions for each of the 2 pages?
Note: I'm using XAMPP and localhost rather then running it on the web just yet.
Use a constant or something that defines your base directory and work with that.
For example:
<?php
$baseDirectory = __DIR__;
function assets_path($append)
{
return "{$baseDirectory}{$append}";
}
And then:
<img class="logo" src="<?= assets_path('images/logo.png') ?>">
Choosing where to place this is on you.
A possible option would be to use .htaccess file and the RewriteRule like so:
RewriteRule ^(.*)SiteAssets/(.*)$ http://YourSite.com/AssetsRoot/$2
And then use like this:
<img class="logo" src="SiteAssets/images/logo.png">
Also, if your DocumentRoot setting is correctly set in httpd.conf, you could simply use this throughout your site:
<img class="logo" src="/images/logo.png">
See https://stackoverflow.com/a/7823598/715105
Base tag
<head>
<base href="https://example.com/">
</head>
This will set your base on every page. No need to use ../images
see https://www.w3schools.com/tags/tag_base.asp
Duplicate of this question:
src absolute path problem
The trick was to reference it as such:
<img src="http:\\localhost\site\img\mypicture.jpg"/>
but with forward slashes rather then backward slashes

Issue in loading image file in PHP

I am facing some typical issue in loading image files in PHP. Image got successfully loaded but cannot get displayed properly.
I have declared a define value, then echoed it in an img tag src attribute :
define('image_site_url','localhost/projects/faantush/product_images/');
<img src="<?php echo image_site_url.$row['image1'] ?>" />
The image file is not found but it is there in product_images. Here are the pictures:
In the HTML code: You are missing the: http:// before localhost
<img src="http://localhost/ ...
If your pictures are hosted on the same server than your website, you don't really need to use an absolute path. You could use a relative path and remove the domain name (localhost in this case):
<img src="/projects/path/to/your/picture.jpg" />
You must use absolute paths when linking to another website, but you can also use absolute paths within your own website. This practice is generally frowned upon, though. Relative links make it easy to do things like change your domain name without having to go through all your HTML pages, hunting down links and changing the names. As an added bonus, they force you to keep your site structure neat and organized, which is always a good idea.
An interesting use case of not using absolute paths is, if you want to set your website to SSL/TLS: you will have to change all the occurrences of http to https. Although it is not a big deal, this is generally not the kind of work you want to do.
See Relative path or url for html src and href attributes and the post from where this quote is taken.

PHP template system, image paths

I'm making a basic templating system in PHP, the problem is:
I have a main request handler where from where everything is loaded and processed, but because of that the paths inside the CSS template I downloaded aren't correct anymore.
Example :
<img src="assets/images/contact.gif" />
has to be:
<img src="templates/grey-box/assets/images/contact.gif" />
Is there any way to fix this, PHP wise?
Don't be lazy
Always use an absolute path.
Make it explicit, not with some dirty hacks. It will be a support nightmare.
You can use some helper variables, like $tpl_assets_path and use it in the template.
Absolute path starts from / where / designates web-seite root.
And every path on your HTML page should be.
So, the template should be either
<img src="/templates/grey-box/assets/images/contact.gif" />
or
<img src="<?=$tpl_assets_path?>images/contact.gif" />
define('HTTP_SERVER', 'http://www.myhomeurl.com/');
<img src="<?php echo HTTP_SERVER?>assets/images/contact.gif />
it's here
$imgarray = array('assets','script','css');
$tpl = '/templates/grey-box/';
foreach($imgarray as $i){
$tpldir = $tpl.$i;
$str = preg_replace("/([^\/])($i)([\/])/i","\$1$tpldir\$3",$str);
}

While using mod_rewrite what is the common practice to render right path to the img/css/js files?

On the one hand mod_rewrite allows me to make userfriendly urls redirecting requests to proper script but on the other I can't use relative links because they will point at nonexistent files.
A little example:
mod_rewrite redirects request
http://site.ru/param/one/page.html
at http://site.ru/script.php
while I have "myimg.jpeg" file at the same folder I can't use relative link <img src="myimg.jpeg /> cause in that case the browser will try to load img from the "http://site.ru/param/one/myimg.jpeg" address.
While using mod_rewrite what is the common practice to render right path to the img/css/js files?
I have 2 ideas about the realization:
1) to add base tag like that <base href="http://site.ru/" />
2) to define a variable with the baseUrl and then use it while rendering src attributes like that:
$baseUrl = 'http://site.ru/';
...
echo '<img src="' . $baseUrl . 'myimg.jpeg" />
Which is the best solution and are there any other ways to solve the problem?
Don't use relative URLs. In almost all cases, a $baseUrl variable is unnecessary - just reference things by their path from the site's root.
i.e. <img src="/images/myimg.jpeg" /> instead of <img src="images/myimg.jpeg" />

How to keep a website with url routing directory independent

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
?

Categories