Jump to content

rename variables


phpnew

Recommended Posts

Hi,

I have a code that picks the part of URL (page with subfolder, no domain), and creates a variable of it. The purpose is to pass it on so I know from which page the click has come from.

As it's a bit cumbersome to have pagename.html or /subfolder/pagename.html being passed around, I thought I could map pages to numerals like 001, 002, 003 and so on.

Here is my first version of code:
 

$path=parse_url(strtok($_SERVER["HTTP_REFERER"],'?'), PHP_URL_PATH);

$ref = implode("/",array_slice($path,3));
if ($ref == "")
{
$page = "000";
} elseif ($ref == "page1.html") {
$page = "001";
} elseif ($ref == "page2.html") {
$page = "002";
} elseif ($ref == "/sub/pageX.html") {
$page = "003";
} else {
$page = "$path";
}

If the page is not on the list, a real page name ("$path") will be passed to warn me that I have unlisted pages.

Now, I had another version where I would use an array, as 100-ish ELSEIFs seemed a bit too much:
 

$path=parse_url(strtok($_SERVER["HTTP_REFERER"],'?'), PHP_URL_PATH);
$ref = implode("/",array_slice($path,3));

$map = array(
''=>'000', 'page1.html'=>'001', 'page2.html'=>'002', '/sub/pageX.html'=>'003'
);
$page = $map[$ref];

With the array version, I had a challenge about how to pass the page that would not exist on the list. This is all an old code I tried to use, but then abandoned the idea until now.

Questions:

  1. ELSEIF vs array?
  2. how to fix the array version?
  3. would it be possible to have the script pick the URL, write it down into a text file, assigning it a number (000, 001, 002, etc), and then passing the number only into the variable? That way I would not worry about maintaining the list of pages manually.

Thank you

Link to comment
Share on other sites

How about a function that establishes a SESSION var built from a db query that reads a table of all pagenames and returns the name and a number for each in an array?  When the function doesn't detect the SESSION array it builds it and then it simply searches the array with the pagename as the key and gives you the number.

 

No hardcoding, no confusion amongst pages with perhaps different data in them and a simple update process for new pages.

Link to comment
Share on other sites

I don't understand why you prefer cryptic numbers over paths. Those mapping gymnastics are a lot more cumbersome than simply using the (normalized) paths.

 

Also note that most webservers record the referrer by default, so you can simply grab it from the log file.

Link to comment
Share on other sites

Thanks to all. The main purpose of the script is so the referring page gets passed as a variable to sales reporting. Writing it to the txt file is temporary, just for the beginning. Seeing all the paths in reporting is not nice. Reports get downloaded, data subtotaled, so it's not a problem to have numbers like 001 etc. Plus, it ads bit of privacy, as reporting is provided by a third party.

 

So, when the sales happens, bunch of variables gets passed through already, and this one would be one more (last page where the referring click has happened). Server logs cannot help here as everything goes through networks like Commission Junction and alike.

 

I like the idea of the "smart" function. No hard coding could be implemented to multiple websites without changing anything, and having a database or just a table should not be a problem, even though I thought I would not need it at the end. +1 here, thanks.

Now, LOL, if I would know how to make that... that's why I'm here.

Link to comment
Share on other sites

Seeing all the paths in reporting is not nice.

 

How the report looks like is an entirely different story. Right now, we're talking about a sane storage format, and, no, manual numbering is no sane option.

 

 

 

Server logs cannot help here as everything goes through networks like Commission Junction and alike.

 

Where exactly do you think PHP got its information from? Hint: The same entity which writes the logs.

Link to comment
Share on other sites

Going back to your original post.  Whatever do you mean by:

 

"it's a bit cumbersome to have pagename.html or /subfolder/pagename.html being passed around,"

 

It's not like it's in your suitcase or carry-on.  It's not like you have to re-create it every time you need to use it.  It's just a variable.  And it takes no more trouble than storing an entire file's contents, or an array of 1000 items, or a simple integer value.  It's A Variable.

 

So what is this all about?

 

BTW - have you made use of the official PHP Manual to see if there is something that could make this whole question moot?  Are you aware of the many functions that break certain structures down into their components? Such as pathinfo()?  Or how about parse_url()?  That would surely be something you should read about.   I'd give you the link but maybe you would enjoy the search more.  It's not hard to find.

Link to comment
Share on other sites

You don't tell us why you are creating this scheme and we tell you we don't understand it and you don't help us.  So what kind of help are you looking for?

 

The reasons you gave don't make sense as I mentioned in a previous post.  I/we have no idea what you are talking about when you say "being passed around".  Yes - we do read OPs.  Do you read the responses?

 

Hope you find what you are looking for.  

Link to comment
Share on other sites

 

You don't tell us why you are creating this scheme and we tell you we don't understand it and you don't help us.  So what kind of help are you looking for?

 

The reasons you gave don't make sense as I mentioned in a previous post.  I/we have no idea what you are talking about when you say "being passed around".  Yes - we do read OPs.  Do you read the responses?

 

I really appreciate this and I apologize.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.