Setting cookies with php - php

I have enabled cookies in my browser, but when I run the following PHP, no cookies are being set. The print_r only gives me an empty array.
<?php
echo "My SSL Browser Cookie is set!";
$name = "SSLCookie";
$value = "1508 - Day 3";
$expire = time() + (60*60*24*7);
setcookie($name, $value, $expire);
?>
<pre>
<?php
print_r($_COOKIE);
?>
</pre>

worked for me
Array
(
[_ga] => GA1.1.1962982090.1494253367
[_gid] => GA1.1.1398313405.1494636302
)
i set cookies something like this
$cookie_name = "user";
$cookie_value = "Bob";
$hostname = "localhost";
setcookie('$cookie_name', $cookie_value, time() + 2678400, '/', $hostname, isset($_SERVER["HTTP"]) , true);
hope it helps

You cannot set a cookie after an echo.
Move your echo down after setcookie.

Related

setcookie is not setting value in browser in PHP

I try to set cookie using below code in PHP but cookie is not displaying when I check using print_r($_COOKIE);
$str_zipcode = "20304";
setcookie("zipcode", $str_zipcode, 2147483647);
Then I try to set cookie using below code in PHP and cookie is displaying when I check using print_r($_COOKIE); But when I refresh browser or close and reopen browser and check cookie value using print_r($_COOKIE); then cookie not displaying and that means. Not sure what is wrong I am doing in this code.
$str_zipcode = "20304";
$_COOKIE['zipcode'] = $str_zipcode;
Below are my laptop configuration.
PHP 7
Ubuntu 16.0
Mozilla Firefox 80.0.1
EDIT #1
I checked in mobile's chrome browser too and it's behaving same as I mentioned above for my laptop.
the last parameter for cookie is its expiration date :
eg:
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
}

How to store $_POST data to $_COOKIE in moodle php

I was using the moodle and was not able to get the data of $_POST to $_COOKIE please help me out my code is given below.
<?php
$something = $_POST['UserType'];
$cookie_name = $something;
setcookie(cookie_name, $cookie_name, time() + (86400 * 30), "/");
?>
Than on another page I did something like this stated below.
<?php
$hello = $_COOKIE['cookie_name'];
echo $hello;
?>
But I didn't received any cookie stored in the browser's cookie.
<?php
$something = $_POST['UserType'];
$cookie_name = $something;
setcookie($cookie_name, $cookie_name, time() + (86400 * 30), "/");
?>
I have done this and strangely it worked.. Thank you all..

How To Get Cookie's Name In PHP?

I want to get cookie's name in PHP. Not cookie's value! My code like below:
<?php
session_start();
ob_start();
$user_id = $_GET["user_id"];
$timing_clock = "";
if(isset($_COOKIE["timing_type"])){
// cookie's value
$timing_clock = setcookie("timing_type");
// how to get cookie's name?
} else {
echo("0");
}
?>
How can i do that? I want to set cookie's name to a variable. Because cookie's name are very important for me.
it will help you, use var_dump($_COOKIE) and get all COOKIE name.
$cookie_name = "user";
$cookie_value = "Dave";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
if(isset($_COOKIE)) {
var_dump($_COOKIE);
foreach($_COOKIE as $key => $val)
{
echo "cookie name = ".$key.", and value = ".$val;
}
}
This will print all with respective name
<pre>
<?php print_r($_COOKIE); ?>
</pre>
In this example
setcookie("timing_type");
the cookie name would be $_COOKIE['timing_type']
When you use the setcookie function you are applying the cookie name(key).
All cookies names are stored in $_COOKIE as "Name" => "value".
Simply output the keys of the $_COOKIE array and you'll have your names :)
Simply read $_COOKIE["name"] to get the name set by setcookie
The first paramater of setcookie() is the name of the cookie
<?php
session_start();
ob_start();
$user_id = $_GET["user_id"];
$timing_clock = "";
if(isset($_COOKIE["timing_type"])){
// cookie's value
$timing_clock = setcookie("timing_type");
// how to get cookie's name?
$cookie_name = $_COOKIE['timing_type'];
} else {
echo("0");
}
?>
$_COOKIE["name"] = "JASON STATHAM";

PHP cookie value not being passed from one page to another

