Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Problem Looking For 2 Variables In String

    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.

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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by Fou-Lu View Post

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


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

    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.

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by Phred View Post
    Great! That does it. Altho I swapped 'strpos' with 'stristr' just in case some joker enters the word in uppercase letters. Some people do that.

    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.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question

    Quote Originally Posted by DJCMBear View Post
    You could have used stripos(); which does the same thing.
    I am not familiar with that code. What's the difference from stristr

  • #6
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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'.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #7
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    So, would there be some advantage to using it? It seems like the only difference is the value returned.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by Fou-Lu View Post
    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.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #10
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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)

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #12
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Yep, I thot that had something to do with datatype.
    Thanx for the help, guys!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •