Getting Referring Url from PHP with javascript?

Discussion in 'JavaScript' started by DemoIXI, Sep 15, 2006.

  1. #1
    Hello,

    I was wonder if it's possible to retrieve from the current url the url in which the guest was referred by.

    At the moment, whenever I try including my PHP file that checks for the referring url, it keeps checking itself instead of checking the page in which I included it on with php. Here's the code that I'm trying to use:

    code.php
    
    <?php echo 'document.write("'.$_SERVER['HTTP_REFERER'].'")'; ?>
    Code (markup):
    referer.html
    
    <script language="javascript" src="code.php"></script>
    Code (markup):
    When you go to referer.html, it's showing "referer.html" instead of showing the referral url to referer.html.

    Thanks for the help in advanced.
     
    DemoIXI, Sep 15, 2006 IP
  2. discoverclips

    discoverclips Peon

    Messages:
    491
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you want to find the referrer page?
    <script>document.write('<img src="hidden.php?ref=' + document.referrer + '" style="display:none;">');
    
    Code (markup):
    hidden.php

    $ref = $_REQUEST['ref'];
    echo $ref;
    PHP:
    is this what you want?
     
    discoverclips, Sep 16, 2006 IP
  3. DemoIXI

    DemoIXI Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, that's what I'm looking for, thanks!
     
    DemoIXI, Sep 16, 2006 IP
  4. DemoIXI

    DemoIXI Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    For some reason, it's still not allowing me to track search engine referrals. All that document.referrer records is up to "&hl=en". For example, if I searched for the word "test" on Google, the script would only pick up "http://www.google.com/search?hl=en". Is there a way to fix this?
     
    DemoIXI, Sep 16, 2006 IP
  5. discoverclips

    discoverclips Peon

    Messages:
    491
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try this, not sure if it will work though:

    <script>document.write('<img src="hidden.php?ref=' + escape(document.referrer) + '" style="display:none;">');
    HTML:
     
    discoverclips, Sep 16, 2006 IP
  6. DemoIXI

    DemoIXI Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah it works now, thanks for the help.
     
    DemoIXI, Sep 16, 2006 IP