Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-25-2010, 05:13 AM   PM User | #1
Phred
New Coder

 
Join Date: Oct 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
Phred is an unknown quantity at this point
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');
Phred is offline   Reply With Quote
Old 12-25-2010, 06:13 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 12-25-2010, 06:54 PM   PM User | #3
Phred
New Coder

 
Join Date: Oct 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
Phred is an unknown quantity at this point
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.
Phred is offline   Reply With Quote
Old 12-25-2010, 07:17 PM   PM User | #4
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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
DJCMBear is offline   Reply With Quote
Old 12-25-2010, 10:09 PM   PM User | #5
Phred
New Coder

 
Join Date: Oct 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
Phred is an unknown quantity at this point
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
Phred is offline   Reply With Quote
Old 12-25-2010, 11:02 PM   PM User | #6
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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
DJCMBear is offline   Reply With Quote
Old 12-26-2010, 12:38 AM   PM User | #7
Phred
New Coder

 
Join Date: Oct 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
Phred is an unknown quantity at this point
So, would there be some advantage to using it? It seems like the only difference is the value returned.
Phred is offline   Reply With Quote
Old 12-26-2010, 01:37 AM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 12-26-2010, 02:14 AM   PM User | #9
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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
DJCMBear is offline   Reply With Quote
Old 12-26-2010, 03:02 AM   PM User | #10
Phred
New Coder

 
Join Date: Oct 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
Phred is an unknown quantity at this point
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)
Phred is offline   Reply With Quote
Old 12-26-2010, 04:57 AM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 12-26-2010, 11:25 PM   PM User | #12
Phred
New Coder

 
Join Date: Oct 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
Phred is an unknown quantity at this point
Yep, I thot that had something to do with datatype.
Thanx for the help, guys!
Phred is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:28 PM.


Advertisement
Log in to turn off these ads.