使用PHP的当前URL
我已经设置了一个链接缩短器系统(Yourls),这样您就可以使用使用PHP的当前URL,php,Php,我已经设置了一个链接缩短器系统(Yourls),这样您就可以使用?url=[xyz](其中[xyz]是您希望缩短的url)转到短url,添加到末尾将缩短url。我想添加一个链接到一个单独的页面(在我的MediaWiki wiki上),以缩短页面的永久链接。我需要将按钮添加到我的模板中,以便将当前页面的URL添加到链接中。MediaWiki是一个PHP平台,所以这是首选(但JavaScript也不错)。我该怎么做 (如果这让人困惑,我很抱歉) 更新:我对PHP很糟糕,很抱歉。 我刚把 <?p
?url=[xyz]
(其中[xyz]
是您希望缩短的url)转到短url,添加到末尾将缩短url。我想添加一个链接到一个单独的页面(在我的MediaWiki wiki上),以缩短页面的永久链接。我需要将按钮添加到我的模板中,以便将当前页面的URL添加到链接中。MediaWiki是一个PHP平台,所以这是首选(但JavaScript也不错)。我该怎么做
(如果这让人困惑,我很抱歉)
更新:我对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,您可以使用它。正如Babiker所说,它将返回URI。我建议过滤或删除url。WordPress有一个名为esc_url的函数 来自WordPress core 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);
}
然后你会运行
,如果我是你,我会像我一样缩短你自己的URL(http://tecn.me)我学到了很多,获取url的可能性是无穷的:
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
我建议使用urlencode($url)
和urldecode($url)
将其包装和展开,以便安全传输。是否要通过php在地址栏中获取url?是的。这正是我想要的。
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];