简体   繁体   English

Href链接和链接

[英]Href link and linking

Ok so i have a little problem now, that i hope someone would be able to help me resolve. 好的,所以我现在有一个小问题,我希望有人能够帮助我解决。

I've made a website in html, and after finishing the design i decided to break it up in php include lines where it would be easier to navigate and continue to code. 我用html制作了一个网站,完成设计后,我决定将其分解为php include几行,以便于浏览和继续编写代码。

For example: 例如:

HTML CODE HTML代码

<html>
<head></head>
<body></body>
</html>

And after breaking it up: 分解后:

PHP code PHP代码

include 'php/head.php';
include 'php/body.php';

It would all be OK if i didn't had to make a sub menu's and categories in another folder. 如果我不必在另一个文件夹中创建子菜单和类别,那一切都OK。 The main problem is that i have folder tree like this: 主要问题是我有这样的文件夹树:

nslife(root)
   images
   css
   js
   php
   novisad(folder that contains the sub categories)
   index.php

Ok so building the page as i did by breaking the code up works excellent on the main index page but after copy/pasting the same code on to another new page which is a sub category all of the href links didn't work. 好的,所以像我一样通过破坏代码来构建页面在主索引页面上效果很好,但是将相同的代码复制/粘贴到另一个新页面(属于子类别)后,所有href链接都无法正常工作。

For example: Primary index page: 例如:主索引页:

<img id="right-img" src="images/baners/baner500x60.jpg">

But after using it in my Secondary page from the sub folder "novisad" the image doesn't show. 但是,在子文件夹“ novisad”的“辅助”页面中使用它之后,图像不再显示。

I know it has to do something with ../ or // or anything else but tried every possibility and nothing works, and if it works for one page i doesn't work on another. 我知道它必须与../或//或其他任何方式一起工作,但是尝试了所有可能,但没有任何效果,如果它适用于一个页面,则无法在另一个页面上使用。

Potential solutions: 潜在的解决方案:

  1. Update all links to use an absolute path, or use ../ (which traverses up 1 level) to get to the right directory. 更新所有链接以使用绝对路径,或使用../ (遍历1级)进入正确的目录。 Or use a single / to indicate the root directory at the start of every path. 或使用单个/表示每个路径开头的根目录。

  2. Define some constant like ROOT_URL , setting it equal to the root address of your site, and prepend this to every path in your code 定义一些常量,例如ROOT_URL ,将其设置为等于站点的根地址,并将其添加到代码中的每个路径之前

  3. Use: $_SERVER['DOCUMENT_ROOT'] and prepend this to your paths 使用: $_SERVER['DOCUMENT_ROOT']并将其放在您的路径之前

Here's a good article on the subject which you might like to read. 这是一篇有关您可能想阅读的主题的好文章

You are currently using relative urls, so when you reference images/baners/baner500x60.jpg , you need to have a folder called images in the same directory as your file that is referencing that location. 您当前使用的是相对网址,因此在引用images/baners/baner500x60.jpg ,需要在与引用该位置的文件相同的目录中拥有一个名为images的文件夹。

For your purposes, use absolute urls instead. 为了您的目的,请改用绝对网址。 Something like http://www.yourdomain.com/images/baners/baner500x60.jpg . http://www.yourdomain.com/images/baners/baner500x60.jpg东西。 These will always reference the same location regardless of the location of the file that's referencing it. 这些文件将始终引用相同的位置,而不管引用它的文件的位置如何。

You definitely need the absolute path. 您绝对需要绝对路径。 Your code would be referring to a file like images/image.jpg but from the subfolder the actual path would be ../images/image.jpg . 您的代码将引用像images/image.jpg这样的文件,但在子文件夹中,实际路径为../images/image.jpg To fix this, use http://yoursite.com/images/image.jpg . 要解决此问题,请使用http://yoursite.com/images/image.jpg You may run into issues if your site sees these as external links and won't run scripts from them as a result, in which case you need to find an alternate solution (different includes for that folder, or modify the includes so that you rewrite the links depending on depth of the subfolder). 如果您的站点将这些链接视为外部链接并且因此不会从中运行脚本,则可能会遇到问题,在这种情况下,您需要查找替代解决方案(该文件夹的其他包含项,或修改该包含项以便重写链接取决于子文件夹的深度)。

对于img src属性,请使用绝对URL而不是相对URL,如下所示:

<img id='right-img' src='http://www.domain.com/nslife/images/baners/baner500x60.jpg'>

您可以使用完整地址http://url.com/images/images.png或将../images/image.png放在URL的上一级。

In my programming I set a variable called: $level = ''; 在我的程序中,我设置了一个变量: $level = ''; which is set at the top of the page. 设置在页面顶部。

At the top level it is: $level = ''; 最顶层是:$ level ='';

Sub Folder it is set as: $level = '../'; 子文件夹设置为:$ level ='../';

Sub Sub Folder it is set as: $level = '../../'; Sub Sub Folder设置为:$ level ='../../';

and so forth. 等等。 Then you simple prepend the all links with the variable. 然后,您只需在所有链接前添加变量。

$level.'css/style';

all of your links will respond correctly as a result. 结果,您所有的链接都会正确响应。

For larger projects I do this on my conn page: 对于较大的项目,请在conn页面上执行此操作:

$db_host = "localhost"; 
    $db_username = "????"; 
    $db_pass = "????"; 
    $db_name = "????";

    $db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ));

    //$site_url = 'http://'.$_SERVER['SERVER_NAME'];    
    $site_url = 'http://localhost/demo';

    # this page stuff   
    $pg_uri = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    $pg_url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
    $this_file = basename($_SERVER['SCRIPT_NAME'], ".php");
    $file_name = basename($_SERVER['SCRIPT_NAME']);

    # urls      
    $u_inc = $site_url.'/inc';  
    $u_panels = $site_url.'/panels';            
    $u_legal = $u_inc.'/legal';
    $u_forms = $u_inc.'/forms';              
    $u_admin = $site_url.'/admin';
    $u_plugin = $site_url.'/plugins';        
    $u_js = $site_url.'/js';    
    $u_regions = $site_url.'/regions';
    $u_menus = $site_url.'/menus';

    # paths
    $p_root = $_SERVER['DOCUMENT_ROOT'];     
    $p_inc = $level.'inc';
    $p_panels = $level.'panels';
    $p_legal = $p_inc.'/legal';              
    $p_forms = $p_inc.'/forms';
    $p_admin = $level.'admin';              
    $p_plugin = $level.'plugins';
    $p_js = $level.'js';                    
    $p_regions = $level.'regions';  
    $p_menus = $level.'menus';

Once the vars are committed to memory it flows with the rest of your programming. 一旦将var提交给内存,它将与其余的程序一起流动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM