• 0

[php] Remove slash from beginning of REQUEST_URI generated link


Question

This is ruining my day and I'd appreciate some help.

 

I'm working on a simple portfolio website. The only php I use is to get the current URL in order to avoid using the regular social share buttons.

 

In case you don't know what I'm talking about, instead of adding the javascript buttons on the page, you can use simple links to share things on Facebook, Twitter etc. I'll use Google+ as an example, just for lolz.

 

To share a page on it, this is what you need:

<a href="https://plus.google.com/share?url=INSERT_URL">+1</a>

I used the REQUEST_URI function to get the current page url, in order to save time and not repeat the process manually for each individual page. The problem is I use datacontent-url to display the pages on top of index.html, and REQUEST_URI has no way of knowing that, so I had to tweak it a bit.

 

This would be the default code:

<a href="https://plus.google.com/share?url=<?php echo(('http://').$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); ?>">+1</a>

This would output http://example.com/examplepage.html as the url for sharing, which I don't want. What I need is http://example.com/index.html#examplepage.html

 

So i tried this:

<a href="https://plus.google.com/share?url=<?php echo(('http://').$_SERVER["SERVER_NAME"].('/index.html#').$_SERVER["REQUEST_URI"]); ?>">+1</a>

The problem is that REQUEST_URI adds a slash at the beginning of its part, so I end up with a broken link:

http://example.com/index.html#/examplepage.html

Is there  way to remove the slash from the beginning of the url which is the result of the REQUEST_URI function?

 

Sorry if it's a dumb question, and I guess I can always update each individual portfolio page with custom, proper links, but I just figured this would save a lot of time. I tried examples from this page, but it doesn't work.

 

Thanks in advance.

6 answers to this question

Recommended Posts

  • 0
  On 30/03/2014 at 13:28, Seahorsepip said:

You're trying to get url hash through php? If I'm correct that's impossible, instead just send the url on submit or whatever with js as a post variable.

The hash is included if you're loading the page, but not if you click a link on that page, it's just an indication of an anchor.

  • 0
  On 30/03/2014 at 12:38, virtorio said:
substr($_SERVER["REQUEST_URI"], 1);

 

 

  On 30/03/2014 at 12:38, James123 said:

Just use ltrim

ltrim($_SERVER['REQUEST_URI'], '/')

edit: too slow :p

 

Both of these do what I need. I have another question though REQUST_URI adds extra characters to my URL and I was wondering if you guys could figure out why it's happening.

 

This is the url I get:

http://example.com/index.html#examplepage.html?_=1396199176412

Why is it adding "?_=1396199176412" after .html because it breaks the url :/

  • 0

Figured it out, ended up using PHP_SELF and it loses the extra parameters, giving me exactly what I need. In case anyone needs something similar:

<a href="https://plus.google.com/share?url=<?php echo(('http://').$_SERVER["SERVER_NAME"].('/index.html#').substr($_SERVER['PHP_SELF'], 1)); ?>">+1</a>

Again, huge thanks to both virtorio and James123. I'll mark it solved.

This topic is now closed to further replies.
  • Posts

    • I need someone to explain to me what "passkey" is and what happens if my device crashes and is not recoverable.
    • Editor: "YouTube is bring" should be "YouTube is bringing" in your main article title.
    • Opera files complaint against Microsoft for manipulating customers into using Edge by Usama Jawad Most of us here at Neowin are well aware of Microsoft's tactics to get its customers to use its Edge browser. It only recently stopped injecting massive banner ads for its own browser in Chrome's download portal. However, tired of Redmond's continued efforts to manipulate customers into using Edge rather than other alternatives, Opera has filed a complaint against Microsoft in Brazil. As reported by The Verge, the reason for filing a complaint against Brazil rather than any other country is that it is one of Opera's main markets, where the company enjoys a respectable third place. Opera hopes that Brazil will set a global precedent for other countries if it rules in its favor. Regarding the actual nature of the complaint, Opera alleges that Microsoft leverages "manipulative design tactics" and anti-competitive methods to force people into using Edge. Examples of this include third-party browsers not having the option of being bundled in a Windows installation, and the fact that Microsoft's own tools like Widgets, Teams, Search, and Outlook ignore a user's browser preferences and prioritize Edge when opening file like PDFs. If Brazil's regulator does rule in favor of Opera, the company will be seeking remedies like the ability to pre-install other browsers in Windows by default and restricting "dark patterns" that force customers to use Edge. This isn't Opera's first legal battle with Microsoft in recent memory. A little over a year ago, it pressured the European Union (EU) to label the Redmond tech firm's browser as a gatekeeper under the Digital Markets Act (DMA) due to the dominance of Windows in the desktop space and the inability for users to select a browser through a choice screen. However, Opera was unsuccessful in its efforts. As such, it will be interesting to see if it can score a win against Microsoft with its complaint this time.
  • Recent Achievements

    • Apprentice
      MikeK13 went up a rank
      Apprentice
    • Week One Done
      andeyhawk65 earned a badge
      Week One Done
    • First Post
      Jake2530 earned a badge
      First Post
    • Explorer
      Deranox went up a rank
      Explorer
    • Week One Done
      John Volks earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      686
    2. 2
      ATLien_0
      263
    3. 3
      Xenon
      179
    4. 4
      neufuse
      135
    5. 5
      +FloatingFatMan
      100
  • Tell a friend

    Love Neowin? Tell a friend!