...

View Full Version : Problem Looking For 2 Variables In String



Phred
12-25-2010, 05:13 AM
I'm trying to do a redirect but ONLY if the referring page has two specific words in its URL.

For example, if someone comes to my site from Google after they've searched for the word 'disney', then I want them to be redirected to a specific page.

This is what I'm using, it's supposed to scan the referrer for two words (google and disney) and then redirect to another page but ONLY if the person came from 'google' AND searched for the word 'disney'.

It redirects okay if I only scan for one word, but it won't redirect for both words.




$refer = $_SERVER[HTTP_REFERER];
$goo = 'google';
$disn = 'disney';
if (strlen(stristr($refer,$goo,$disn))>0)
header('Location: http://demopage.com');

Fou-Lu
12-25-2010, 06:13 AM
That's not right.
You cannot combine these types of string manipulations to search for multiple strings within a string. How you approach is depends on how complex and accurate you want it to become.


if (strpos($refer, $goo) !== false && strpos($refer, $disn) !== false)
{
header('Location: http://demopage.com');
}

This doesn't have a lot of variance in it though, so it will not work properly with dynamic variables. If you have an unknown number of search terms that require unique control, then look at using a pcre pattern.

Phred
12-25-2010, 06:54 PM
if (strpos($refer, $goo) !== false && strpos($refer, $disn) !== false)
{
header('Location: http://demopage.com');
}





Great! That does it.:thumbsup: Altho I swapped 'strpos' with 'stristr' just in case some joker enters the word in uppercase letters. Some people do that.:cool:

The problem is that people are typing in specific search words but google keeps displaying the wrong page from my web site in the search results. So people click thru to the wrong page and then hit their 'back' key instead of going to the correct page.

DJCMBear
12-25-2010, 07:17 PM
Great! That does it.:thumbsup: Altho I swapped 'strpos' with 'stristr' just in case some joker enters the word in uppercase letters. Some people do that.:cool:

The problem is that people are typing in specific search words but google keeps displaying the wrong page from my web site in the search results. So people click thru to the wrong page and then hit their 'back' key instead of going to the correct page.

You could have used stripos(); which does the same thing.

Phred
12-25-2010, 10:09 PM
You could have used stripos(); which does the same thing.

I am not familiar with that code. What's the difference from stristr:confused:

DJCMBear
12-25-2010, 11:02 PM
strstr checks if theres a string in a string so if you search for 'be' in 'because' it will return true and the same with stristr but that as you know allows 'Be' to return true when searching in 'because' now strpos doesn't return true or false but returns the position in the string so strpos('hello there how are you?','t') this will return '6' as it is the sixth letter in and stripos is case insensitive so if you did this. stripos('hello there how are you?','T') it would return '6' where as if you did this strpos('hello there how are you?','T') it would return '-1'.

Phred
12-26-2010, 12:38 AM
So, would there be some advantage to using it? It seems like the only difference is the value returned.

Fou-Lu
12-26-2010, 01:37 AM
The only difference is the return type.
The strstr returns a cut out section of a string, while strpos returns the position of the string. In general numbers are always faster to process than strings (including boolean as an integer, which it pretty much is in PHP since C doesn't have a boolean).
When there is no match in strpos though, it won't return -1. Rather it returns false. In PHP, it is datatype weak which is why I have used if (strpos($str, $needle) !== false). This allows us to check if the needle exists anywhere within the $str. In the event that it starts at the first letter of the string it will return 0. Checking this: if (strpos('hello world', 'h')) will return false since 0 is considered not true. This is why I check for !== false.

DJCMBear
12-26-2010, 02:14 AM
When there is no match in strpos though, it won't return -1. Rather it returns false.

Damn I was thinking about Javascript as their indexOf('') function returns -1 lol I should have known it returns 0 to be honest lol.

Phred
12-26-2010, 03:02 AM
The only difference is the return type.
The strstr returns a cut out section of a string, while strpos returns the position of the string. In general numbers are always faster to process than strings (including boolean as an integer, which it pretty much is in PHP since C doesn't have a boolean).
When there is no match in strpos though, it won't return -1. Rather it returns false. In PHP, it is datatype weak which is why I have used if (strpos($str, $needle) !== false). This allows us to check if the needle exists anywhere within the $str. In the event that it starts at the first letter of the string it will return 0. Checking this: if (strpos('hello world', 'h')) will return false since 0 is considered not true. This is why I check for !== false.

One question... Would this work the same?

if (strpos($str, $needle) != false)

My understanding is, not equal to should be written as: !=.
Or is it to be written as !== when using string data?

(Still learning php:o)

Fou-Lu
12-26-2010, 04:57 AM
That is not the same no.
!== is using datatype explicit checking.
if (strpos($str, $needle) != false) will fail if the match is the first character, since strpos will return 0 as its offset location (all arrays in PHP are 0 based, including string/char*'s). So an if(0) is the same as an if (false) in PHP.
When you use explicit checking (and there are several functions including strpos that do so), you must use !== and === to make a comparison since you otherwise cannot tell the difference between a fail and the first offset.

Phred
12-26-2010, 11:25 PM
Yep, I thot that had something to do with datatype. :D
Thanx for the help, guys! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum