BruceTD
asked on
Error: Can't use function return value in write context
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_REFER ER'], PHP_URL_QUERY) ? parse_query_string(parse_u rl($_SERVE R['HTTP_RE FERER'], 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_arra y[0]] = $name_value_array[1]; // make the name the key, and the value the value in an array
} else {
$query_string_array[$name_ value_arra y[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_R EFERER']);
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_REFER ER'], PHP_URL_QUERY) ? parse_query_string(parse_u rl($_SERVE R['HTTP_RE FERER'], PHP_URL_QUERY)) : "";
// assume existance of a query string means it's a search engine
if (count($referrer_query_str ing) != 0) {
// look for default search keywords variable
if (isset($referrer_query_str ing['q']) || isset($referrer_query_stri ng['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_hostna me, $search_engine) !== false && isset($referrer_query_stri ng[$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[$qu ery_variab le]);
}
?>
Fatal error: Can't use function return value in write context ... on line 35
Line 35 is:
$referrer_query_string = isset(parse_url($_SERVER['
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_
$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_
} else {
$query_string_array[$name_
}
}
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.
//initialize various variables for output
$referrer_query_string = array();
$query_variable = "";
$search_engine = "";
// parse referrer and request url
//$referrer_parsed = parse_url($_SERVER['HTTP_R
print 'parse_url($_SERVER[\'HTTP
exit;
// get referrer hostname and parsed query string
$referrer_hostname = isset($_SERVER['HTTP_HOST'
$referrer_query_string = isset(parse_url($_SERVER['
// assume existance of a query string means it's a search engine
if (count($referrer_query_str
// look for default search keywords variable
if (isset($referrer_query_str
//$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_hostna
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_
}
?>
Any particular reason your don't use $_REQUEST or $_GET which already has an array of the query string?
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 ...
The problem with 'exit' may be due to some missing \' in the line before ...
Sorry, second comment was nonsense ... the 's are correct there.
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_REFER ER'], PHP_URL_QUERY) ? parse_query_string(parse_u rl($_SERVE R['HTTP_RE FERER'], PHP_URL_QUERY)) : "";
///
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['
///
try this
$referrer_query_string[] = isset(parse_url($_SERVER[' HTTP_REFER ER'], PHP_URL_QUERY) ? parse_query_string(parse_u rl($_SERVE R['HTTP_RE FERER'], PHP_URL_QUERY)) : "";
instead of
$referrer_query_string = isset(parse_url($_SERVER[' HTTP_REFER ER'], PHP_URL_QUERY) ? parse_query_string(parse_u rl($_SERVE R['HTTP_RE FERER'], PHP_URL_QUERY)) : "";
$referrer_query_string[] = isset(parse_url($_SERVER['
instead of
$referrer_query_string = isset(parse_url($_SERVER['
parse_url() takes only one parameter and returns an array of data.
Replace this:
$referrer_query_string = isset(parse_url($_SERVER[' HTTP_REFER ER'], PHP_URL_QUERY) ? parse_query_string(parse_u rl($_SERVE R['HTTP_RE FERER'], PHP_URL_QUERY)) : "";
With:
$referrer_query_string = array();
if (!empty($_SERVER['HTTP_REF ERER'])) {
$url = parse_url($_SERVER['HTTP_R EFERER']);
if (!empty($url['query'])) {
$referrer_query_string = parse_query_string($url['q uery']);
}
}
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.
Replace this:
$referrer_query_string = isset(parse_url($_SERVER['
With:
$referrer_query_string = array();
if (!empty($_SERVER['HTTP_REF
$url = parse_url($_SERVER['HTTP_R
if (!empty($url['query'])) {
$referrer_query_string = parse_query_string($url['q
}
}
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.
ASKER
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.
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.
ASKER
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_R EFERER'], PHP_URL_QUERY);
$referrer_query_string = isset($referrer_parsed) ? parse_query_string($referr er_parsed) : "";
$referrer_parsed = parse_url($_SERVER['HTTP_R
$referrer_query_string = isset($referrer_parsed) ? parse_query_string($referr
great, thats a good info and glad to know the problem was solved
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.