0

I have a redirection script and I have a quick question.

<?php

$user = ;

header ("location: http://socialmedia.com/follow_user=$user");
?> 

Note that socialmedia.com ISNT on the server. I want to grab the end of the url so if I have an index.php file I can have example.com/myusername redirect to socialmedia.com/follow_user=myusername.

1 Answers1

1

Use parse_url.

For example:

$url = 'http://example.com/myusername';
print_r(parse_url($url));

Will give you:

Array
(
    [scheme] => http
    [host] => example.com
    [path] => /myusername
)

So you can take that path portion of the array and use it however you wish.


<?php
$url = 'http://example.com/myusername'; // the url you start with
$partYouNeed = parse_url($url, PHP_URL_PATH); // get the portion you need
$partYouNeed = ltrim($partYouNeed, '/'); // remove the slash from it

header('location: http://socialmedia.com/follow_user=' . $partYouNeed);
rg88
  • 20,742
  • 18
  • 76
  • 110
  • Thanks Stuart. I'm a PHP n00b with a lot of things. Can you please help me implement it into this script? – TechTubeCentral Feb 16 '13 at 04:01
  • Sorry for being so annoying. Is there a PM feature on this thing? It's not working but I don't want to post the source code and have someone steal the idea – TechTubeCentral Feb 16 '13 at 04:27
  • No, there is no PM. FWIW, I doubt anyone would find anything worth stealing in a few lines of url parsing code. – rg88 Feb 16 '13 at 04:28
  • That pastebin example is not working for you because you are trying to access a portion of the url that does not exist in your example. Your example url does not have a url path in it so PHP_URL_PATH does not work. You would need something like `$url = 'http://subto.me/getsgrabbed';` – rg88 Feb 16 '13 at 04:39
  • Multiple basic errors in what you just added. There is a double semicolon after you define the url plus you are not handling the inclusion of the variable in the header correctly. You are printing out the string $partYouNeed rather than parsing the variable. – rg88 Feb 16 '13 at 04:48
  • Change the header function call to: header('location: youtube.com/subscription_center?add_user=' . $partYouNeed); – rg88 Feb 16 '13 at 04:50
  • Now it's because you have a malformed url in your header. You need: `header('location: http://youtube.com/subscription_center?add_user=' . $partYouNeed);` but you have: `header('location: youtube.com/subscription_center?add_user=' . $partYouNeed);` – rg88 Feb 16 '13 at 04:57
  • @TechTubeCentral - Don't forget. If you found this helpful you can mark my answer as "accepted" by clicking the green arrow underneath the up/down arrows up under the score at the top of this answer. – rg88 Feb 16 '13 at 05:04
  • Uh Stuart I just tested it and it redirects to youtube.com/subscription_center?add_user=getgrabbed http://pastebin.com/fCSRHxVp – TechTubeCentral Feb 16 '13 at 08:19
  • Of course it does... you are providing it with "getsgrabbed". If you change the url to http://subto.me/whateveryouwant it will go to http://youtube.com/subscription_center?add_user=whateveryouwant. Isn't that what you asked for? A way to just use whatever you want in the url and have that added to the redirect? – rg88 Feb 16 '13 at 09:06
  • Stuart I want it to be like a variable so you don't have to change it in a file if that makes sense so it grabs whatever the user types in the address bar. So if the user types in subto.me/channelnamehere it goes to their specific channel. It basically needs to grab whatever is on the end of subto.me/ and redirect to youtube.com/subscription_center?add_user=whateverisaftersubto.me/ – TechTubeCentral Feb 16 '13 at 17:25
  • To get the url and store it in a variable see this: http://stackoverflow.com/questions/6768793/php-get-the-full-url Once you have it in a variable you can use it as above. – rg88 Feb 16 '13 at 21:18