2

I am currently working on a fresh link system for my affiliate sites. However, I am still relatively inexperienced in the PHP area, even though I have always managed it. The affiliate links have a different structure and I would like to create a URL by means of PHP, with which these are automatically provided with the structure. I can not describe it very well, but I still try it with an example:

User X enters the URL "example.net/ref.php?url=NORMAL_URL" and depending on the domain in the "NORMAL_URL" a structure is applied. For example, an "? Aff = XYZ" is added to the URL "example.net/ref.php?url=http://example.de/". If the URL "example.net/ref.php?url=http://example.nl/" is directed to "affiliate_network.de/encoded_url=http%3A%2F%2Fexample.nl%2F I hope you have It at least a very small bit understood.Thanks for any help!

For a better understanding:

Tobias
  • 49
  • 1
  • 8
  • 1
    So to help me better understand how this work can you confirm if this example follows what you are attempting. You want `example.net/ref.php?Aff=XYZ&url=httl://example.de/` to then redirect to a URL associated with affiliate XYZ. If XYZ means you want to redirect to `affiliate_network.de` from `example.net` with the `encoded_url` coming from the `url` portion of `example.net`? And for every `?Aff=` combo `affiliate_network.de` would change to the affiliate website. Is that correct? – Mic1780 Jul 20 '17 at 18:21
  • Not quite, I once tried to make a graphic something understandable: http://prntscr.com/fy6yf5 – Tobias Jul 20 '17 at 18:43

1 Answers1

0

Send the url into this function, it uses preg_match with a regex from here to extract the domain part.
$matches will be an array with the domain in element 1 so you can find it with $matches[1]
This function checks if the array is empty (no domain is found) and will return null in that case It then uses a switch to compare the matched domain to a list of domains you can specify, if none of the domains listed match then it will again return null

Within each case I have simply put a return value, but you can be creative and do anything you wish in there to build the URL in the way your application requires.

I'd suggest rather than putting the redirect inside this function, which would be semantically confusing when you come to maintain your application later. You should check the return value of the function, and if it is not null redirect to the return value. If it is null, then you can do something else (display error for example)

function getReturnFromURL($url) {
    $matches = null;
    preg_match("/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/im", $url, $matches);
    if (empty($matches)) return null;
    switch ($matches[1]) {
        case "ebay.de":
            return "ebay-network.de/XXX?encoded_url=the_encoded_?-{$matches[1]}";
            break;
        case "amazon.de":
            return "amazon.de/XXX?encoded_url=the_encoded_?-{$matches[1]}?tag=XYZ";
            break;
        default:
            return null;
    }
}
Darren H
  • 910
  • 8
  • 24
  • Thanks a lot! However, I do not understand what I edit in line two and four. Thanks for any help! – Tobias Jul 22 '17 at 11:05
  • What you need to change will depend on exactly what you want to do. I wrote a fairly generic answer that would serve as a good base for you but I could not tailor it more specifically because I'm not entirely clear what your requirements are – Darren H Jul 22 '17 at 11:24
  • Hello, The code checks whether the variable is exactly "amazon.de" in this example. The variable should only contain "amazon.de". Before and after, the rest of the link is, if this is the case with this code, default is triggered. I'm looking forward to hearing from you! – Tobias Jul 22 '17 at 11:30
  • The regex will take a full url as `$url` and return the domain part. For example if `$url = 'https://stackoverflow.com/abc/def?xyz=123` then `$matches[1]` will contain `'stackoverflow.com'`. If then checks that against each domain that you want to handle and performs a different task depending on which domain it is. If that is not what you wanted then I have misunderstood you. – Darren H Jul 22 '17 at 11:38
  • It might be clearer if you could provide real examples rather than incomplete examples, because your explanation is quite hard to follow – Darren H Jul 22 '17 at 11:40