Welcome to WebmasterWorld Guest from 50.19.190.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

script optimization

     
3:19 am on Mar 20, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 2, 2006
posts:2112
votes: 2


I use a script where I pull data from search engines. I wonder about the code below. In some cases I have over 200 entries for various sites. As I see it right now, it goes line by line until it hits or does not find.
Can this be optimized to be faster and lighter on the server?

$site_query = array(
"google" => "q:01",
"yahoo" => "p:02",
"bing" => "q:03",
"live" => "q:04",
"aol" => "q:05",
and so on
);


Thanks
10:52 am on Mar 22, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


That's just an array. Are you saying you are looping through it? If so, and you are merely looking for a certain entry, then I would test for the index being set instead and yes, it will be much faster. I'm not sure how or what format your data is arriving but I'll assign it to a variable here first. But the main thing is to use isset():
$searchEngine = 'live'; 
if (isset($site_query[$searchEngine])) {
// found
$q = $site_query[$searchEngine]; // $q = q:04
} else {
// not found
}
4:16 pm on Mar 22, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 2, 2006
posts:2112
votes: 2


Thanks.

I believe that the array was built to make it faster then when using IF approach.

This is what happens after the array:

foreach ($_GET as $key => $value)
{
if(! $_GET[$key]) {
unset($_GET[$key]);
$_GET['var1'] = $key;
break;
}
}

where "var1" is one of my variables.

After that, there are several lines of code to get the query string:

$v1 = $_GET['var1']; //my variable attached to incoming ad URL

list($url, $querystring) = split("\?", $_SERVER['HTTP_REFERER']);
$url = preg_replace("|http://([^\/]+)/.*|", "\\1", $url);

$referer = $url;
foreach ($site_ques as $key => $value)
{
if(preg_match("/.*\.$key\..*/", $url)||preg_match("/^$key\..*/", $url))
{
list($ques, $referer) = split(":", $value);
break;
}
}
$v3 = $referer;

if (isset($ques) && preg_match("/^([^\&]+\&)*$ques=([^\&]*)(\&[^\&]+)*$/", $querystring))
{
$v2 = preg_replace("/^([^\&]+\&)*$ques=([^\&]*)(\&[^\&]+)*$/", "\\2", $querystring);
} else {
$v2 = 'FindQuery';
}


I just wondered if there was a faster way than having a list if search engines in an array. This was made for me for my sites and this is the stripped version for direct linking.

Thank you

[edited by: engine at 4:27 pm (utc) on Mar 22, 2013]
[edit reason] Disable graphic smile faces for this post [/edit]

1:25 am on Mar 24, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 2, 2006
posts:2112
votes: 2


But with that there will be no number assigned to each source search engine. That's also what the script does. Instead of passing through the names of referring sites, I replace it with the code (plain number).

Thanks
3:57 pm on Mar 28, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


This part is where I would focus first, if at all possible ...
foreach ($site_ques as $key => $value) { 
if(preg_match("/.*\.$key\..*/", $url)||preg_match("/^$key\..*/", $url)) {
list($ques, $referer) = split(":", $value);
break;
}
}

I was recommending going for the direct hit using isset() rather than loop through the entire array. However, I see now that you have a regular expression match going on in there against the referrer to figure out which SE it is. In order to use the isset() approach you would need just the key index value (google|yahoo|bing|etc.) out of the referrer first. Which presents a whole new task. Perhaps your loop *is* your best option.