公用PHP在不同路径的文件中被引用时,就要让公用PHP文件中的href为一个绝对url,考虑到我们的网站变化移植等因素,我们采用动态获取相对url的办法。
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<?php $nowUrlcatalog='http://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'],0,strripos($_SERVER['PHP_SELF'],'/')); ?>
<link rel="stylesheet" href="<?php echo $nowUrlcatalog.'/css/index.css'; ?>">
<link rel="icon" href="<?php echo $nowUrlcatalog.'/src/image/logo.ico'; ?>" type="image/x-icon">
<link rel="shortcut icon" href="<?php echo $nowUrlcatalog.'/src/image/logo.ico'; ?>" type="image/x-icon">
</head>
<body>
<div class="top"></div>
<div id="guide">
</div>
</body>
</html>
本文介绍了一种使用PHP动态获取当前页面目录的方法,并将其应用于构造CSS和图片资源的相对URL路径,确保了即使在网站结构发生变化时,链接也能正确地指向所需资源。


被折叠的 条评论
为什么被折叠?



