简体   繁体   English

获取当前页面的URL

[英]Getting url of the current page

I am trying to get the website url of the current page on which the person is. 我正在尝试获取该人所在的当前页面的网站网址。 I would use it in the social share buttons that I am creating. 我会在我创建的社交分享按钮中使用它。 The websites are all Wordpress websites. 这些网站都是Wordpress网站。 I got the following url for getting the current url, 我得到了以下网址来获取当前网址,

<?php if (is_home()) { echo site_url(); } else {  echo the_permalink(); } ?>

The script does not execute and when I click the button, the url does not generate but the php script is displayed in the browser as it is. 脚本没有执行,当我点击按钮时,url不会生成,但php脚本会在浏览器中显示。

Please help out. 请帮忙。 Thanks 谢谢

You would use 你会用的

<?php echo $_SERVER['PHP_SELF']; ?>

This gives you the url of the current page. 这将为您提供当前页面的网址。 For example index.php or something similar 例如index.php或类似的东西

Using Javascript: 使用Javascript:

var currentURL = document.URL;
alert(currentURL);

Using PHP: 使用PHP:

<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>

If you want file name Use PHP Magic Constants such as __FILE__ 如果你想要文件名使用PHP Magic Constants,如__FILE__

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

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