0

I'm trying to make an if statement to serve different scripts based on the current URL that the user is on.

this is what I came up with so far, but it seems to not work, thanks

// url to show script A
$url_1 = 'https://www.example.com' ;

// url to serve script
$url_2 = 'htpps://www.example.com/page-to-run-script/';

$request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$is_page = strpos($request_uri, '/page-to-run-script/');

if ($is_page == true) {
    // run script A...
} else {
    // run script B...
}

Osh
  • 75
  • 9
  • Ok... so what is the question? What is the problem? – RiggsFolly Jun 13 '19 at 15:45
  • 1
    RED BOX FROM THE MANUAL: Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. – RiggsFolly Jun 13 '19 at 15:46
  • how can I target those url's to serve different content or scripts ? because for some reason this is not working. maybe there is a better way – Osh Jun 13 '19 at 15:47
  • Surely that shouls be `if ($is_page === FALSE) {` – RiggsFolly Jun 13 '19 at 15:50
  • 1
    $url_2 has a typo. – user3783243 Jun 13 '19 at 15:50
  • @RiggsFolly thanks I will check this out. is there another method of doing this – Osh Jun 13 '19 at 15:52

2 Answers2

0

It should be something like that :

$main_url= parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$main_url_segment= explode('/', $main_url);
$url_2= $main_url_segment[2];
$is_page ="page-to-run-script";
if($is_page == $url2){
   // run script A...
} else {
   // run script B...
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • @Osh It's not my answer, I just edited it to fix the formatting. – Barmar Jun 13 '19 at 16:46
  • ohh thanks. I think I found a fix how should I post it ? its [here](https://pastebin.com/WnyUhYGy) was following this stack answer [answer](https://stackoverflow.com/questions/6768793/get-the-full-url-in-php) – Osh Jun 13 '19 at 17:08
0

Found a fix to my problem thanks to all, next stage is to make it as a function that takes in an array of url's.


        // get user url
        $current_user_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

        //list of url's
        $is_page ="https://www.example.com/page-to-run-script";
        $is_category = "https://www.example.com/category";
        $is_home = "https://www.example.com";

        //check if user is in this page
        if ($is_page == $current_user_url || $is_home == $current_user_url) {
            // run script A...
        } elseif ($is_category == $current_user_url) {
            // run script B...
        }

Osh
  • 75
  • 9