0

This is a very strange error, i am trying to fix it without success. I am trying to check if a link contains a string:

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $actual_link;

The output is:

http://xxx.xxx.xxx.xxx/plesk-site-preview/***********.com/xxx.xxx.xxx.xxx/

Then:

if(strstr($actual_link,"plesk-site-preview")  ){
echo"<meta name='robots' content='noindex'>";
}

The problem is that strstr return false despite the substring plesk-site-preview is contained in http://xxx.xxx.xxx.xxx/plesk-site-preview/***********.com/xxx.xxx.xxx.xxx/.

How can i fix this error?

EDIT:

I have inserted before if(strstr($actual_link,"plesk-site-preview") ){ the following line for testing purpose:

$actual_link='http://xxx.xxx.xxx.xxx/plesk-site-preview/***********.com/xxx.xxx.xxx.xxx/';

Now the code is working! It seems that the string assigned at the variable $actual_link is lost before the IF statement.

Daniele
  • 129
  • 12

2 Answers2

1

The documentaion says

string strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] )

Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack.

Returns the portion of string, or FALSE if needle is not found.

And you code

if(strstr($actual_link,"plesk-site-preview")) 

Perhaps it ought to be

if(strstr($actual_link,"plesk-site-preview") != "") 

as it returns a string, not a boolean if successful.

Hmm, actually it would be better to

 if(strstr($actual_link,"plesk-site-preview") !== FALSE)
Community
  • 1
  • 1
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
0

If you need check string presence of a substring in a string then you can use strpos, for example:

if(strpos($actual_link, "plesk-site-preview")){
    echo"<meta name='robots' content='noindex'>";
}

This way is better, because strpos is faster than strstr

Maksym Fedorov
  • 6,383
  • 2
  • 11
  • 31
  • I have tried with your code but the error has not been fixed! – Daniele Sep 15 '18 at 21:47
  • @Daniele, This condition is correct. You can check it, for example: `if(strpos($actual_link, "plesk-site-preview")){ echo "Substing finded"; }` You should check other code. – Maksym Fedorov Sep 15 '18 at 21:54
  • 1
    @Daniele, I was tested it. My code: `$actual_link = "http://84.200.70.56/plesk-site-preview/***********.com/84.200.70.56/"; if(strpos($actual_link,"plesk-site-preview") ){ echo "Substring found"; }` and it works – Maksym Fedorov Sep 15 '18 at 22:04
  • Try print $actual_link variable before if statement – Maksym Fedorov Sep 15 '18 at 22:17
  • `strpos($actual_link, "plesk-site-preview") !== FALSE` will solve your problem – Martin Nov 10 '18 at 17:28