1

I have this little snippet here, and it returns false even if it satisfies the if statement.

$urls = $_POST['links'];
    trim($urls);
$urls = explode("\r\n",$urls);
foreach($urls as $url){
    trim($url);
    if(strpos($url,'http://') === false)
        $url = 'http://'.$url;
    $parse = parse_url($url, PHP_URL_HOST);
    if(($parse != 'site.com') || ($parse != 'www.site.com')) //problem here
        echo 'false:'.$parse.'<br>';
    else
        echo 'true:'.$parse;
}

The input is from a textarea:

http://site.com
site.com
http://www.site.com
www.site.com

Output:

true:site.com
true:site.com
false:www.site.com
false:www.site.com

What do you think is the problem?

Jürgen Paul
  • 14,299
  • 26
  • 93
  • 133
  • Just a little tip for you: You can use arrays in GET and POST parameters e.g var[0]=abc&var[1]=def Your explode construct is a little unsafe. – peipst9lker May 02 '12 at 09:19
  • @peipst9lker sorry I didn't get what you mean, can you explain? – Jürgen Paul May 02 '12 at 09:23
  • @peipst9lker I'm not sure this would help because it seems like the 4 lines are all from the same text area. I'm also not sure explode is unsafe as you suggest. I do, however, think he should use "\n" as the delimiter rather than "\r\n". – jedwards May 02 '12 at 09:24
  • 1
    what should you suggest a safe way to do it? – Jürgen Paul May 02 '12 at 09:25
  • 1
    @peipst9lker @jedwards I saw an [answer here](http://stackoverflow.com/a/7058202/1099531) and he recommended `\r\n` – Jürgen Paul May 02 '12 at 09:30
  • @JackSpairow: Nice find! I am all too familiar with the dos v. unix line ending issues so I always stick to `\n` and trim off the `\r` if its there, but that link suggests that `\r\n` is the web standard even on unix machines, so you should be fine to go with that. Good on ya. – jedwards May 02 '12 at 09:49

4 Answers4

4

I'm not sure what you really intended, but the following line is definitely wrong:

    if(($parse != 'site.com') || ($parse != 'www.site.com')) //problem here

This will always return true, because it's true if parse is not 'site.com' but it's also true if parse is not 'www.site.com' and since it can't be both of them at the same time, it must always be true. Did you mean && instead of ||? I.e., logical AND instead of logical OR?

if(($parse != 'site.com') && ($parse != 'www.site.com')) 

EDIT: Actually, if what you've put in your question is desired behaviour, i.e., true for site.com and false for www.site.com, then you just want:

if ($parse == 'site.com')
{
    echo 'false:'.$parse;
}
else if ($parse == 'www.site.com')
{
    echo 'false:'.$parse;
}

Or maybe that's not really what you want...

Ben
  • 66,838
  • 37
  • 84
  • 108
1

Think about the logic you used in the second if statement. No matter what $parse is, you will always fall into the if branch. (That is, you will never fall into the else branch.)

Consider the following alternatives:

// "Not A AND Not B"
if(($parse != 'site.com') && ($parse != 'www.site.com'))
// ...

// "Neither A nor B"
if(!(($parse == 'site.com') || ($parse == 'www.site.com')))
// ...

// "Either A or B"
if(($parse == 'site.com') || ($parse == 'www.site.com'))
    echo 'true:'.$parse;
else
    echo 'false:'.$parse.'<br>';
// Note here we also swapped the two branches.
jedwards
  • 29,432
  • 3
  • 65
  • 92
0

Change the if line to:

if(($parse != 'site.com') && ($parse != 'www.site.com'))
h00ligan
  • 1,471
  • 9
  • 17
0

replace this line :

if(($parse != 'site.com') && ($parse != 'www.site.com'))
j0k
  • 22,600
  • 28
  • 79
  • 90
Chintan
  • 1,204
  • 1
  • 8
  • 22