cemanga Posted August 24, 2011 Share Posted August 24, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/ Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 <?php $Month = 2592000 + time(); setcookie('tblog', 'tblogvalue', $Month); print_r($_COOKIE); ?> Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261294 Share on other sites More sharing options...
cemanga Posted August 24, 2011 Author Share Posted August 24, 2011 <?php $Month = 2592000 + time(); setcookie('tblog', 'tblogvalue', $Month); print_r($_COOKIE); ?> I tried it but now it say; Array ( [phpSESSID] => b87d0f51338e4d77526e5fa17a21cb70 ) Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261297 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 it's working for me, check your browser. Maybe you don't have cookies enabled? my result: Array ( [tblog] => tblogvalue [sqliteManager_currentLangue] => 2 ) Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261305 Share on other sites More sharing options...
cemanga Posted August 24, 2011 Author Share Posted August 24, 2011 Cookies are enabled, i copied this code from my other website's index page, on that page its working perfectly but when it comes to this one, its not working at all. Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261307 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261309 Share on other sites More sharing options...
cemanga Posted August 24, 2011 Author Share Posted August 24, 2011 <? 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>";} } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261311 Share on other sites More sharing options...
codefossa Posted August 24, 2011 Share Posted August 24, 2011 Just as a warning, you shouldn't give the PHPSESSID value here. If you give the site you're on, a user can spoof your account and access anything you have permissions to use. Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261313 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 Is that your entire code? where does $sifre come from? Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261314 Share on other sites More sharing options...
cemanga Posted August 24, 2011 Author Share Posted August 24, 2011 Thanks alot, just like you said, there was some outputs. Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261316 Share on other sites More sharing options...
Adam Posted August 24, 2011 Share Posted August 24, 2011 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'; Quote Link to comment https://forums.phpfreaks.com/topic/245573-set-cookie-help/#findComment-1261323 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.