Including headers and CSS in different directories - php

This must be a simple fix, but I just can't figure it out!
I am including the header via PHP into each new page, the header contains the CSS and scripts etc.
Only problem is since the page is in a different directory, when I link to the header like ../header etc it looks fine but the included scripts, are the included via a short URL e.g. /js/script.js
Which means on the page (in another directory) the scripts do not work!
I'm finding it hard to explain but take a look at this:
http://www.healthygit.com/
If you view the source all the scripts link fine.
Now look at this:
http://www.healthygit.com/fitness/running.php
If you try to click on a script to view it, it takes you to a 404 or in this case 302's you to the homepage.

Easy peasy, your source for the scripts, stylesheets etc. should have a / in front of them, that way the header file will always refer to the files from the root of the site.
I.e. this:
src="js/whatever.js"
Should become
src="/js/whatever.js"
This way it will always look for the files from the root of the site.
The same applies with CSS files, i.e. /css/whatever.css.

Related

loading a template in php

I'm building a small CMS in php but I have a problem with the front end. I have a folder name template inside the folder I got different themes
-admin
-template
---theme1
---theme2
---theme3
-index.php
when I load the index.php I can load the theme, but in the browser URL I get localhost/cms/template/theme1/page.php
but I like to have localhost/cms/page.php instead.
will you please tell me when I'm doing wrong!
Thanks.
I'm a little confused...you say this is a front end issue, but you point to the URL as the issue.
My guess is this: You need to identify different themes using separate stylesheets in CSS. Having different pages called page.php that looks differently definitely requires more work and complicates the issue. CSS was designed for you to customize the look of different pages and/or templates. Why not create 3 different stylesheets and then create a simple form that allows the administrator to choose which stylesheet to use (by radio button, or something else).
You can determine which stylesheet is "in use" in a number of ways - either on the back end or the front end. Given that this is a CMS, you'll probably want your administrators to choose the stylesheet and allow their selection to update a database entry or change a file on the back-end. No matter what you choose, you're URL's probably should not be affected.
Finally, you can rewrite URLs with .htaccess, but that can lead to more issues as you develop the site. Generally speaking - htaccess is overkill for issues like this.

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.

How do I preload .css and .js files, before the whole page (dynamic by PHP) is generated

My homepage (index.php) is generated dynamicly by PHP, so it takes about 1s till the the page is delivered to the user.
Because my css and js script is in the header of (index.php), after this 1s the css and js will start to download.
Is there some general way of preloading this js and css files?
My aproach would be to delete all content of index.php, include just <'head> with my css and js file, and then do some js to load the whole indexOriginal.php (with the dynamic content generated on my server by PHP), so this should enable the user to download the css, js in parallel of indexOriginal.php.
Is this the right aproach?
The approach you suggested could work. You also might consider altering your PHP script to hurriedly deliver the first part of the page (<html> down through <body>) before it starts getting the rest of the page together (see http://php.net/manual/en/function.flush.php).
Your page full generated by backend and sended to user only after php script end its work.
You could, for example, create page like.
<html><head><script></script></head><body></body></html>
In <script></script> load, for example, JQuery and do something like
$(document).ready($('body').load('/path/to/your/script.php'))
But I really thing this wouldn't be a good practice. Also I don't think that Google would parse your site content with this solution.
I think you should look better about your JS (compress, gzip, split to many files and upload them from different CDNs, move everything to $().ready, etc.)
My recommendation would be to have your initial PHP script JUST deliver the CSS and JS tags. Once those are loaded, use the JavaScript to make an AJAX call that gets the rest of the page then displays it. Or you can build the page in the JavaScript (which would allow you to use loading icons and the like to give a better indicator to the user that "the page is coming")

stop google indexing php page that redirects

I have a (randomly named) php file that does a bit of processing and then uses a header("location:url") to redirect to another page.
As mentioned, the script has a random name (eg: euu238843a.php) for security reasons as I don't want people stumbling upon it.
Thing is - How do I stop Google from indexing it - I want other pages in the same directory to be indexed but not this php file. I don't want people to be able to do a site:myurl.com and find the "hidden" script.
I would normally put a meta name="robots" content="noindex" in the head but, I can't do that as the page needs to ouput headers at the end to redirect.
You can dynamically updated the robots.txt file within the directory using a PHP script that outputs a new or appended robots.txt as needed. If you specify each dynamic filename on a new line, such as Disallow: /File_12345.html, you can avoid having to disallow the entire directory.

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

Categories