Jump to content

Set Cookie Help


cemanga

Recommended Posts

Hi,

I was trying to use setcookie on my website but when I try to use, it wasn't setting anything and then I tried to make sure if it's setting something, I added

echo $_COOKIE;

But it shows Array (just the word) instead of tblogvalue.

 

This is the code I'm using;

$Month = 2592000 + time();
setcookie(tblog, tblogvalue, $Month);

echo $_COOKIE;

Link to comment
https://forums.phpfreaks.com/topic/245573-set-cookie-help/
Share on other sites

php manual says:

If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.

 

do you have output before that code?

Link to comment
https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261309
Share on other sites

<?
if(isset($_POST['login']))
{
if (isset($_POST["password"]) && ($_POST["password"]=="$pass")) {

$Month = 2592000 + time();
setcookie('tblog', 'tblogvalue', $Month);
print_r($_COOKIE);

}
else
{
if (isset($_POST['password']) || $password == "") {
echo "<b>Password Wrong</b><br>Please reenter your password!</a><br><br>";}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261311
Share on other sites

Actually that won't have been the whole issue.

 

<?php

setcookie('foo', 'bar');
print_r($_COOKIE);

?>

 

If you try running just this code, you will notice that the cookie is not included within the output the first time you run it. That's because $_COOKIE is populated prior to the execution of your code, and a call to setcookie() does not actually create the cookie. It simply adds a header to the response sent by the server to the browser, telling the browser to create it. So realistically you don't know for sure that the cookie has been set until the next request, however you can populate the $_COOKIE array yourself so that you can use it later in your code:

 

$_COOKIE['foo'] = 'bar';

Link to comment
https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261323
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.