HREF="" 自动添加到当前页面 URL(在 PHP 中)。想不通

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8764288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:27:10  来源:igfitidea点击:

HREF="" automatically adds to current page URL (in PHP). Can't figure it out

phphtml

提问by Evan Hobbs

Longtime reader of stackoverflow but first question.

stackoverflow 的长期读者,但第一个问题。

I'm working with Wordpress (specifically thesis theme) in the custom_functions.php file and am finding for some reason is automatically adding the current page url. For example this code is to query the database and then loop through outputting each product in it's own div:

我正在 custom_functions.php 文件中使用 Wordpress(特别是论文主题),我发现由于某种原因会自动添加当前页面 url。例如,这段代码是查询数据库,然后循环输出每个产品在它自己的 div 中:

$result = db_connection($query);
while ($row = mysql_fetch_array($result)) { ?>
    <div class="box"><a href="">
        <img src="http://www.electricbikehub.co.nz<?php echo $row['product_root_directory']         . $row['mid_size_image'] ?>">
        <h2><?php echo $row['name']?></h2>
        <p><?php echo $row['description_brief'];?></p>
        <p><span class="multiple_product_red"><span class="multiple_product_linethrough">RRP: <?php echo $row['list_price']; ?>.</span> Our discounted price: <?php echo $row['our_price']; ?>. Includes delivery and GST.</span></p>
        </a>
    </div>
<?php } ?>

As you can see 3rd line says href="" but the actual link being generated is the current page (in this case 'http://www.electricbikehub.co.nz/?page_id=1192'). If I add anything in the href, such as href="something" it will just add it to the end, ie http://www.electricbikehub.co.nz/?page_id=1192something.

如您所见,第 3 行表示 href="" 但生成的实际链接是当前页面(在本例中为“ http://www.electricbikehub.co.nz/?page_id=1192”)。如果我在 href 中添加任何内容,例如 href="something" 它只会将其添加到末尾,即http://www.electricbikehub.co.nz/?page_id=1192something

Any help would be greatly appreciated!

任何帮助将不胜感激!

回答by Wes Crow

This is how a browser interprets and empty href. It assumes you want to link back to the page that you are on. This is the same as if you dont assign an action to a <form>element.

这就是浏览器解释和清空 href 的方式。它假定您想链接回您所在的页面。这与您未将操作分配给<form>元素的情况相同。

If you add any word in the href it will append it to the current page unless you:

如果您在 href 中添加任何单词,它会将其附加到当前页面,除非您:

  • Add a slash /to the front of it telling it to append it to your base url e.g. http://www.whatever.com/something
  • add a #sign in which case it is an in-page anchor
  • or a valid URL
  • /在它的前面添加一个斜杠,告诉它把它附加到你的基本网址,例如http://www.whatever.com/something
  • 添加一个#标志,在这种情况下它是页内锚点
  • 或有效的网址

EDIT:It was suggested that I add a link to help clarify the situation. I found the following site that I think does a really good job explaining the hrefattribute of anchor tags and how it interprets URL paths. It is not incredibly technical and very human-readable. It uses lots of examples to illustrate the differences between the path types: http://www.mediacollege.com/internet/html/hyperlinks.html

编辑:有人建议我添加一个链接来帮助澄清情况。我发现以下网站在解释href锚标记的属性以及它如何解释 URL 路径方面做得非常好。它不是令人难以置信的技术性和非常易读的。它使用大量示例来说明路径类型之间的差异:http: //www.mediacollege.com/internet/html/hyperlinks.html

回答by Mukesh

Add http:// in front of url

在 url 前添加 http://

Incorrect

不正确

<a href="www.example.com">www.example.com</span></p>

Correct

正确的

<a href="http://www.example.com">www.example.com</span></p>

回答by Rikin Adhyapak

if you want to redirect it to some other url lets google.com then make your like as happy to help other says rikin<a href="//google.com">happy to help other says rikin</a>this will remove self site url form the href.

如果你想将它重定向到其他一些 url,让 google.com 然后让你喜欢 帮助其他人说 rikin<a href="//google.com">happy to help other says rikin</a>这将从 href 中删除自我站点 url。

回答by strix25

You can just put // in front of $yourUrl in href:

你可以把 // 放在 href 中的 $yourUrl 前面:

<a href="//<?=$yourUrl?>"></a>

回答by Kai Qing

You do realize this is the default behavior, right? if you add /something the results would be different.

您确实意识到这是默认行为,对吗?如果添加 /something 结果会有所不同。

you can do a number of things to prevent default behavior.

你可以做很多事情来防止默认行为。

href="#":

href="#"

Will do nothing but anchor - not the best solution since it may jump to page top.

除了锚点什么都不做 - 不是最好的解决方案,因为它可能会跳到页面顶部。

<a href="#">

href="javascript:void(0);"

href="javascript:void(0);"

Will do nothing at all and is perfectly legit.

什么都不做,完全合法。

<a href="javascript:void(0);"></a>

href="your-actual-intended-link"(Best)

href="your-actual-intended-link"最佳

obviously the best.

显然是最好的。

<a href="<your-actual-intended-link>"></a>


If you don't want an atag to go somewhere, why use an atag at all?

如果您不想让a标签出现在某个地方,那为什么还要使用a标签呢?

回答by Andreas Fiske

A solution that works no matter if you are developing on a local server or live is to add "//" in front of your link. This will effectivly remove the URL of the site you are currently on.

无论您是在本地服务器上开发还是在线开发,都可以使用的解决方案是在链接前添加“//”。这将有效地删除您当前所在站点的 URL。

Example:

例子:

<a href="www.google.com">Google.com</a>

This will in output http://localhost/mySite/currentPage/www.google.com

这将在输出http://localhost/mySite/currentPage/www.google.com

What you should do instead is this:

你应该做的是:

<a href="//www.google.com">Google.com</a>

This will output www.google.com

这将输出www.google.com

回答by Ben D

In any case, your code will generate invalid markup: You shouldn't wrap block contents in a link. tags don't work like this. If you want this effect you should use js or create an absolutely positioned link above the content (z-index). More on this here: Make a div into a link.

在任何情况下,您的代码都会生成无效标记:您不应将块内容包装在链接中。标签不是这样工作的。如果你想要这种效果,你应该使用 js 或在内容上方创建一个绝对定位的链接(z-index)。更多相关信息:Make a div into a link

You should make sure to validate your code when it renders: http://validator.w3.org

您应该确保在呈现时验证您的代码:http: //validator.w3.org

回答by Frederick Marcoux

Use this:

用这个:

<a href="<?php echo(($_SERVER['HTTPS'] ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); ?>">Whatever</a>

It will create a HREF using the current URL...

它将使用当前 URL 创建一个 HREF...