2016-02-13 49 views
0

我有一個包含文件時的衝浪者來自某些網站的腳本,它看起來像這樣:如何使用strpos()insted的的preg_match()的

<?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'); 
} 
?> 

但它殺死了我的服務器,我想用吊索取代它(),但我不知道怎麼回事,我嘗試這樣做:

<?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'); 
} 
?> 

但它不工作:(

+1

你說的這個'strops'是什麼? – adeneo

+0

strpos()也許? – aletzo

+0

strpos()不使用正則表達式 – FareedMN

回答

2
<?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'); 
} 
?> 
+0

這是可行的。謝謝! – Belhor

+0

也可能不是一個壞主意,而是使用stripos。它與個案無關,誰知道哪種情況下你會在referer header中得到... – kay27

0

看看PHP文件位置:http://php.net/manual/en/function.strpos.php

Strpos在另一個字符串中找到一個特定的字符串,所以你不能使用正則表達式。你可以找到一個特定的字符串。

e.g

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

將包含google.com任何字符串返回true。如果你想檢測多個不同的字符串,你可以將多個strpos結合在一起(使用一個或一個操作符),或者堅持使用當前的方法。

+0

謝謝。我能做類似strpos('google.com \ /main.html',$ referral')或strpos('google.com \ /main.html | yahoo.com \ /two.html',$ referral')的內容嗎? ? – Belhor

+0

你可以這樣做: 'if(strpos('google.com',$ referal')或strpos('yahoo.com',$ referal')){'等,但它不是特別整潔。 – Tom