redirect that also extracts part of referrer and appends that part to url

8 replies
I would like to achieve the following situation:

Let's say I have two links:
  • Case 1: domain.com/webpage-123.php
  • Case 2: domain.com/webpage2-456.php (note the 2 here!)

If a person comes from ip 123.456.789 or 987.654.321 (and more are possible), he should just continue to the webspot/link he or she clicked. However, if the person does not come from said ip address, he or she should be redirected:
  • In case 1: domain.com/custom_string1=123 (where 123 is extracted from the original URL and appended!)
  • In case 2: domain.com/custom_string2=123 (where 456 is extracted from the original URL and appended!)

If this could be done with a php redirect, that would be preferred I guess since I don't want to mess with the htaccess all the time, but if no alternatives are available, of course htaccess code would do fine.

Thanks a lot
#appends #extracts #htaccess #part #php #redirect #referrer #url
  • Profile picture of the author tweakr
    I'm not sure if this would be what you're looking to do or not, I'm a little unclear about your directions but I believe this will do the trick for you.

    P.S. I've been trying to post here for the past 15-20 minutes but it kept saying I couldn't and that I had to wait X amount of time, so I pasted the code at pastebin in order to make this post.
    {{ DiscussionBoard.errors[5429398].message }}
    • Profile picture of the author Kelebek
      Hi tweakr,

      Thanks a lot for your reply. I think it's close, but I understand your confusion. Let me try to explain a bit better.

      It's actually a cloaking script I need for creatives I plan to run at different places. These creatives are just images. For this example, let's say we have 4 images/creas. Their target urls are:

      crea 1: domain.com/lander1-img1.php
      crea 2: domain.com/lander1-img2.php
      crea 3: domain.com/lander2-img1.php
      crea 4: domain.com/lander2-img2.php

      If the user comes from an array of ips (e.g. 123.456.789 or 789.654.321) then the user should just be taken to the original url appointed to the image. So, if a user from any of those ips clicks on crea 1, he'll be taken to domain.com/lander1-img1.php, if he clicks on crea 3, he'll go to domain.com/lander2-img1.php.

      However, all other users should be redirected. In case of:

      crea 1, he should be taken to domain.com/base.php?c=112&key=123456789&keyword=img1 (where the img1 part is extracted from the original url, the part following domain.com up until keyword= should be entered by me.)

      So, for crea 2, the user should be taken to domain.com/base.php?c=112&key=123456789&keyword=img2

      However, for creas 3 and 4 (the ones with lander2) they should respectively go to

      domain.com/base.php?c=112&key=abcdefghi&keyword=img1
      and
      domain.com/base.php?c=112&key=abcdefghi&keyword=img2

      Here again, the img1 and img2 parts are extracted from the original url, the part following domain.com up until keyword= should be entered by me.

      I hope I could make myself a bit more clear now?

      Also, where do I put such a php script, and how can I make sure it's activated immediately?

      Kind regards, and thanks a lot for your effort and time!

      P.S. wanted to thank you, but somehow don't have a thank button underneath your reply?
      {{ DiscussionBoard.errors[5429603].message }}
  • Profile picture of the author frenchsquared
    Give me a few minutes. I will write it for you.
    {{ DiscussionBoard.errors[5431160].message }}
  • Profile picture of the author frenchsquared
    does it have to be:
    domain.com/lander1-img1.php
    this is a problem

    or can it be:
    domain.com/?lander=1&img=1
    I can do this in a few minutes
    {{ DiscussionBoard.errors[5431186].message }}
  • Profile picture of the author frenchsquared
    if you will use: domain.com/?lander=1&img=1

    Example here: metamorphosistheme.com/redirect/?lander=3&img=1

    then this does what you need.

    <?php
    $ipArray = array('173.8.241.122', '173.8.241.123');

    $ip = $_SERVER['REMOTE_ADDR'];
    if(in_array($ip, $ipArray)){
    echo 'Your ip is a match<br/>';
    echo 'redirct to: domain.com/lander'.$_GET['lander'].'- img'.$_GET['img'].'.php';
    } else {
    echo 'your ip does not match<br/>';
    echo 'redirct to: domain.com/base.php?c=112&key=abcdefghi&keyword='.$_GET['img'];
    }

    function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
    }
    ?>

    you can replace the echo with a header("Location") and it will redirect.
    If needed I can show you how. Just need to know if you can change your url.
    {{ DiscussionBoard.errors[5431244].message }}
  • Profile picture of the author tweakr
    Alright, finally got back. Thanks for explaining that better. Gordon is right if you could change the URL as he suggested it would be much better and much more stable but if you can't I have whipped up a little code that should help you out.

    I put it over on pastebin again (the code always seems to get screwed up or it doesn't let me post here).

    To use it just save it as whatever you like, in my example I will call the file "redirect.php" and it will be in the same directory as the "landerX-imgX.php". Now that it's saved in the same directory just open the "landerX-imgX.php" files and add "include('redirect.php');" at the very top (it's important that it's included before anything else or it won't work). Once that's completed you should be all set, and everything should work as you like.

    P.S. Thanks for your appreciation, not sure about the thanks button maybe you need a certain number of posts first?
    {{ DiscussionBoard.errors[5432002].message }}
    • Profile picture of the author Kelebek
      Hi frenchsquared and tweaker, thanks a lot for your help and input on this one.

      I'm afraid that it has to be domain.com/lander1-img1.php:
      • the url in the crea and the final destination URL must be exactly the same the same
      • the web page is a single php file

      I will have a look at your codes, try them out and get back to you with the results.

      Cheers.
      {{ DiscussionBoard.errors[5433402].message }}
      • Profile picture of the author Kelebek
        Hi tweaker,

        I implemented it and your code works just fine. Thanks a lot for the effort.

        Unfortunately, I still can't use it (my ads keep on being rejected) since the approver nitwits still see some other urls popping up between the time they click on the crea to the time they're on the actual lander.

        Albeit both URLs are now the same if they come from the given ip, the whole redirect (or something else?) still creates some temp urls I guess, which makes them disapprove :confused:

        I don't understand how, since there is only a geo script and the include running in that php page.

        Before I take out the geo script, could the redirect create some temp urls that may briefly pop up at their end?

        Cheers,
        Kelebek
        {{ DiscussionBoard.errors[5442411].message }}

Trending Topics