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.
Related
I would like to send two parameters, "Full name" and "CourseID" using session variable from one PHP to another.
Also, I would like to set a cookie variable with my fav programming language and fav operating system.
How can I do that?
Here is my code in first.php [ sending file ]
<?php
$_SESSION['fullname'] = 'Jeyanthi Meenakshisundaram';
$_SESSION['courseid'] = 'IT-2600';
$favlanguage = 'JavaScript';
$favoperatingsystem = 'iOS';
$expire = strtotime('+1 year');
$path = '/';
$lifetime = 60 * 60 * 24 * 14;
session_set_cookie_params( $lifetime, $path);
session_start();
setcookie($favlanguage, $favoperatingsystem, $expire, $path);
?>
****
Here is my second file which gets the value and prints:
*****
<?php
echo "The cookies sent are: <br/>";
print_r($_COOKIE); //output the contents of the cookie array variable
How does session_set_cookie_params work?
I want to ensure all cookies are set with httponly=true, and secure=true.
But instead of adding these arguments to every call to setcookie(), I can
just - before session_start() - set them in session_set_cookie_params()?
And henceforth, every call to setcookie sets those params i each and every cookie?
That would save a lot of tedious work (and surely error-prone).
I would imagine something like this
$cookieParams = session_get_cookie_params();
$cookieParams['httponly'] = true;
$cookieParams['secure'] = true;
session_set_cookie_params($cookieParams);
session_start();
So now, if I do:
setcookie("ABC_user", "", time()+3600);
That cookie has those params in argument 6 and 7 set? Is there a way to check that it works? Or is there an even better way to accomplish this?
This simple code will give you what you want.
function set_cookie($name,$content,$time){
$http_only = true;
$secure = true;
$path = "/";
$domain = ".example.com"; // Include All Subdomains
setcookie($name,$content,$time,$path,$domain,$secure,$http_only);
}
set_cookie("ABC_user", "", time() + 3600);
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
}
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";
I never worked with cookies before so I'm probably messing up really hard.
Thing is. I set a cookie and then try to echo it for testing. If I inspect my browser, it shows me the cookie is there, but the cookie being fetched is always one cookie previous to the one last created. Here is what I'm doing:
function set_cookie_test($user_id)
{
$this->load->helper('cookie');
$this->load->module('site_security');
$this->load->module('site_settings');
$now_time = time();
$one_day = 86400;
$one_week = $one_day * 7;
$one_week_ahead = $now_time + $one_week;
$data['cookie_code'] = $this->site_security->generate_random_string(128); // the cookie_code on the table is 128 chars long
$data['user_id'] = $user_id;
$data['expiry_date'] = $one_week_ahead;
$this->_insert($data);
$cookie_name = $this->site_settings->_get_cookie_name();
set_cookie($cookie_name, $value = $data['cookie_code'], $expire = $data['expiry_date'], $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE);
$cookieVal = get_cookie($cookie_name);
echo $cookieVal; die();
}
This is what happens after I log in (when I call this function)
As you can see, the cookie in the browser has a value of 8XvaQZjWX7... This is the current cookie, it matches my DB.
The previous cookie value in my db is the one being echoed out. I need to figure this out for the following reason:
Whenever I use sessions for the login, I set userdata so I can retrieve the user's info to display his name and avatar on the screen, for example.
I need to be able to do the same when I use cookies...
Any help is very much appreciated.