troubleshooting Question

Error: Can't use function return value in write context

Avatar of BruceTD
BruceTD asked on
PHP
10 Comments1 Solution1382 ViewsLast Modified:
I'm writing a script that is supposed to parse the querystring from the referer.  I'm getting the following error message:

Fatal error: Can't use function return value in write context ... on line 35

Line 35 is:

$referrer_query_string = isset(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY) ? parse_query_string(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY)) : "";

I have two questions:
1. Why am I getting this error?
2. I put exit; on line 31, so why is the script not stopping?  It shouldn't even reach line 35.

Here is the code:

<?php
// returns query string as an associative array
function parse_query_string($query_string) {
      $name_value_pairs = explode("&", $query_string);  // put name/value pairs into an array
      $query_string_array = array();
      
      foreach ($name_value_pairs as $name_value_pair) {  // for each name/value pair...
            $name_value_array = explode("=", $name_value_pair);  // put the name and value into an array
            if (count($name_value_array) == 2) {  // if the name has a value...
                  $query_string_array[$name_value_array[0]] = $name_value_array[1];  // make the name the key, and the value the value in an array
            } else {
                  $query_string_array[$name_value_array[0]] = "";  // if no value, make the name the key, and the value empty in an array
            }
      }
      return $query_string_array;
}

// create array of custom search engine search query variable names ($search_engines)
// 'q' and 'query' are the defaults
include('querystrings.inc.php');

//initialize various variables for output
$referrer_query_string = array();
$query_variable   = "";
$search_engine    = "";

// parse referrer and request url
//$referrer_parsed = parse_url($_SERVER['HTTP_REFERER']);

print 'parse_url($_SERVER[\'HTTP_REFERER\'], PHP_URL_QUERY) = '.parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY).'<br />';
exit;

// get referrer hostname and parsed query string
$referrer_hostname = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : "";
$referrer_query_string = isset(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY) ? parse_query_string(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY)) : "";

// assume existance of a query string means it's a search engine
if (count($referrer_query_string) != 0) {
      // look for default search keywords variable
      if (isset($referrer_query_string['q']) || isset($referrer_query_string['query'])) {
                  //$query_variable = "q";
                  $query_variable = key($referrer_query_string);
                  
                  // otherwise loop through list of custom keywords
      }      else {
            foreach($search_engines as $search_engine => $query_variable)      {
                  // if the custom search engine hostname fragment occurs anywhere in the referrer hostname
                  // and the custom search engine query variable exists in the query string
                  if(strpos($referrer_hostname, $search_engine) !== false && isset($referrer_query_string[$query_variable])) {
                        break;
                  }
                  
                  // clear out foreach variables
                  $search_engine = "";
                  $query_variable = "";
            }
      }
}

// if query variable was set,
// change referrer display to the search engine keywords
if ($query_variable != "") {
      $search_engine = $referrer_hostname;
      $referrer_display = urldecode($referrer_query_string[$query_variable]);
}

?>
ASKER CERTIFIED SOLUTION
Vee_Mod

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros