Jump to content

My code wont work:


Recommended Posts

Guest MrLeN

I am trying to get the username of the channel that clicks to my website from their YouTube homepage:

 

if (isset($_SERVER['HTTP_REFERER'])) {
echo '...<br />';
echo ''.$_SERVER['HTTP_REFERER'].'<br />';
echo '...<br />';
$url = $_SERVER['HTTP_REFERER'];
$url_parts = parse_url($url);
$path_parts = explode('/', $url_parts['path']);
foreach ($path_parts as $pp) {
if ($path_parts[0] == 'user') {
  $user = $path_parts[1];
} else {
  $user = $path_parts[0];
}
  $user = $pp;
  break;
}
}
echo 'username: '.$user.'';
exit;

 

..but it doesn't echo a username.

 

I can't work out why.

 

Can someone help me?

Link to comment
https://forums.phpfreaks.com/topic/264499-my-code-wont-work/
Share on other sites

There's a fatal syntax error in this code, perhaps that's causing it.

 

Also, you'll need to debug this on your own.  You can't just write an entire program and hit "go" and then give us "when I hit go nothing goes."  Does the referrer come in properly?  Does the explode produce the data you'd expect?  Are you sure you know what foreach does?  What happens when you remove that (completely unnecessary) break statement?

Link to comment
https://forums.phpfreaks.com/topic/264499-my-code-wont-work/#findComment-1355485
Share on other sites

Guest MrLeN

Well, I don't know the answer to any of those questions. Because someone helped me with the code in MSN messenger, and now he is gone.

 

All I want to do is detect the YouTube username of the YouTube channel -- for if someone clicks from that channel to my website.

 

I don't understand much of the code above.

Link to comment
https://forums.phpfreaks.com/topic/264499-my-code-wont-work/#findComment-1355488
Share on other sites

This is your code

if ($path_parts[0] == 'user') {
  $user = $path_parts[1];
} else {
  $user = $path_parts[0];
}

 

This is $path_parts

...
http://www.youtube.com/user/MyYouTubeUserName?feature=mhee
...
Array ( [0] => [1] => user [2] => MyYouTubeUserName) username: 

 

 

(You also have that section looping in a foreach which is not needed)

Link to comment
https://forums.phpfreaks.com/topic/264499-my-code-wont-work/#findComment-1355510
Share on other sites

Guest MrLeN

This is your code

if ($path_parts[0] == 'user') {
  $user = $path_parts[1];
} else {
  $user = $path_parts[0];
}

 

This is $path_parts

...
http://www.youtube.com/user/MyYouTubeUserName?feature=mhee
...
Array ( [0] => [1] => user [2] => MyYouTubeUserName) username: 

 

 

(You also have that section looping in a foreach which is not needed)

 

I really don't understand.

 

I am trying to work out what you are pointing out, but I have no clue.

Link to comment
https://forums.phpfreaks.com/topic/264499-my-code-wont-work/#findComment-1355511
Share on other sites

Guest MrLeN

I know that the reason this code is in there:

 

if ($path_parts[0] == 'user') {
  $user = $path_parts[1];
} else {
  $user = $path_parts[0];
}

 

..is because sometimes the YouTube URL has /user/ in it, and sometimes it doesn't.

 

That is why there are two different variations.

Link to comment
https://forums.phpfreaks.com/topic/264499-my-code-wont-work/#findComment-1355522
Share on other sites

Guest MrLeN

I give up.

 

I know you're frustrated, but I sat here for a long time trying to figure out why you are saying what you are saying.

 

I don't get it. I'll agree that I look like an idiot, if that makes you feel better.

 

I have no idea what you are saying, or why. I am trying to understand.

Link to comment
https://forums.phpfreaks.com/topic/264499-my-code-wont-work/#findComment-1355548
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.