Im trying test the cookie that are set in my browser, earlier cookieid was given inside the url(mydomain.com?cookieid=1234). now im trying to set a cookie by calling the script which sets it. when i load the script known as proceed.php, i want that to go on and set the cookie as i have written. but it does not set the cookie as i intend
proceed.php
<?php
$cookieid = 1234;
include('bin/setcookie.php');
?>
setcookie.php
<?php
include_once dirname(__FILE__).'/../Lodread.php';
$cookieid = isset($_REQUEST["cookieid"]) ? floatval($_REQUEST["cookieid"]) : 0;
$project_id = isset($_REQUEST["projectid"]) ? intval($_REQUEST["projectid"]) : 0;
if (isset($_SERVER['HTTP_REFERER'])){
$urlParts=parse_url($_SERVER['HTTP_REFERER']);
if (isset($urlParts['query'])){
$vars = parse_str($urlParts['query']);
if (isset($vars['cookieid']) && floatval($vars['cookieid']) > 0 ){
$cookieid = floatval($vars['cookieid']);
}
}
}
if ($cookieid) {
if ( ! isset($_COOKIE["cid"])){
$cid = ($project_id?"$project_id:$cookieid":$cookieid);
setcookie("cid", $cid, time() + 60 * 60 * 24 * 365, "/", ".mydomain.com");
header('Location: '.Config_Reader::readProjectConfig('Cookie')->base_url.'/set2.php');
}
}
?>
This is sample, try like this.
How to Get the Contents of a Cookie:
Cookies set for a page can be retrieved from the variable $_COOKIE['cookie_name'] where 'cookie_name' is the name of the cookie you set earlier.
For example, if you wanted to display the value of the "userlogin" cookie, the following code should do the trick.
echo "Welcome back to the site" . $_COOKIE['userlogin'] ;
Note that you cannot set a cookie in PHP and hope to retrieve the cookie immediately in that same script session. Take the following non-working PHP code as an example:
/* WARNING: THIS WILL NOT WORK */
setcookie ( "userlogin", "anonymous", time()+60 );
echo "Value of userlogin: " . $_COOKIE['userlogin'] ;
Set Cookie without "mydomain.com".
use this:
setcookie("cid", $cid, time() + 60 * 60 * 24 * 365, "/");
now you check cookie set.
Try it without specifying the domain when using the setcookie function. Also, I would recommend passing $cid directly to your header function since the cookie value can't be retrieved within the same php script right away. Then finally you can see if the cookie is set for future use by using $_COOKIE['cookie_name'] in a separate php script to get the cookie's value.
Related
I know this has been asked many times before, and I've read and tried every suggestion I've found.
I set a cookie in a separate file with the following code:
<?php
session_start();
$q = $_SESSION['qty'];
setcookie ("quantity", $q, time() + (86400 * 30), "/");
$_COOKIE['quantity'] = $qty;
if(isset($_COOKIE['quantity'])) {
print_r("set");
}
else print_r("not set");
?>
It print's "set" every time, indicating to me it has been set. However, in a different php page I test for the cookie with the following code...
if(!isset($_COOKIE["quantity"])) {
include("functions/setQty.php");
}
...and it always takes me to the setQty page, indicating to me that it isn't set (which other issues verify that it's not). What am I missing?
I created this Cookie alert bar for my site, it works as intented. But you need to click the close link twice to close down the warning for some reason that I cannot figure out.
I use this following function to check if cookie exists or not.
function checkIfCookieExist($cookieName) {
if(isset($_COOKIE[$cookieName])) {
return true;
}
else {
return false;
}
}
if cookie does not exist, and the cookie get parameter exists and equals 1 I create a cookie
if (!checkIfCookieExist('cookieConfirmation') && isset($_GET['cookie']) && $_GET['cookie'] == 1) {
$cookieName = 'cookieConfirmation';
$cookieValue = 'Cookie confirmation';
$cookieDuration = 86400 * 30 * 3;
setcookie($cookieName, $cookieValue, time() + $cookieDuration);
}
Prints out the cookie bar, links to index with a get parameter for the cookie
function renderCookieBar() {
echo('
<div id="cookieBar">
<p>blabla cookie, cookies on this site yo</p>
I understand, close this box!
</div>
');
}
Calls function to print out cookiebar at given place in my html code
if(!checkIfCookieExist('cookieConfirmation')) {
renderCookieBar();
}
I appreciate any answers or help,
Cheers!
When you set the cookie in the header, the cookie is not directly present; means: the cookie is available up on the next page hit.
Check the manual: http://php.net/set_cookie
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array. Cookie values may also exist in $_REQUEST.
You can either
1. Set the cookie and immediately reload the page
Set the cookie and force a browser refresah by using header("Refresh:0");.
if (!checkIfCookieExist('cookieConfirmation') && isset($_GET['cookie']) && $_GET['cookie'] == 1) {
$cookieName = 'cookieConfirmation';
$cookieValue = 'Cookie confirmation';
$cookieDuration = 86400 * 30 * 3;
setcookie($cookieName, $cookieValue, time() + $cookieDuration);
header("Refresh:0");
}
2. Use Javascript
When setting the cookie with JavaScript it is directly available from the browser. You may also rewrite your script, so the JavaScript sets the cookie and removes the notification bar.
There are many solutions (also here on SO) how to work with cookies in JavaScript easily. If you are using a JavaScript library like jQuery you also have plugins handling the cookies.
I have tried to find a solution here, and worked out some issues with Cookies (such as the problem with setting the cookie before the HTML/Header is created). I have checked my browser, and the cookie is set.
However, when I use code like the following, the value is not showing:
echo "cookie name: " . $cookie_name . "<br>";
if( isset( $_COOKIE[$cookie_name] ) )
{
echo "Cookie set: " . $_COOKIE[$cookie_name] . "<br>";
The value of the variable $cookie_name is coming from a configuration file that is included, and is returning the correct value. The problem is that isset() is not seeing the cookie. This is probably a very simple issue, but I'm not seeing it. Thanks in advance -- Ken
Per request, adding code to question for readability:
The two variables being used are set to this (at least for now):
// for login cookies
$cookie_name = 'GSPCMS_Id';
$cookie_time = (3600 * 24 * 10); // 10 days
The command that sets the cookie (in the login routine) is:
setcookie( $cookie_name, $_SESSION['user_name'], time()+$cookie_time );
Hi guys i set a cookie using my script known as cookieset.php
setcookie("atid", $atid, time() + 60 * 60 * 24 * 365, "/", ".mydomain.com");
and it is shown in the browser
Name atid
Content 1234
but when i try to retrieve it like this from another script
echo 'value is: ' . $_COOKIE['atid'];
it gives the error saying
undefnied index: atid in.........
can anybody help me over this
setcookie("atid",$atid,time()+315360,"/");
// use
if (isset($_COOKIE['atid'])) {
echo "cookeies set ";
} else {
echo "cookeies not set ";
}
use mozila firebug / cookies to see cookies file
The $_COOKIE array is populated with information sent from the browser. The first time you request the file that calls setcookie - cookieset.php - the server sends the cookie to the browser, but at this time the $_COOKIE array was already populated without the cookie you just set.
The cookie will be available in subsequent requests until it expires.
To see the cookie in PHP, just do like this.
if (isset($_COOKIE['atid'])) {
echo 'Cookie found with value ' . $_COOKIE['atid'];
} else {
setcookie('atid', $atid, time() + 60 * 60 * 24 * 365, "/", ".mydomain.com");
echo 'Cookie was set. Please refresh to see it working';
}
The problem (from the error), seems to be that $_COOKIE['atid'] (depending on what line the error is at) is undefined - that means it has not been set, and seeing that you actually set the cookie, I am saying that its the atid that is undefined. Make sure you are getting it, check with isset()
Try this :
if (isset($_COOKIE['atid'])) {
echo $_COOKIE['atid'];
} else {
echo "No cookie Set";
}
And one more point :
it won't be available until the next page load or by doing the page request again.
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.
can someone please how what hell this
cookie stops at 2 ?
<?php
if (isset($_COOKIE["count"]))
{
$cookie = ++$_COOKIE['count'];
}
else {
echo "Welcome guest!<br>";
setcookie("count", 1, time()+3600);
}
ECHO $cookie;
?>
thank you all
$cookie = ++$_COOKIE['count']; is only called once. If $_COOKIE[count] has a numerical value, $cookie will store that value plus 1.
Also, the following is not strictly correct:
echo "Welcome guest!<br>";
setcookie("count", 1, time()+3600);
You cannot call an echo before a header. I recommend you change it to this:
setcookie("count", 1, time()+3600);
echo "Welcome guest!<br>";
You cannot change cookie value by incrementing $_COOKIE[xxx], you have to use setcookie() function for that. This will work:
<?php
$cookie = isset($_COOKIE["count"]) ? $_COOKIE["count"] : 0;
setcookie('count', $cookie + 1, time()+3600);
ECHO $cookie;
You cannot update a cookie that way. However you can overwrite it.
See setcookie for more info.
If you set a cookie, it won't retrieved until the next request and so the data won't be present in $_COOKIE.
So setting a cookie and accessing it cannot be in same instance. You need to redirect or refresh after setting it.
Simply use setcookie() to increment.
to increment you place the ++ after the string, not before it.