I'm about to write some code for mahara. I'm trying to store a variable in a cookie. If I do, it will disappear on the next page.
Example:
foo.php:
...
$myfoo = 'bar';
setcookie('mycookie', $myfoo)
var_dump($_COOKIE)
...
executing foo.php: all the mahara cookies & 'mycookie' is set. Like expected, everything's fine.
bar.php
...
var_dump($_COOKIE)
...
executing bar.php after foo.php: only mahara standard cookies set, but no 'mycookie'.
I can't really explain that.
Also $_SESSION does not work like intended.
My server is set up correctly, cookies generally work.
Has anyone an idea?
Edit: I see the cookies via var_dump in my foo.php. Even if I stop to set them. They are there. But not on other pages.
<?php
$myfoo = 'bar';
setcookie('mycookie', $myfoo, time() + (86400 * 30), "/"); // 86400 = 1 day
var_dump($_COOKIE);
?>
Your cookies are expiring because
Specifies when the cookie expires because if expiry time is omitted or set to 0, the cookie will expire at the end of the session (when the browser closes). Change it to some value like time()+86400*30
For more details: https://www.w3schools.com/php/func_network_setcookie.asp
Related
I am completely baffled by this problem. Setting a cookie should be the easiest thing in the world, but for whatever reason it's just not working. Currently I'm just trying to get a test-script to work. It looks like this:
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + 86400 * 30, "/");
setcookie("act", "sj", time() + 86400 * 365);
setcookie("bbba", "Hello", time() + 86400);
echo $_COOKIE['act'];
echo $_COOKIE['bbba'];
echo $_COOKIE['user'];
None of these cookies will set. Nothing will echo, and I can not find the cookies when using the inspector. I've tried the following:
- Placing the echo $_COOKIE in another file in the same directory.
- With and without ob_start() and ob_flush()
- Using "/", "/direcotry" and nothing at all as path
- Moving the file to the root directory to see if it works there.
Nothing seems to work, and I cannot see what could possibly be wrong. Other scripts using cookies are working on the same domain - which is located on a web hotel.
Can anyone see the problem here?
Cookies will not become visible until the next loading of a page that
the cookie should be visible for. To test if a cookie was successfully
set, check for the cookie on a next loading page before the cookie
expires. Expire time is set via the expire parameter. A nice way to
debug the existence of cookies is by simply calling
print_r($_COOKIE);.
It's from php manual. You can set the value in $_COOKIE array by manual if you really want it in same page which's declared.
$_COOKIE['key'] = 'value';
echo $_COOKIE['key'];
PHP Manual setcookie
The problem was caused by whitespace at the beginning of the document.
I can't figure out why I can't remove a cookie or it's value:
I have simple log in script, when user enters correct login details, this is
setcookie('logged', $admin['username'], time()+60*60*24*365);
Also, session_start() is present on all pages.
When I want to log off a user, the following happens:
if($page=='logoff') {
setcookie('logged', "", time() - 3600);
unset($_COOKIE['logged']); // tried also this
session_destroy();
$_SESSION=null;
header("Location: index.php"); // if this is removed, the code below acts like there's no $_COOKIE['logged'] or it's empty (until refresh)
}
Once it gets redirected to index.php the $_COOKIE['logged'] is back with the old value, like something would set it again (nothing does for sure, I even removed the one and only login cookie set line)
I couldn't find a solution in similar questions. Tested in chrome and IE.
You can't "unset" a cookie. "Expire" it by setting it to a value in the past:
<?php
// set the expiration date to one hour ago
setcookie("logged", "", time() - 3600);
?>
http://www.w3schools.com/php/php_cookies.asp
I have a problem with the cookies.
I have created a simple cookie and tried to echo it on my server.
setcookie('user', 'John', time() + 4800);
echo $_COOKIE['user'];
I also checked the value using the function var_dump() and the result is NULL.
I do not understand why :-(
Note that you cannot set a cookie in PHP and hope to retrieve the cookie immediately in that same script session. It will be available on the next request.
This doesn't mean that the cookie has not been sent, it just means you can't test it in the same script run.
Of course, before that, make sure that your cookies are turned on.
if(!isset($_COOKIE['user'])) {
setcookie('user', 'John', time() + 4800, '/');
// set the cookie with the fourth parameter with root
// so that its sitewide
} else {
echo $_COOKIE['user'];
}
I have a cookie set like this:
$_COOKIE['admin'] = 'foo';
Now the first time, I can see this cookie getting serialized using var_dump($_COOKIE['admin']) So, I removed that cookie and just placed this instead.
if(isset($_COOKIE['admin']){
echo 'hello admin';
}else{
echo 'hello visitor';
}
Normally this should work for all pages, but it only works once. Meaning, if I browse index page, it works, if I navigate to other page (same website) then comeback to the index page, the cookie gets lost. And there is nothing to destroy/unset any cookie/session in any page.
What could be the problem here
I think you're supposed to set cookie values like this:
setcookie("name","value", $time, "/");
This is covered here in the PHP docs.
To make cookie work in all pages, use like this
$value = 'foo';
setcookie('admin', $value, time() + (60 * 60 * 24));
Now, a cookie named 'admin' with value 'foo' will be available for 1 day. The path parameter is optional. But if you set it to "/", it will be availabel within entire domain.
I have a page (mypage.html) which sets a cookie as follows:
setcookie ("sessionid", md5 (uniqid (rand())));
Now, at the top of an include which displays the site header I have the following:
echo "cookie is ". $_COOKIE['sessionid'];
When I am on mypage.html, which includes the header, the echo command displays the cookie name, as it should...e.g.
cookie is 4d40102ff2d2268d907dd31debc411e2 cookie is 4d40102ff2d2268d907dd31debc411e2
But if I move aeway from the page which set the cookie, all I see is
cookie is
with no name - If I go back to mypage.html it reads it again with no problem. I have no clue how this can happen?? Any ideas?
Set an explicit path for the cookie. The default is the current directory only, so if you navigate to a script in another directory, the cookie won't be sent back by the browser.
// Cookie is valid for all paths ( / ) in the current domain
// This also has an explicit expiry time of 1 hour from the time it's set...
setcookie ("sessionid", md5 (uniqid (rand())), time() + 3600, "/");
It's a little unusual to be setting your own session cookies though, when simply initiating a session handles it for you:
session_start();
// Id is set for you...
echo session_id();