Jump to content
xisto Community
Sign in to follow this  
snlildude87

Get Filename Of Referring Url php code to get filename of referring URL

Recommended Posts

Hey :P

 

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://forums.xisto.com/no_longer_exists/ redirects the user to Page B which has a URL of http://forums.xisto.com/no_longer_exists/.

 

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 :)) but that did not work so well.

 

Thanks in advance!

Share this post


Link to post
Share on other sites

mobious, try closing the CODE tag, buddy.

I would use something along the lines of:

$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);  }}

Share this post


Link to post
Share on other sites

$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://forums.xisto.com/no_longer_exists/

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

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

$filename will hold the filename you are looking for.

Share this post


Link to post
Share on other sites

I tried no9t9's code because his post flowed so well, and it worked! And I also love arrays :) 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!

Share this post


Link to post
Share on other sites

$_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

Share this post


Link to post
Share on other sites

this code will not work in the example below

If the url to be parsed is, for eaxample: http://forums.xisto.com/no_longer_exists/

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 /txtmngr/images/smileys/smiley2.Gif

 

 

-reply by johnKeywords: php get requested file name from url

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.