This is in a page called headersessioncookie.php
<?php
session_start();
if ( ! isset ( $_SESSION['loggedin'] ) ) {
$_SESSION['loggedin'] = FALSE;
}
$expiry = time()+60*60*9000;
setcookie('cookie[loggedin]', '', $expiry, "", "", "", TRUE);
if ( ! isset ( $_COOKIE['cookie[loggedin]'] ) ) {
$_COOKIE['cookie[loggedin]'] = FALSE;
}
?>
This is in a page called test.php
<?php
require_once('headersessioncookie.php'); //start session and cookie
$_SESSION['loggedin'] = TRUE;
$_COOKIE['cookie[loggedin]'] = TRUE;
?>
When I run test.php and then run this page below called test1.php ...
<?php
require_once('headersessioncookie.php'); //start session and cookie
echo "sessionvalue" . $_SESSION['loggedin'] . '<br>';
echo "cookievalue" . $_COOKIE['cookie[loggedin]'] . '<br>';
?>
... I get
sessionvalue1
cookievalue
Why don't I get...
sessionvalue1
cookievalue1
...??
The superglobal variable $_COOKIE only contains the cookie values. If you modify this value won't affect to the cookie because you need to sent the headers to the browser to do so.
If you need to modify it you have to use the method setCookie because this will sent the headers with the new value.
Note Remember that the $_COOKIE only will be updated after use setCookie when you refresh the page.
So this should work:
File: headersessioncookie.php
<?php
//Session
session_start();
if ( !isset($_SESSION['loggedin']) )
$_SESSION['loggedin'] = FALSE;
//Cookie
$expiry = time()+60*60*9000;
if ( !isset($_COOKIE['cookieloggedin']) )
setcookie('cookieloggedin', '', $expiry, "", "", true);
?>
File: test.php
<?php
require_once('headersessioncookie.php'); //start session and cookie
$_SESSION['loggedin'] = TRUE;
setcookie('cookieloggedin', '1', $expiry, "", "", true);
?>
File: test1.php
<?php
require_once('headersessioncookie.php'); //start session and cookie
echo "sessionvalue" . $_SESSION['loggedin'] . '<br>';
echo "cookievalue" . $_COOKIE['cookieloggedin'] . '<br>';
?>
Please notice also:
-How to update a cookie: https://stackoverflow.com/a/6487597/3933332
-Is a Cookie Case Sensitive: https://stackoverflow.com/a/11312272/3933332
Answering my own question. Turns out there were 3 major problems with my code.
1) I was trying to set the cookie value by doing this:
$_COOKIE['cookie[loggedin]'] = FALSE;
Turns out one needs to use setcookie() to set the cookie value. Assigning a new value to $_COOKIE will change the value of that variable (within the scope of the same page), but it won't change the value inside the cookie (outside the scope of that page, calling $_COOKIE will yield the value stored in the cookie).
2) The following is incorrect
echo "cookievalue" . $_COOKIE['cookie[loggedin]'] . '<br>';
Instead it should be
echo "cookievalue" . $_COOKIE['cookie']['loggedin'] . '<br>';
3) Cookie necessarily has to be passed a string value. I was trying to pass a value = FALSE which is not a string. Instead, I could have correctly passed a value = 'FALSE'

Not able to get cookie value on other page in magento

Hi I have set a cookie in Magento as:
$cookie_value = $_GET["utm_source"];
$cookie = Mage::getSingleton('core/cookie');
$name = "Pixel_Track";
$url = "stage.test.com";
$expiry = time() + 86400 * 365 * 1;
$cookie->set($name, $cookie_value ,$url,$expiry);
Now I want to get on another page and I am using:
$cookie = Mage::getSingleton('core/cookie')->get($name);
Where I am doing wrong? Because print_r is not giving the cookie name.
Mage_Core_Model_Cookie class contains functions to set, get and delete cookie. so try:
$cookie_value = $_GET["utm_source"];
$cookie = Mage::getModel('core/cookie');
...
$cookie->set($name, $cookie_value, $period ,$url,$expiry);
and
$cookie = Mage::getModel('core/cookie')->get($name);
I just got the solution by defining the path attribute of cookie.
$cookie->set($name, $cookie_value ,time()+86400,'/');
Try this:
echo $cookie = Mage::getModel('core/cookie')->get("Pixel_Track");
//your can't get your variables in another page, so please type cookie name.

Categories