webpage http URL issue - php

When i try to open my page with normal request something.com my page's few animation is not working properly but if i do the same by typing full address i.e
http://something.com everything is normal. I tried using php to fix this issue.
<?php
header("Location: http://www.something.com/home.html");
?>
However, it's not working.

This is most probably the case because you hard coded your base path into the path of the assets you are loading for these animations.
Wrong:
<html>
<head>
<script type"text/javascript" src="http://www.something.com/my-asset.js"/>
</head>
Right:
<html>
<head>
<script type"text/javascript" src="/my-asset.js"/>
</head>
If you want full, absolute paths you can also dynamically set a basePath variable and use this in front of the asset path but it's not necessary in many cases.

Related

Is it possible for an HTML file to point to an external set of links?

I'm building a site that allows users to make games within it. I wanted to know if the in the main .html file, could I have a set file that has all the scripts and stylesheets and then link that file in my main .html file? It's kind of like:
index.html
<head>
<link rel="???package???" href="package.php">
<title>Make a Game!</title>
</head>
package.php
<head>
<link rel="stylesheet" href="standard.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
... etc ...
</head>
My main reason for doing it in PHP is so I can control if the current developer is premium or not and if they are, give them a couple more libraries to work with.
looks like you just want to include one file in another so :
include 'package.php';
is all you need, remove the <head> </head> as you don't want them twice
You can use ajax you have to read that file with AJAX and set the output of ajax wherever you want in your html.
But for that you have to echo your whole php file.

CSS file being ignored?

I have a contact page on my site set up like this;
contact.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="webpage.css">
<?php include 'header.php'; ?>
</head>
...rest of content
I recently set up an htaccess page with some mod_rewrite changes.
So now, I have urls like
www.example.com/user
and
www.example.com/user/contact
When I look at www.example.com/user/contact the CSS is not recognized and the page does not print any of my divs but just some worded content.
So if I have
<div id="userinfo">
User info
</div>
It just displays "User info" on the left of the page without the div being recognized.
How can I fix this problem?
The href in your CSS is a relative link. So for www.example.com/user the browser will request the css file at www.example.com/webpage.css, but for www.example.com/user/contact it will request www.example.com/user/webpage.css
That may cause part of your problem and can be fixed by preceding your hrefs with a /, so href="/webpage.css". The / will cause the browser to always request from the root of your domain so it will always request www.example.com/webpage.css regardless of the page you're on. If your css is inside some folder within your DocumentRoot (I assume you're using Apache here), you should be providing the full path to the file from the start of the doc root so possibly something like href="/css/webpage.css"
In the comments, Michael is also correct that you may have a rewrite rule error so please add that information to your question.
Use absolute URL beginning from /:
<link rel="stylesheet" href="/webpage.css" />

CSS only working inline with PHP API

For some reason my CSS is only working when inline. I'm somewhat new to web design and not really sure about how to track down why this is occurring. It's not like I can open the console to check for errors, so what kinds of things should I look for? I'm sorry I'm not being more specific, I'm just really not even sure what to do.
Say the url is "www.example.com/testing/3"
The page testing.php would load, call API.php and determine what to do when on page testing.php with vale 3. API.php would then call a function on testing.php to deal with whatever that function tells it to do. So basically it calls out, determines which function to handle the URL, then calls back in.
When I include the CSS in the head of testing.php it works. When I just have a link, it doesn't. All of the files are in the same folder and I'm developing on localhost, no files are admin-restricted: I'm the admin anyway so permission isn't an issue.
//test.css
P.special {
color:green;
border:solid red;
margin-top:85px;
}
Head of testing.php
<link a href="test.css" rel="stylesheet" type="text/css"/>
A function on testing.php
function view_event($event_id){
?>
<p class ="special">
//stuff here
</p>
<?
}
<link a href="test.css" rel="stylesheet" type="text/css"/>
would need to be changed to:
<link a href="/test.css" rel="stylesheet" type="text/css"/>
Using relative URLs will not work when you traverse into other directories. Setting it to an absolute URL with / will allow it to work anywhere on site.
You can check the generated page source in your browser and make sure the CSS link is valid and loads the intended CSS file.
Web Developer Tools built into your browser (or as plugins) are very helpful in debugging html, css and js. Get to know them well.

Web development - relative URLs without duplicating files

