PHP Error: Function name must be a string
I added the following lines to the top of my PHP code, but it throws an error:
Fatal error: Function name must be a string in /home/reg.php on line 2
<?php
if ($_COOKIE('CaptchaResponseValue') == "false")
{
header('location:index.php');
return;
}
?>
开发者_Python百科I tried: $_COOKIE("CaptchaResponseValue")
. The cookie is successfully set and is available. Why is it giving me an error when I am using $_COOKIE
?
It should be $_COOKIE['name']
, not $_COOKIE('name')
$_COOKIE
is an array, not a function.
Using parenthesis in a programming language or a scripting language usually means that it is a function.
However $_COOKIE
in php is not a function, it is an Array. To access data in arrays you use square braces ('[' and ']') which symbolize which index to get the data from. So by doing $_COOKIE['test']
you are basically saying: "Give me the data from the index 'test'.
Now, in your case, you have two possibilities: (1) either you want to see if it is false--by looking inside the cookie or (2) see if it is not even there.
For this, you use the isset function which basically checks if the variable is set or not.
Example
if ( isset($_COOKIE['test'] ) )
And if you want to check if the value is false and it is set you can do the following:
if ( isset($_COOKIE['test']) && $_COOKIE['test'] == "false" )
One thing that you can keep in mind is that if the first test fails, it wont even bother checking the next statement if it is AND ( &&
).
And to explain why you actually get the error "Function must be a string", look at this page. It's about basic creation of functions in PHP, what you must remember is that a function in PHP can only contain certain types of characters, where $
is not one of these. Since in PHP $
represents a variable.
A function could look like this: _myFunction _myFunction123 myFunction
and in many other patterns as well, but mixing it with characters like $ and % will not work.
Try square braces with your $_COOKIE
, not parenthesis. Like this:
<?php
if ($_COOKIE['CaptchaResponseValue'] == "false")
{
header('Location: index.php');
return;
}
?>
I also corrected your location header call a little too.
It will be $_COOKIE['CaptchaResponseValue']
, not $_COOKIE('CaptchaResponseValue')
If you wanna ascertain if cookie is set...use
if (isset($_COOKIE['cookie']))
In PHP.js, $_COOKIE is a function ;-)
function $_COOKIE(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length,c.length).replace(/\+/g, '%20'));
}
return null;
}
via http://phpjs.org/functions/setcookie:509
A useful explanation to how braces are used (in addition to Filip Ekberg's useful answer, above) can be found in the short paper Parenthesis in Programming Languages.
<?php
require_once '../config/config.php';
require_once '../classes/class.College.php';
$Response = array();
$Parms = $_POST;
$Parms['Id'] = Id;
$Parms['function'] = 'DeleteCollege';
switch ($Parms['function']) {
case 'InsertCollege': {
$Response = College::InsertCollege($Parms);
break;
}
case 'GetCollegeById': {
$Response = College::GetCollegeById($Parms['Id']);
break;
}
case 'GetAllCollege': {
$Response = College::GetAllCollege();
break;
}
case 'UpdateCollege': {
$Response = College::UpdateCollege($Parms);
break;
}
case 'DeleteCollege': {
College::DeleteCollege($Parms['Id']);
$Response = array('status' => 'R');
break;
}
}
echo json_encode($Response);
?>
精彩评论