SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: $http_referer

  1. #1
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $http_referer

    Hello. I read somewhere of a method to use $HTTP_REFERER without the FQDN of the URL, that is with a reference to the local file by somehow parsing it through a $PATH call.

    In other words, what I want to do is reference a page like this:
    PHP Code:
    $HTTP_REFERER("localpage.php"); 
    instead of:
    PHP Code:
    $HTTP_REFERER("http://mydomain.com/folder/localpage.php"); 
    so that when I move these pages to where they're actually going I don't have to worry about the FQDN.

    Anyone?
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  2. #2
    SitePoint Evangelist CyberFuture's Avatar
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not really clear on what your trying to do, I suggest creating a page like so:
    PHP Code:
    <?php
    phpinfo
    ();
    ?>
    This will give you a whole bunch of global varibles for you to play around with.

  3. #3
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $HTTP_REFERER is a variable...not a function...

    you could munge the referer by exploding it or use an regular expression to grab only the filename i guess...
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  4. #4
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use the basename() function to return a filename from a full path. I think it would work on a URL too.

  5. #5
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this would work
    PHP Code:
    $ref $_SERVER['HTTP_REFERER'];
    $url parse_url($ref);
    echo 
    $url['path']; 

  6. #6
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One little note about parse_url(), according to the comments on its manual page, it will return the entire path after the domain name.

    So, if your HTTP_REFERER is "http://mydomain.com/folder/localpage.php", using the 'path' from parse_url() will return "/folder/localpage.php".

    If you want to just get "localpage.php", I would do:

    PHP Code:
    $ref $_SERVER['HTTP_REFERER'];
    $url parse_url($ref);
    $filename basename($url['path']);
    echo 
    $filename

  7. #7
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then I would do this. I've never used basename() so I do it a different way.
    PHP Code:
    <?php
    $ref 
    $_SERVER['HTTP_REFERER'];
    $url parse_url($ref);
    $fn explode("/"$url['path']);
    echo 
    $fn[sizeof($fn)-1];
    ?>
    Last edited by notepad_coder; Jun 20, 2002 at 19:24.

  8. #8
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had to do something similar in ASP recently... uses the same variables sort of, but I had the problem of it not removing anchornames (you know "http://..../pagename.asp#anchor"), so I had to write a function to get rid of those before it redirected the user...

  9. #9
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All you have to do for that with PHP is
    PHP Code:
    <?php
    $url 
    "http://www.site.com/foo/bar.php#bookmark";
    $p parse_url($url);
    echo 
    "Before: " .$url"<br>";
    echo 
    "After: " .str_replace("#" .$p['fragment'], ""$url);
    // There is probably an easier way then this
    ?>
    Isn't PHP much much better then ASP
    - the lid is off the maple syrup again!

  10. #10
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm undecided actually I find the syntax of ASP tons easier to understand

    [VBS]refererValue = (Request.ServerVariables("HTTP_REFERER"))
    findHash = instr(1, refererValue, "#")

    if findHash > 0 then
    cleanHashes = left (refererValue, findhash - 1)
    response.redirect cleanHashes & "#" & ItemID

    else

    if findHash = 0 then
    response.redirect refererValue & "#" & ItemID

    else
    response.redirect refererValue

    end if
    end if[/VBS]

    Probably easier ways to do it though in ASP

  11. #11
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hold the phone!

    Cyngon and notepad-coder,

    I started to say that:
    both of your code produce the folder in the path. Even using basename() and explode().

    But...

    Ok, I take it all back. Basename() worked. The odd thing was that it didn't work the first 5 times I tried it!!! Then all of a sudden it starts working. Very freakish. I'm also getting very strange errors if I copy and paste code (has that happened to anyone here?). For example, I just got a "fata error, undefined function basename() and parse_url(). I had to retype the lines myself for it to work. The strangest thing of all is that my editor (Dreamweaver MX) poperly recognized the functions by color-coding them appropriately.

    Oh well, another mystery. I was just curious if anyone had run up against that.

    Thanks for your suggestions.
    Last edited by pata; Jun 21, 2002 at 08:59.
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  12. #12
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by platinum
    I find the syntax of ASP tons easier to understand
    You'll learn one day

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  13. #13
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heh -- i'm trying... beleive me!


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
  •