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..
Related
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 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.
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 am using these below code in a.php
$cookieId = rand(100000,999999);
setcookie('senderSession', $cookieId);
echo $_COOKIE['senderSession'];
I am using these below code in b.php
$cookieId = rand(100000,999999);
setcookie('travelerSession', $cookieId);
echo $_COOKIE['travelerSession'];
But both are giving blank. Both files are using in a project.
I do not know what question is, but I have an idea.
PHP Create/Retrieve a Cookie
The following example creates a cookie named "user" with the value "A.Kushwaha". The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer).
We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). We also use the isset() function to find out if the cookie is set:
<?php
$cookie_name = "user";
$cookie_value = "A.Kushwaha";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");// 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
I am trying to set a cookie and redirect. Using Debian GNU/Linux 6.0 (64 bit) with PHP 5.3.3-7+squeeze19 with Suhosin-Patch (cli) (built: Feb 17 2014 10:10:23) and Apache/2.2.16 (Debian).
For some reason this works:
<?php
$cookie_name = $_GET['a'];
$cookie_value = $_GET['b'];
setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
?>
But this does not:
<?php
$cookie_name = $_GET['a'];
$cookie_value = $_GET['b'];
setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
header("Location: http://www.example.com");
exit;
?>
Even after several page loads. I've tried adding error reporting to the top of my code, but I don't see any errors when I load the page nor in the Apache log (/var/log/apache2/error.log):
error_reporting(E_ALL);ini_set('display_errors','1');
For some reason whenever I redirect, even using javascript as below, a cookie will not add.
<?php
$cookie_name = $_GET['a'];
$cookie_value = $_GET['b'];
setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=http://www.example.com">
<script type="text/javascript">
window.location.href = "http://www.example.com"
</script>
<title>Page Redirection</title>
</head>
<body>
If you are not redirected, follow <a href='http://www.example.com'>this link</a>!
</body>
</html>
Why does the first example work but not the others?
Use include rather than redirect
This also saves the Browser a round trip HTTP request
<?php
$cookie_name = $_GET['a'];
$cookie_value = $_GET['b'];
setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
include('/home/user/public_html/index.html');
exit;
?>
While I prefer include over a redirect header, your cookie should work. I have tested and it works just like it should.
In my test I redirected to another domain. The cookie is set in the domain where the PHP script resides.
setcookie('test', 'test', time() + (86400 * 30), "/");
header("Location: http://www.intel.com");
Works just like it is supposed to:
I was also getting this weirdness but with js redirect. Testing with chrome browser on xp.
The way I solved it was to do the cookie setting with injected js using document.cookie =
?>
<script type="text/javascript">
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
setCookie("foo","<?php echo $bar; ?>",30);
window.location = "<?php echo $destination_page; ?>.php";
</script>
<?php
then the problem went away.
It felt like the redirect was causing the php setcookie to fail for some reason...