Welcome to WebmasterWorld Guest from 34.238.192.150

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

rename variables

     
2:23 am on May 8, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2241
votes: 8


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:

    - ELSEIF vs array?
    - how to fix the array version?
    - 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
4:50 pm on May 8, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 10, 2005
posts:5852
votes: 200


write it down into a text file
Ick!
I'd use a database table- much easier to maintain and easy to run stats.

Psuedocode:
- check DB table for current page
-- if exists, get the ID
-- if it doesn't exist, add and get the new ID
Done!
9:33 pm on May 8, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2241
votes: 8


ick, ick!

Database would be good, and I would use it if I know how to make the smart function which would pick the referring URL, check if it exists, increment the hit, if it does not exist, add it as a new row. Done with no hard coding at all!

Super, but only if I know how to do that.
12:20 pm on May 21, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts: 334
votes: 73


- ELSEIF vs array?

You also have the SWITCH statement. It depends on how you are comfortable or not with each method and how it makes your code easy to read and modify.

- how to fix the array version?

$page = isset ( $map [ $ref ] ) ? $map [ $ref ] : $path ;

It tests if the $ref exists in the array. If it does, it returns the value, otherwise it returns $path (your default if I don't make mistake). In PHP 7 you can use the ? ? (two question marks without space) instead of the isset .

- would it be possible...

The problem is that, there is a risk two processes try to update your file at the same time. So it can result in URLs being added twice to your file, or loosing the count when the file is overwritten.

If you want to do it with a Database, as suggested by LifeinAsia. Here is an example (MySQL)

To connect to the Database :
$mysql_link = mysqli_connect ( $mysql_host , $mysql_user , $mysql_password , $mysql_dbname , $mysql_port , $mysql_socket ) ;


Considering the database has been previously created, here is how you can create a table (named "list_pages") to store your list of URL, with their ID and count
mysqli_query ( $mysql_link , 'CREATE TABLE list_pages (id INT UNSIGNED NOT NULL AUTO_INCREMENT,url VARCHAR(255) UNIQUE NOT NULL,count INT UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY(id)) ENGINE=MyISAM' ) ;


To add a new URL, update the count and get the page ID ("0" if it fails):
$url_escaped = mysqli_real_escape_string ( $mysql_link , $url ) ;
mysqli_query ( $mysql_link , 'INSERT INTO list_pages (url,count) VALUES ("' . $url_escaped . '",1) ON DUPLICATE KEY UPDATE count=count+1' ) ;

$page_id = 0 ;

if ( ( $result = mysqli_query ( $mysql_link, 'SELECT id FROM list_pages WHERE url="' . $url_escaped . '"' ) ) !== FALSE )
{
$row = mysqli_fetch_array ( $result , MYSQLI_ASSOC ) ;
mysqli_free_result ( $result ) ;
$page_id = $row [ 'id' ] ;
}


If you want the $page_id to be 3 digits, like in your post
$page_id = str_pad ( $page_id , 3 , '0' , STR_PAD_LEFT ) ;


And if you want to list all URLs
if ( ( $result = mysqli_query ( $mysql_link, 'SELECT * FROM list_pages' ) ) !== FALSE )
{
while ( $row = mysqli_fetch_array ( $result , MYSQLI_ASSOC ) )
{
// $row [ 'id' ] ;
// $row [ 'url' ] ;
// $row [ 'count' ] ;
}

mysqli_free_result ( $result ) ;
}
7:19 am on May 23, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2241
votes: 8


What can I say? Haven't tested it yet, but the effort by Peter here is tremendous. Thanks so much, I'll be back with the comment after trying it out.