Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 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? Quote Link to comment Share on other sites More sharing options...
trq Posted June 20, 2012 Share Posted June 20, 2012 In English please? Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 I am trying to get the username of the channel that clicks to my website [ie: user ID out of the http_referrer] from their YouTube homepage: Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted June 20, 2012 Share Posted June 20, 2012 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? Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 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. Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 The code doesn't display any errors. It simply displays: ... http://www.youtube.com/user/USERNAME?feature=mhee ... username: Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 ie: the part after username: doesn't display a username. If I can just get the username to display, I know how to fwrite it to a text file (which is the text file you helped me with before thorpe). Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 Change this $path_parts = explode('/', $url_parts['path']); foreach ($path_parts as $pp) { To $path_parts = explode('/', $url_parts['path']); print_r($path_parts); foreach ($path_parts as $pp) { And tell us what you see. Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 It returns: ... http://www.youtube.com/user/MyYouTubeUserName?feature=mhee ... Array ( [0] => [1] => user [2] => MyYouTubeUserName) username: Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 So do you see now why your code doesn't work? Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 No, I am sorry, I don't Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 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) Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 You are looking is $path_parts[0] == 'user'. It isn't. So your code says $path_parts[0] must be the username. Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 So I have to echo $path_parts[0] ? Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 Oh, the numbers are wrong? [1] and [0] should be [1] and [2] ? Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 But path_parts[0] is NEVER going to be 'user'. Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 But path_parts[0] is NEVER going to be 'user'. Why not? Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 I give up. Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 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. Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted June 20, 2012 Share Posted June 20, 2012 Ok, I give upo too. It's 1:37am. I need to go to sleep. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 Oh, the numbers are wrong? [1] and [0] should be [1] and [2] That was correct, I don't know what you are still confused about. Did you change it? If so post the updated code. Remove that section from the foreach. Quote Link to comment Share on other sites More sharing options...
xyph Posted June 20, 2012 Share Posted June 20, 2012 If you want it coded for you, I suggest the Freelancer's forum. If you want to learn PHP, I think you need to take a few steps back, because what you're trying to do seems way beyond your current abilities or knowledge of the language. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.