如何使用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

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

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.

in your style.css you can use

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

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)

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> 
}