简体   繁体   中英

Matching the url based on the page the user is on

I'm trying to right a simple if statement to basically say that if the user is on page_A then echo "i am on page A"; and if I'm on page_B then echo "i am on the page B"; and I'm using the PHP function $_SERVER['REQUEST_URI']; to check if they match what is in my string that is stored in the variable $page_A and $page_B .

my code is as follows:

<?php
    $page_A = "/test.co.za/voice/";
    $page_B = "/test.co.za/artistes/";
    $match = $_SERVER['REQUEST_URI'];

    if ( $page_A == $match ) {
        echo "i am on page A";
    }elseif ( $page_B == $match ) {
        echo "i am on the page B";
    }
?>

Where am i going wrong or is there another way i can achieve getting this to work?

Thanks team :)

if(strpos($_SERVER['REQUEST_URI'],'voice')!==false){
  echo "i am on page A";
}elseif(strpos($_SERVER['REQUEST_URI'],'artistes')!==false){
  echo "i am on the page B";
}

I don't quite understand how you are doing this, if you have 2 different pages you always know at which page you are.

If you are using htaccess to redirect the request to a unique file, you should do something like this: Add this line in htaccess:

RewriteRule ^([0-9a-zA-Z-_]*)\/?([0-9a-zA-Z-_]*)\/?([0-9a-zA-Z-_]*)\/?([0-9a-zA-Z-_]*)\/?$ index.php?p=$1&s=$2&d=$3&ID=$4&%{QUERY_STRING}

Now, if you open for instances: http://test.co.za/voice/ this will be redirected to /index.php. At index.php you can read:

$page = $_REQUEST['p'];
if ($page == 'voice') { //do stuff }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM