Belhor
Belhor

Reputation: 57

How to use strpos() insted of preg_match()

I have a script that is including files when surfer comes from certain websites, it looks like this:

<?php
$referral = $_SERVER['HTTP_REFERER'];
if (preg_match('/yahoo.com\/main.html|yahoo.com\/secound.html/', $referral)) {
require_once ('a.php');
} else if (preg_match('/google.com/', $referral)) {
require_once ('b.php');
} else {
require_once ('c.php');
}
?>

But it is killing my server and I want to replace it with strops() but I do not know how, I tried this:

<?php
$referral = $_SERVER['HTTP_REFERER'];
if (strops('/yahoo.com\/main.html|yahoo.com\/secound.html/', $referral)) {
require_once ('a.php');
} else if (strops('/google.com/', $referral)) {
require_once ('b.php');
} else {
require_once ('c.php');
}
?>

But it's not working :(

Upvotes: 0

Views: 160

Answers (2)

kay27
kay27

Reputation: 909

<?php
$referral = $_SERVER['HTTP_REFERER'];
if ((strpos($referral, 'yahoo.com/main.html')!==false)
  ||(strpos($referral, 'yahoo.com/secound.html')!==false)) {
require_once ('a.php');
} else if (strpos($referral, 'google.com')!==false) {
require_once ('b.php');
} else {
require_once ('c.php');
}
?>

Upvotes: 2

Tom
Tom

Reputation: 1068

Look at the PHP documentation here: http://php.net/manual/en/function.strpos.php

Strpos finds a particular string in another string, so you can't use regex. You can just find a particular string.

e.g

strpos('google.com', $referral')

would return true for any strings containing google.com. If you wanted to detect multiple different strings you could combine multiple strpos together (with an or operator), or stick with your current approach.

Upvotes: 0

Related Questions