We help IT Professionals succeed at work.

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

BruceTD
BruceTD asked
on
1,376 Views
Last Modified: 2013-11-05
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]);
}

?>
Comment
Watch Question

BraveBrainCNC programmer
CERTIFIED EXPERT

Commented:
Any particular reason your don't use $_REQUEST or $_GET which already has an array of the query string?
CERTIFIED EXPERT

Commented:
Isn't there missing a ')' before the '?' in the mentioned line?

The problem with 'exit' may be due to some missing \' in the line before ...
CERTIFIED EXPERT

Commented:
Sorry, second comment was nonsense ... the 's are correct there.
Kiran Paul VJComputer Engineer
CERTIFIED EXPERT

Commented:
the most probable reason is

parse_query_string function returns an array ($query_string_array), and you are trying to assign an array to a string/ordinary variable in line 31.

////
Line 31
$referrer_query_string = isset(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY) ? parse_query_string(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY)) : "";
///
Kiran Paul VJComputer Engineer
CERTIFIED EXPERT

Commented:
try this

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

instead of

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

Commented:
parse_url() takes only one parameter and returns an array of data.

Replace this:

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

With:

$referrer_query_string = array();
if (!empty($_SERVER['HTTP_REFERER'])) {
  $url = parse_url($_SERVER['HTTP_REFERER']);
  if (!empty($url['query'])) {
    $referrer_query_string = parse_query_string($url['query']);
  }
}

Also, review the way you handle $query_variable ! There is a code block where you use it in a foreach loop, and then you use it as if it was one plain variable. This could cause trouble.

Author

Commented:
Thank you all for your suggestions.  Unfortunately, none of them helped.

Zoppo:
There was a missing a ')' before the '?' in the mentioned line, but adding it in did not fix either problem.

BraveBrain:
$_REQUEST and $_GET will only get the query string for this page.  Im trying to parse the query string from the referrer URL (The search engine URL that the visitor clicked on to reach my page).

kiranvj:
$referrer_query_string is defined as an array on line 23:

$referrer_query_string = array();

cpulse:
According to php.net (http://www.php.net/parse_url), since version 5.1.2, parse_url() can take as a second parameter any of its several components.

Also, though $query_variable is used in this foreach loop:

foreach($search_engines as $search_engine => $query_variable)

it is not used as an array.  The foreach loop is looping through the $search_engines array and on each iteration assigning the key from that array element to $search_engine and the value to $query_variable, each as string/ordinary variables.

Author

Commented:
Ok, I solved the problem.  According to php.net, isset() only works with variables as passing anything else will result in a parse error.  I was attempting to pass the result of a function call to isset(), and it was choking on that.  So I assigned the result of the function call to a variable, then fed that variable to isset() as follows, and it works fine:

$referrer_parsed = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);

$referrer_query_string = isset($referrer_parsed) ? parse_query_string($referrer_parsed) : "";
Kiran Paul VJComputer Engineer
CERTIFIED EXPERT

Commented:
great, thats a good info and glad to know the problem was solved
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.