SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Counting external link clicks whilst maintaining ease of navigation

    Good evening,

    I'm writing some php code to count the number of clicks that a link receives. Once the extra click has been counted it will redirect the user to the target webpage.

    However - it must also allow the user to click the back button once on her browser in order to return to the first page (as oposed to going back to the redirect page which will then instantly send the user to the website that I'm linking to).

    Sounds tricky to me but indeed.com have managed it. If you click on one of the jobs links on the following webpage you will be taken to a redirect page and then the target webpage. And... if you then use the back button in your browser you will go straight back to the search results, bypassing indeed's redirect page and so keeping navigation easy for the user:

    http://www.indeed.co.uk/jobs?q=engineer&l=

    How do they do that?!

    Thanks everyone,

    Stu

  2. #2
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a file that I wrote called hitbox.php. Her is how it works, if someone clicks on a banner on a site like www.fosterfollynews.com it calls hitbox.php with an affiliate id "aid" set to the id of the affiliate (advertisement) in the database . It then does a query on the database and returns the url field, from the database. If the url field has .php in it, that means it should load a .php ad file on the current server. If the url field has a www in it, then it redirects to the appropriate website address stored in the url field. I wrote this a few years back and it is still running, -- I just did a quick copy and paste. Note the include/startup.php is nothing but a file that connects to the database.

    If you think about it, if it redirects to another site, then the visitor clicks the back button, it does an onload which is set to history.back(-1) --- this is the magic code. Without this, it would just load and forward them back to the external url every time they hit the back button (I hate sites that do that!)


    PHP Code:

    <?php




    // loads affilate links when banner clicked, or does nothing if out of range affiliate id

    include("include/startup.php");

    $afid = (int) $_GET['afid'];  # casting is a good way to prevent sql injections

    if($afid <= 0$afid=0;



    $H "<HTML> 
    <HEAD> 
    [url] 
    <TITLE></TITLE> 
    </HEAD> 
    <BODY [MESSAGE]> 

    </BODY> 
    </HTML> "
    ;

    $sql "select id,url,clicks from banners where id=$afid limit 1";
    $r QueryDatabase($sql,0);

    $found mysql_num_rows($r);


    if(
    $found){
        
        while(
    $a mysql_fetch_array($r)){
       
             
    $url  $a['url'];
             
    $clicks $a['clicks'];
             
    $id $a['id'];
              
    # load an ad page on this server
              # otherwise the url field contains www.somesite.com

              
    if(substr($url,-4) == ".php"$url=  $_SERVER['HTTP_HOST'] . "/home/Ads/story.php?sid=$id&ad=1";

        }
        
       
    # update hit counter
        
    $clicks++;
        
    $sql "update banners set clicks=$clicks where id=$afid";
        
    $r   QueryDatabase($sql,0);
        
        
    $meta "<META HTTP-EQUIV=\"refresh\" content=\"0;URL=http://$url\">";
        
    $H str_replace("[url]",$meta,$H);
        
    $H str_replace("[MESSAGE]","",$H);
        

        
    } else {

      
    $H str_replace("[url]","",$H);
      
    $H str_replace("[MESSAGE]","onload=javascript:history.back(-1);",$H); # note this stubarny

    }

        echo 
    $H;

    ?>
    intragenesis, llc professional web & graphic design

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using a http header redirect will work.
    PHP Code:
    <?php
    header
    ("Location: http://example.com");
    exit();
    ?>
    Back button will not break.

  4. #4
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys,

    That was very kind of you - cranial-bore's works perfectly so I'll stick with it because it's shorter :-)

    Thanks again,

    Stu


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •