How to use 2 addresses in the if condition ( !strpos( $_SERVER['REQUEST_URI'] , '/') )

Asked

Viewed 129 times

1

What to do to use two addresses at the same time in this condition?

OBS.: I need this code not to load the HTML if one or another address is accessed

<?php 
if ( !strpos( $_SERVER['REQUEST_URI'] , '/') ){ 
?> 
<a id="seloEbit" href="http://www.ebit.com.br/90809" target="_blank" data-noop="redir(this.href);"> </a> 
<script type="text/javascript" id="getSelo" src="https://imgs.ebit.com.br/ebitBR/selo-ebit/js/getSelo.js?90809"> </script> 
<?php 
} 
?>
  • I didn’t get it well, you want to add this JS and this link if the page is accessed at home?

  • No, I just didn’t put the address there and used the example "/". I need this html NOT to load if it is accessed 'checkout/librepag' or 'gerencianet/success&payment'

  • if (condition1 || condition2) { /* Code Here */ } or preg_grep(), or switch ($var) { /* Code Here */ } ??

  • I tried to use if ( !strpos( $_SERVER['REQUEST_URI'] , 'gerencianet/success&payment') || !strpos( $_SERVER['REQUEST_URI'] , 'checkout/librepag')) and it didn’t work. That’s the way it is?

2 answers

1


Use the logical operator &&:

<?php 
if ( !strpos( $_SERVER['REQUEST_URI'] , 'alguma coisa') && !strpos( $_SERVER['REQUEST_URI'] , 'outra coisa') ){ 
?> 
<a id="seloEbit" href="http://www.ebit.com.br/90809" target="_blank" data-noop="redir(this.href);"> </a> 
<script type="text/javascript" id="getSelo" src="https://imgs.ebit.com.br/ebitBR/selo-ebit/js/getSelo.js?90809"> </script> 
<?php 
} 
?>

It means both conditions must be false to enter the if.

0

There are several ways to make this check:

Example 1: Here we will use the if with the OR or simply ||.

$request_uri = $_SERVER["REQUEST_URI"];

if(
    strpos($request_uri, "checkout/librepag") === false ||
    strpos($request_uri, "gerencianet/success&payment") === false
){
    echo "Exibe html";
}

It works, however, if the value of REQUEST_URI for gerencianet/success&orderId=998787&payment, it won’t work.

Example 2: Here we will use the if with Regex (Expressão Regular)

$request_uri = $_SERVER["REQUEST_URI"];

if(
    !preg_match("/(checkout\/librepag|gerencianet\/success.+&payment)/", $request_uri)
){
    echo "Exibe html";
}

Works for any value you have checkout/librepag and gerencianet/success&payment. I mean, it’ll work for: 1. gerencianet/success&orderId=998787&payment, 2. checkout/librepag and also 3. ZZZZZ/checkout/librepag/ZZZZZ

  • I used the second example, but it didn’t work as you described it

  • What is the value of the variable $_SERVER["REQUEST_URI"] when you try? If possible add echo $_SERVER["REQUEST_URI"]; before the if and put here the result.

  • appears this: /index.php? route=checkout/librepag And loads html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.