IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

Get Filename Of Referring Url

, php code to get filename of referring URL


snlildude87
no avatar
Moderator
***************
Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



Post #1 post Apr 8 2005, 03:29 AM
Hey smile.gif

I want to know how to get the filename of the referring URL. Look at the following example:

Page A which has a URL of http://blah.trap17.com/blah/blah1.php redirects the user to Page B which has a URL of http://blah.trap17.com/blah/blah2.php.

Is there a PHP code that I can put on blah2.php that will output blah1.php? I tried _SERVER["HTTP_REFERRER"]; (please note the code may not exactly be correct as I do not remember the code laugh.gif) but that did not work so well.

Thanks in advance!
Go to the top of the page
+Quote Post
round
no avatar
Super Member
*********
Group: Members
Posts: 463
Joined: 8-November 04
Member No.: 2,186



Post #2 post Apr 8 2005, 04:00 AM
not sure but why not just have it redirect the same way as the first page. I may not be getting what your problem exactly.
round
Go to the top of the page
+Quote Post
mobious
no avatar
Advanced Member
*******
Group: Members
Posts: 113
Joined: 14-January 05
From: Philippines
Member No.: 3,271



Post #3 post Apr 8 2005, 04:08 AM
just manipuate the url.

[CODE]$filename strrpos($_SERVER['HTTP_REFERER'], strrpos($_SERVER['HTTP_REFERER'], '/') + 1);
Go to the top of the page
+Quote Post
Spectre
no avatar
Privileged Member
*********
Group: Members
Posts: 873
Joined: 30-July 04
Member No.: 246



Post #4 post Apr 8 2005, 08:21 AM
mobious, try closing the CODE tag, buddy.

I would use something along the lines of:
CODE
$url = $_SERVER['HTTP_REFERER'];
if( $url ) {
 $url = strrev($url);
 $last_slash = strlen($url) - strpos($url,'/') - 1;
 $url = strrev($url);
 if( $last_slash ) {
   $file_name = substr($url,$last_slash);
 }
}
Go to the top of the page
+Quote Post
mobious
no avatar
Advanced Member
*******
Group: Members
Posts: 113
Joined: 14-January 05
From: Philippines
Member No.: 3,271



Post #5 post Apr 8 2005, 09:46 AM
@spectre: ok. i'll close my tags always.
Go to the top of the page
+Quote Post
no9t9
no avatar
Privileged Member
*********
Group: Members
Posts: 773
Joined: 4-November 04
Member No.: 2,118



Post #6 post Apr 8 2005, 12:55 PM
$path = explode('/', $_SERVER['HTTP_REFERER']);

all you need is the above code. It is pretty simple.
It will break down your URL into pieces using the "/" (Slash) as the divider.

using your url as an example http://blah.trap17.com/blah/blah1.php

You will get an array with elements
[0] => http:
[1] => blah.trap17.com
[2] => blah
[3] => blah1.php

Using this method, you will realize that your filename will be the last element in the array no matter what your URL looks like. Note: unless you are using query strings. since the filename will always reside in the last element in the array, the filename is always under: $path[count($path)-1]

using count($path)-1 ensures that even if your referring URL is under any number of directories, you will be able to identify the filename.

I am not sure if you are familiar with arrays. If not, then just ignore the explanation and put the following code in your script
CODE
$path = explode('/', $_SERVER['HTTP_REFERER']);
$filename=$path[count($path)-1];


$filename will hold the filename you are looking for.
Go to the top of the page
+Quote Post
Spectre
no avatar
Privileged Member
*********
Group: Members
Posts: 873
Joined: 30-July 04
Member No.: 246



Post #7 post Apr 8 2005, 01:01 PM
You could use the parse_url() function first, which will return just the path as part of the array (index 'path').

Alternatively, you could try checking the string for '?' (question mark), and trimming it from there.
Go to the top of the page
+Quote Post
snlildude87
no avatar
Moderator
***************
Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



Post #8 post Apr 8 2005, 03:10 PM
I tried no9t9's code because his post flowed so well, and it worked! And I also love arrays smile.gif

Spectre, the question mark suggestion would be great except my referring URL does not have a question mark.

That's it, question answered. Thanks again guys!
Go to the top of the page
+Quote Post
iGuest
no avatar
Hail Caesar!
*********************
Group: Members
Posts: 5,876
Joined: 21-September 07
Member No.: 50,369



Post #9 post Nov 11 2008, 05:31 AM
$_SERVER[\\
Get Filename Of Referring Url

I have a link which is on a remote place.
When the link is clicked it will call my script at my server

As my server is in https, the link contains it. Now I don't know where the link is placed and I want to know the referer.

I have used $_SERVER['REMOTE_ADDR'] to get the referer IP and it works. But in the case of referer page $_SERVER['HTTP_REFERER'] does not work.

What will I do now??? I mean how can I make it working?

-question by Himel
Go to the top of the page
+Quote Post
iGuest
no avatar
Hail Caesar!
*********************
Group: Members
Posts: 5,876
Joined: 21-September 07
Member No.: 50,369



Post #10 post Apr 4 2009, 03:39 PM

this code will not work in the example below

If the url to be parsed is, for eaxample: http://www.Foo.Com/bar/

The code:

$path = explode('/', $_SERVER['HTTP_REFERER']);
$filename=$path[count($path)-1];

will return bar/ as the filename, when in fact its a folder.

Just thought add meantion it

 



 

-reply by john
Keywords: php get requested file name from url
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   1 guangdian 2,478 7th February 2005 - 02:29 PM
Last post by: OpaQue
No New Posts   9 ARNEL 2,743 21st May 2005 - 11:04 PM
Last post by: badinfluence
No new   14 irmlm 1,290 10th October 2006 - 08:37 PM
Last post by: heavensounds


 



RSS Open Discussion Time is now: 6th November 2009 - 07:40 PM

Web Hosting Powered by ComputingHost.com.