tags:

views:

788

answers:

4

I added following few lines on top of my PHP code, but it is giving 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;
}
?>

I even tried: $_COOKIE("CaptchaResponseValue"). The cookie is successfully set and is available. Why it is giving error when I am using $_COOKIE?

+4  A: 

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.

Asaph
+10  A: 

It should be $_COOKIE['name'], not $_COOKIE('name')

$_COOKIE is an array, not a function.

Niyaz
+2  A: 

If you wanna ascertain if cookie is set...use

if (isset($_COOKIE['cookie']))
Shankar Ramachandran
@anonymous Why the DV?
Shankar Ramachandran
Because of how the vote-system works. People upvote answers they find more relevant and more helpfull to the question. And people down-vote answers that they find less interesting. I imagine that is why you were down-voted.
Filip Ekberg
@Filip Ekberg Thats enlightening...it wud be great if these anonymous souls left a comment as to why....but I guess that is not really a widespread practice :)
Shankar Ramachandran
I just left you a comment.
Filip Ekberg
+6  A: 

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 which symbolise which index to get the data from so by doing $_COOKIE['test'] you are basicly saying: Give me the data from the index 'test'.

Now in your case you have two possibilities either you want to see if it is false by looking inside the cookie or see if it is not even there.

For this you use the isset function which bascily check 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 remeber is that a function in PHP can only contain a certain type 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 aswell, but mixing it with characters like $ and % will not work.

Filip Ekberg