0

I am working on PHP code for automatically redirecting a URL to an affiliate URL based on words in the URL.

This is the code I am using:

if (stripos($mylink, "amazon.in") > 0) {
    if (strpos($mylink, "?") > 0) {
        $tmp = $mylink."&affiliate_id";
    } else {
        $tmp = $mylink."?affiliate_id";
    }

    $loc = "Location:".$tmp;
    header($loc);
    exit();
}

But the problem is that when the Amazon URL is like: http://www.amazon.in/English-Movies-TV-Shows/b/ref=sa_menu_movies_all_hindi?ie=UTF8&node=4068584031 it adds the affiliate id like this: ie=UTF8&affiliate_id (it automatically removes the node and its value) but I want it to be like: ie=UTF8&node=4068584031&affiliate_id.

This means that I need to add &affiliate_id at the end of the URL every time if it is an Amazon link.

Actually I just want to achieve the following simple thing: if the URL doesn't have any ?s, I need to place ?affiliateid and if the URL has a ?, I just need to append &affiliateid at the end.

Can you suggest the very simple method for implementing what I require?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • I attempted to reproduce the issue you're having [here](http://ideone.com/vlwtE7), but the output seems to meet your criteria. How was this not working for you? – Spooky Jun 16 '14 at 23:28
  • Hi Spooky,I am also wondering the same thing it simply removes the node thing and add affiliate id at that place please suggest some other method where i can directly add & affiliate id at the end where the url contains any special characters – user3746628 Jun 17 '14 at 19:14
  • You'd probably benefit from `print`ing/`var_dump`ing some variables in your code, so as to assist with debugging why your current code is not working (because it appears that it should be). You could start off by dumping the value of the resulting URL rather than redirecting to it. If the URL is indeed incorrect, dump variables around the snippet of code you provided. Perhaps you could dump the result of `stripos` and `strpos` to figure out which route is being taken through the conditional statements. – Spooky Jun 17 '14 at 22:08
  • The answer on this question generated a lot of discussion, including attempts to take the discussion onto email and third-party chat systems. I thus wonder if this is too broad in practice, and have voted accordingly. – halfer Dec 25 '15 at 17:25

1 Answers1

1

Let PHP do the string parsing for you. Let's say your affiliate id is XYWZ.

if (strpos($mylink, "amazon.in") !== 0) {
    $urlarray=parse_url($mylink);
    $urlarray['query']='affiliate_id=XYWZ&'.$urlarray['query'];
    $newurl= $urlarray['scheme'].'://'.$urlarray['host'].$urlarray['path'].'?'.$urlarray['query'];

    echo $newurl;
}

will output

http://www.amazon.in/English-Movies-TV-Shows/b/ref=sa_menu_movies_all_hindi?affiliate_id=XYWZ&ie=UTF8&node=4068584031

see? I didn't need to worry if there's a question mark. It will be stripped in the output of parse_url;

Edit: using $mylink instead of $theurl

ffflabs
  • 17,166
  • 5
  • 51
  • 77
  • Hi actually the url is variable it can be anything amazon.in or myntra.com or flipkart.com or anythng so i am using stripos to check first wat the url is and then i need to put my affiliate id at the ned which is not working as i have shown above .The method you suggested have a fixed url which is not my case.please help me – user3746628 Jun 17 '14 at 19:10
  • I only used that fixed url as an example. Just use $mylink, whatever it may be, instead. – ffflabs Jun 17 '14 at 19:31
  • i changed the code to if( stripos($mylink,"amazon.in")>0) { $theurl='$mylink'; $urlarray=parse_url($theurl); $urlarray['query']='affiliateid&'.$urlarray['query']; $newurl= $urlarray['scheme'].'://'.$urlarray['host'].$urlarray['path'].'?'.$urlarray['query']; $loc = "Location:".$newurl; header($loc); exit(); } but the ouput i am getting is ://$mylink?affiliateid& – user3746628 Jun 17 '14 at 19:53
  • see my edit. Try outputting the new url value before doing actual redirection. Just to be sure the link edition was right. That way you will know if the node is stripped elsewhere – ffflabs Jun 17 '14 at 19:58
  • i just need to add $affiliate id at the end whenever it contains any special character like ?" – user3746628 Jun 17 '14 at 19:59
  • output is :-amazon.in/Mens-Clothing/b/ref=sv_Apparel_1?affiliate_id=XYWZ&ie=UTF8 – user3746628 Jun 17 '14 at 20:05
  • again it removed the node thing automatically – user3746628 Jun 17 '14 at 20:13
  • can you echo $mylink and $newurl plis? Just to discard that node is present at the beggining. Also, I'm not able for gtalk. Unless you're a gorgeous college girl living nearby, and I bet you aren't. – ffflabs Jun 17 '14 at 20:17
  • ya echo $mylink itself doesnt contain any nodes – user3746628 Jun 17 '14 at 20:20
  • so it not that code snippet stripping the node as you thought. – ffflabs Jun 17 '14 at 20:25