Forum Moderators: coopster

Message Too Old, No Replies

explode path

show both folder and file name

         

smallcompany

10:14 pm on Oct 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,

I have this:

<?php
$path = explode('/', $_SERVER['HTTP_REFERER']);
$ref = $path[count($path)-1];
echo $ref;
?>

It shows the referring file name. If I change "-1" to "-2" it shows the folder name but no file name.

How do I get both folder if exists and a file name?

Thanks

Dijkgraaf

10:42 pm on Oct 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have and skip the first 3 parts and then iterate through the remaining parts and concatenating them back together with a / between them. A for loop would be the way to go with, 2 to count($path)-1
This would then take care of subfolders etc. as well.

httpwebwitch

2:19 pm on Oct 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ack. don't use a for loop. And you don't need to count() the array either.

explode() takes the file path "a/b/c/d/filename" and splits it into an array that looks like ['a','b','c','d','filename']

if you want the last two items glued back together like "d/filename", do this:

implode("/",array_slice($path,-2));

if you want everything from the 2nd position to the end - say it's a path relative to "a/b" and you want "c/d/filename", do this:

implode("/",array_slice($path,2));

smallcompany

12:30 am on Oct 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



implode("/",array_slice($path,2));


Cool, thanks!

I had to put 3 instead of two to get read of the domain name.

Thanks

Additional question:

As I'm passing those parts of URL around, I would like to code them simply to have them shorter. I did this:

$ref = implode("/",array_slice($path,3));
if ($ref == "page1.html")
{
$page = "001";
} elseif ($ref == "page2.html") {
$page = "002";
} elseif ($ref == "page3.html") {
$page = "003";
} else {
$page = "N/A";
}
echo $page;

I won't really echo at the end, but have it just for the purpose of testing.

Now, from the coding and performance perspective, is there a better way of doing it? I'll have cases where I'll have couple of hundred pages. Array?

Thanks

httpwebwitch

2:46 am on Oct 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well, there are four ways I'd suggest. Here they are in order of speed and maintainability, from worst to best, and subsequently in reverse order from easiest to most challenging / least flexible:

1) use switch/case instead of if/elseif

[php.net...]

anecdotal evidence says that a switch/case is possibly slower than an if/else, but it's a nice way to organize a large block of conditional controls

2) define all your pages as an array, like this:

$map = array(
'page1.html'=>'001', 'page2.html'=>'002',
'page2.html'=>'003', 'page3.html'=>'004'
...
);

then,
$page = $map[$ref];

Arrays are fast, and compared to an if/else/switch/case it's definitely less cluttery. Arrays don't scale particularly well since the whole array has to be defined at runtime and held in memory - even though you'll only be using one element in the array.

3) store the page mapping in MEMCACHE, or SQLITE, and look them up.

SQLite uses memory caching very efficiently - it's faster than a MySQL database, though it doesn't have the convenience of nifty software for manipulating your data.

[php.net...]

Memcache is a simple key->value storage that's 100% in memory. Data retrieval doesn't get any faster.

[php.net...]

4) if the mapping of pages follows a consistent pattern, do it algorithmically.

$page = str_pad( substr($ref,4,strpos($ref,".")), 3, "0");

an algorithm requires no storage, requires no extra resources, and scales infinitely.

smallcompany

5:19 am on Oct 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for all this. I'll do the homework.

smallcompany

3:47 am on Nov 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There seem to be a problem with this when someone lands onto the page and clicks onto the exit link that is being tracked with this code:

$path = explode('/', $_SERVER['HTTP_REFERER']);
$ref = implode("/",array_slice($path,3));
if ($ref == "page1.html")
{
$page = "001";
} elseif ($ref == "page2.html") {
$page = "002";
} elseif ($ref == "page3.html") {
$page = "003";
} else {
$page = "N/A";
}

It looks like the code works only if the visitor browses through at least two pages of my site before he/she clicks onto the exit link.

In other words, if the visitor comes in onto page1 and comes out from the same page, I get N/A.
If the visitor comes onto page1, then page2 and/or whatever else, I get the corresponding number.

What I do is I created an HTML redirect in order to track click conversions with analytic software.

So, among other code I have this in that HTML which is also parsed as PHP:

<?php
include "/full-server-path/referer.php";
?>

and

<meta http-equiv="refresh" content="0;url=<?php print "exit.php?p=$page" ?>" />

The exit.php file finally puts all blocks together by doing this (among other things that work 100%):

$p = $_GET['p'];

and puts $p into the desired place.

All this works fine as I always get either the right number for the corresponding page, or the N/A.

Knowing that I put all pages into the script, N/A tells me that the server is not getting the page with $_SERVER['HTTP_REFERER'] when a visitor just arrives from a search engine.

Why is that?

Thanks

rocknbil

5:10 pm on Nov 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You understand what the referer environment variable does, right? It's the last page you came from - and even then it's only if the browsing client actually sends the referrer, which is not always the case. So it will always fail on a direct URL request. Perhaps someone will have a more stable idea if you explain exactly why you need to do this, for example,

HTML redirect in order to track click conversions with analytic software.


You shouldn't have to do this is you're using something like Google Analytics.

smallcompany

5:27 pm on Nov 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks.

My tracking is composed of my own parameters and Piwik code.

I insert parameters like keyword, AdWords campaign identifier, search engine, etc into the end of my affiliate links. One of those parameters is the last visited page.

I use PHP to do all this. As I can't just insert Piwik (or GA) javascript code into pure PHP, I came up with the ides of having HTML page which does the redirect so my Piwik code gets executed.

All this requires me to carry my parameters around. Some are being written to/called from a cookie, and some like the last page are just carried by a GET call.

I hope this helps.

In any case, if it's true that 50% of clients may not send the referring page, I obviously can't do much about it. But is that really the cause?