سؤال

I've sucesfully tracked where clicks are from and then made my site show elements based off where that click is from. A code example is this:

$referer = $_SERVER['HTTP_REFERER'];

if ( $referer == "http://www.testdomain.com/testpage" ) {
echo '<div id="bgphotos"></div>';
} else {
    echo '<div id="bgvideos"></div>';
}

So this is telling my site to display the bgphotos div if it is clicked from http://www.testdomain.com/testpage. This works awesome. I've even done even more coding to track where certain divs are clicked on within a webpage. But, I would like to show an element if it was clicked on from a category.

Now, I thought the solution would just be to change the $referer to the category webpage. But the problem is, when there are more than one pages of posts for this category, you would have to manually track each category page number... which would take forever.. An example would be:

if ( $referer == "http://www.testdomain.com/category/test/" ) {
//do this }

if ( $referer == "http://www.testdomain.com/category/test/page/2/" ) {
//do this }

if ( $referer == "http://www.testdomain.com/category/test/page/3/" ) {
//do this }

if ( $referer == "http://www.testdomain.com/category/test/page/4/" ) {
//do this }

As you can see this would just get out of hand, I would have to track every single category page possible. So is there any way to say, just track if something is clicked from a category?? Or is there any other solution to this problem?

Any help would be appreciated soo soo soo soo much! :) loll. It really would.

هل كانت مفيدة؟

المحلول

A regex would work.

if( preg_match("(^http://www\.testdomain\.com/category/test/(?:page/\d+/)?$)",$referer)) {
    // do this
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top