开发者

Current URL with PHP

开发者 https://www.devze.com 2023-02-01 12:53 出处:网络
I have a link shortener system (Yourls) set up so you can go to the short url with ?url=[xyz] (where [xyz] is the url you wan to shorten) added to the end will shorten the URL.I want to add a link to

I have a link shortener system (Yourls) set up so you can go to the short url with ?url=[xyz] (where [xyz] is the url you wan to shorten) added to the end will shorten the URL. I want to add a link to a separate page (on my MediaWiki wiki) that shortens the permalink of the page that it is on. I need to add the button to my template in a way that will add the URL of the current page to the link. MediaWiki is a PHP platform, so that is preferred (but JavaScript is fine too). How can I do this?

(I apologize if this is confusing)

UPDATE: I am terrible with PHP, so sorry. I just put

<?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>Suppor开发者_高级运维t &rarr;</a>'; ?>

and that just went to http://sumov.co.cc/?url= (sumov.co.cc is my short link).


To get the url:

$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

And I recommend to use urlencode( $url ) and urldecode( $url ) to wrap and unwrap it for transfering safely.


"http://www.yoursitesdomainname.com".$_SERVER['REQUEST_URI'];

The previouse will be the current page's URL, Do as you may with it.


As Babiker said that will return the URI. I would suggest filtering or exscaping the url. WordPress has a function called esc_url.

From WordPress core wp-includes/formatting.php lines 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};)/', '&#038;$1', $url);
          $url = str_replace( "'", '&#039;', $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);
 }

Then you would run <?php esc_url(http://www.example.com".$_SERVER['REQUEST_URI'];) ?>


if I were you I would make your own URL shortened like I did (http://tecn.me) I learned a lot and the possibilities are endless

0

精彩评论

暂无评论...
验证码 换一张
取 消