srisar
srisar

Reputation: 1571

How can I create a absolute url for my site using php?

Hi I am trying to crate a website using php, and the structure looks like the following:

-index.php
-header.php
-footer.php
-style.css
-images\{images are in this folder}
-\web\index.php
-\contact\index.php

The thing is i am using php's include function on all the index.php files to use the header.php and footer.php files, but the problem is that only the index.php in the root folder is showing the styles, but when i open localhost/web/index.php the page looks broke as the css and images are not found.

How can i resolve this problem.

Help please

Upvotes: 1

Views: 147

Answers (5)

Sujay sreedhar
Sujay sreedhar

Reputation: 3518

add site name in a verible like

$site_base_url = "http://localhost/web";

after that add it in front of url when u using php like

echo '<link type="text/css" href="'.$site_base_url.'/style.css" rel="stylesheet" charset="utf-8">'

or like

echo '<img src="'.$site_base_url.'/images/file_name.jpg" />'

when you use functions use global like this

function find_user(){
global $site_base_url;
echo "no users found";
echo '<a href="$site_base_url">return home</a> 
}

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157915

very easy.
you have to start every path from the site root, which is simply /.

so, addressing style.css as a /style.css will let it be called from any page of the site.
same goes for all the local hyperlinks on the site - just start them from the / (followed by the correct path of course)

Upvotes: 0

Georg Engel
Georg Engel

Reputation: 917

in your style.css you can use

background-image:url(/images/mycoolpic.png);

Upvotes: 0

Frankline
Frankline

Reputation: 41025

Try this structure:

  • index.php

  • styles/style.css

  • templates/header.php

  • templates/footer.php

  • images/{Images go here}

I strongly suggest you read this tutorial. It was really helpful and easy for a beginner.

Upvotes: 0

k102
k102

Reputation: 8079

you can require_once($_SERVER['DOCUMENT_ROOT'].'\header.php');

Upvotes: 0

Related Questions