Jump to content

My code wont work:


Guest MrLeN

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
Share on other sites

Guest MrLeN

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:

Link to comment
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
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
Share on other sites

Guest MrLeN

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Guest MrLeN

It returns:

 

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

Link to comment
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
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
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
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
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
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.