I have a site with index.php in the root folder, images in /img , and overview.php in /content. I have a sidebar.php file that is included in both index.php and overview.php. How should I refer to /img/image.gif if I include a link in each file?
The location of image.gif changes relative to the location of the file that references it.
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php.
The only solution that I can see is to either include a separate sidebar.php in each sub-directory, or include an /img directory in every sub-directory.
The best suggestion that I can find is to use the <base> HTML tag as suggested here:
Change relative link paths for included content in PHP
However, in the same link, SamGoody suggests that the <base> tag is no longer properly supported in Internet Explorer, since version 7.
I'd like some insight on the matter before committing to a course of action.
Thanks.
EDIT: I am using the wrong approach below with "../"
Example-
root/index.php:
...
<link rel="stylesheet" type="text/css" href="style.css" />
<title>title</title>
</head>
<body>
<?php include('include/header.php'); ?>
<?php include('include/menu.php'); ?>
...
root/include/header.php:
...
<div id="header">
<span class="fl"><img src="img/dun1.png"/></span><span class="fr"><img src="img/dun2.png"/></span>
...
root/content/overview.php:
...
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />
<title>Overview</title>
</head>
<body>
<?php include('../include/header.php'); ?>
<?php include('../include/menu.php'); ?>
...
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php
But it shouldn't. The preceding / makes it an absolute path which will work from any point on the server. If this doesn't work for you, there's a problem somewhere - in that case, post some examples.
Unless you are planning to move the whole site into a sub-directory one day, or move images to a Content Delivery Network (both actions would require re-writing the addresses) you can safely use absolute URLs.

HTML Base URL and links

I have the following problem:
The URL is http://www.myhomeurl.com/application1/ and the base is:
<base href="http://www.myhomeurl.com/"/>
All resources like images, css and javascript will be at:
http://www.myhomeurl.com/css/myfile.css
http://www.myhomeurl.com/js/myscript.js
http://www.myhomeurl.com/images/img.jpg
BUT, the link will be at "application1", for example:
http://www.myhomeurl.com/application1/page1.html
http://www.myhomeurl.com/application1/page2.html
http://www.myhomeurl.com/application1/page3.html
The question is: How to apply base URL for resources (like css, js, etc) and apply the base/application1 for page links?
Here is a problem when I have:
Click me!
When the user clicks this the page will going to:
http://www.myhomeurl.com/page1.html
and not to:
http://www.myhomeurl.com/application1/page1.html
You could use a base tag and change it so all urls can be relative to the applications base.
<!DOCTYPE HTML>
<html>
<head>
<base href="http://www.myhomeurl.com/application1/" />
</head>
<body>
<!-- content -->
</body>
</html>
Additional information can be found at http://www.w3schools.com/tags/tag_base.asp
edit: w3c spec on base tag http://www.w3.org/TR/html4/struct/links.html#h-12.4 also illustrates how to pull images from other locations.
Change like this on your resources
<link href="./css/myfile.css" rel="stylesheet" type="text/css" />
Not like
<link href="http://www.myhomeurl.com/css/myfile.css" rel="stylesheet" type="text/css" />
And your base it looks like this
<base href="http://www.myhomeurl.com/application1/"/>
Use this varriable in link HTTP_SERVER
define('HTTP_SERVER', 'http://www.myhomeurl.com/application1/');
Click me!
I'm a little late for this question, but I can see none of the answers really do what (I think) you need.
I suggest you to use (as others have already said) a Base Url like this:
<base href="http://www.myhomeurl.com/application1/" />
This way your links will work in the intended way. Then, when you add resources, you only have to go up to the parent directory, like the following:
<link href="../css/myfile.css" rel="stylesheet" type="text/css" />
Or
<style type="text/css">
#import url(../css/myfile.css);
</style>
Note the ../, telling the browser to exit from "application1" directory, going up one step in the file structure.
Hope this helps :)
you should have something like this
root
root/css
root/js
root/files/html1.htm
root/files/htmml2.htm
root will be nothing but /// your website name
http://www.myhomeurl.com
Define two constant one for your css, js resources and one for links and use them across the application:-
define('RESOURCE_URL','http://www.myhomeurl.com/');
define('LINK_URL','http://www.myhomeurl.com/application1');
so that if any change in base url you can look up to these constants only.
I would start all link herd with /and the top directory of the URI. That makes them relative to the domain. Then just put the protocol and domain in the base.
when using base url you should not put www. in it.
Next if your url is mysite.com and your app is located at mysite.com/app then you set your base to be mysite.com/app when you use your example info.html it will look like mysite.com/app/info.html.
You can also use ../app/info.html and it will look like mysite.com/app/info.html

Categories