我设置了链接缩短程序系统(Yourls),因此您可以在末尾添加?url=[xyz]
(其中[xyz]
是您要缩短的网址),以转到简短网址,以缩短该网址。我想将链接添加到单独的页面(在我的MediaWiki Wiki上),以缩短其所在页面的永久链接。我需要以将当前页面的URL添加到链接中的方式将按钮添加到模板中。 MediaWiki是一个PHP平台,因此是首选(但JavaScript也可以)。我该怎么办?
(如果造成混淆,我深表歉意)
UPDATE:我对PHP感到很抱歉,对不起。我只是把
<?php echo'<a href="http://sumov.co.cc/?url=".$_SERVER["REQUEST_URI"]; class="buttonlink ui-state-default ui-corner-all"><span class="ui-icon ui-icon-newwin"></span>Support →</a>'; ?>
并且刚刚转到http://sumov.co.cc/?url=
(sumov.co.cc是我的短链接)。
获取URL:
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
并且我建议使用urlencode( $url )
和urldecode( $url )
对其进行包装和展开以安全地进行传输。
"http://www.yoursitesdomainname.com".$_SERVER['REQUEST_URI'];
上一个将是当前页面的URL,请按照您的意愿进行操作。
正如Babiker所说,这将返回URI。我建议过滤或转义网址。 WordPress具有名为esc_url的功能。
从WordPress核心wp-includes / formatting.php第2235-2281行
/**
* Checks and cleans a URL.
*
* A number of characters are removed from the URL. If the URL is for displaying
* (the default behaviour) amperstands are also replaced. The 'clean_url' filter
* is applied to the returned cleaned URL.
*
* @since 2.8.0
* @uses wp_kses_bad_protocol() To only permit protocols in the URL set
* via $protocols or the common ones set in the function.
*
* @param string $url The URL to be cleaned.
* @param array $protocols Optional. An array of acceptable protocols.
* Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set.
* @param string $_context Private. Use esc_url_raw() for database usage.
* @return string The cleaned $url after the 'clean_url' filter is applied.
*/
function esc_url( $url, $protocols = null, $_context = 'display' ) {
$original_url = $url;
if ( '' == $url )
return $url;
$url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
$strip = array('%0d', '%0a', '%0D', '%0A');
$url = _deep_replace($strip, $url);
$url = str_replace(';//', '://', $url);
/* If the URL doesn't appear to contain a scheme, we
* presume it needs http:// appended (unless a relative
* link starting with / or a php file).
*/
if ( strpos($url, ':') === false &&
substr( $url, 0, 1 ) != '/' && substr( $url, 0, 1 ) != '#' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) )
$url = 'http://' . $url;
// Replace ampersands and single quotes only when displaying.
if ( 'display' == $_context ) {
$url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&$1', $url);
$url = str_replace( "'", ''', $url );
}
if ( !is_array($protocols) )
$protocols = array ('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn');
if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
return '';
return apply_filters('clean_url', $url, $original_url, $_context);
}
然后您将运行<?php esc_url(http://www.example.com".$_SERVER['REQUEST_URI'];) ?>
[如果我是你,我会像我一样(http://tecn.me)缩短自己的URL,我学到了很多,而且可能性是无穷的