Cookies will not set - php

setcookie("test","sonali",time()+3600,"../php/"," ",false,false);
echo $_COOKIE["test"];
Notice:Undefined index:test in
D:\projects\Trainee2014\Sonali\php\cookiedemo.php on line 3
if possible please give me solution

<?php
setcookie("test","sonali",time()+3600,"../php/"," ",false,false);
$check = $_COOKIE["test"];
if (isset($check)){
echo 'Cookie not set';
}
else{
echo 'Cookie set';
}
?>
Try the above code to handle the notice.

Change your code to this and try,
setcookie("test","sonali",time()+3600,"../php/"," ",false,false);
echo isset($_COOKIE["test"]) ? $_COOKIE["test"] : '';
Problem is at the first time $_COO KIE["test"] is not set. so you need to refresh. in order to avoid this problem you should simply use the if condition with isset().

Try like this:
Syntax setcookie(name,value,expire,path,domain,secure);
Use like this in page :
<?php
$value = "cookie value";
// send a simple cookie
setcookie("user",$value);
OR
// send a cookie that expires in 24 hours
setcookie("user",$value, time()+3600*24);
?>
Access it on page like this :
<?php
echo $_COOKIE["user"];
OR
echo $HTTP_COOKIE_VARS["user"];
// Print all cookies
print_r($_COOKIE);
?>

Try this :
setcookie("test","sonali",time()+3600,"../php/",false,false);
print_r($_COOKIE);
echo $_COOKIE["test"];

Please set setcookes function only 5 parameter
setcookie(name, value, expire, path, domain);

The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/php/', the cookie will only be available within the /php/ directory and all sub-directories such as /php/../ of domain. The default value is the current directory that the cookie is being set in.
Ref: http://php.net/setcookie
setcookie("test","sonali",time()+3600,"/"," ",false,false);
echo $_COOKIE["test"];

Related

Cant create cookies when website is online

when i try to create a cookie with PHP they are not saved when the website is online.
When i'm on the localhost, its all okay.
For example:
setcookie('user_register', 'username', time() + 3600, '/');
How can i troubleshoot this? Is there any config in cpanel to disable cookie creation ?
Thankyou
You say it works on localhost, which alerts me to think that you have placed the setcookie below output like this for example:
<?php
// top of file
Echo "saving cookie";
setcookie('user', 'username', 3600, '/');
?>
This may work on localhost but will always fail on a real php server.
Setcookie is a header and must be placed before any output as stated in the manual http://php.net/manual/en/function.setcookie.php
To make the code above work you need to chaange place of the echo and setcookie so that it first sets the cookie and then outputs the text:
<?php
// top of file
setcookie('user', 'username', 3600, '/');
Echo "saving cookie";
?>
Then to read the cookie you need to go to a new page or refresh the page.
Cookies can't be set and read at the same time in php.
So to complete the code we can do like this:
<?php
// top of file
If($_COOKIE(['user']){
Echo $_COOKIE(['user']);
}Else{
setcookie('user', 'username', 3600, '/');
Echo "saving cookie";
}
?>
Now you can set the cookie and read it on the same page if you refresh the page.
Cookies are used next time you visit the page. So there are 3 general solutions: 1. Save it to cookie and for first time, just echo POST variables instead of COOKIE. Code will look like this:
setcookie('user', $_POST['user'], time()+3600); // notice time() function
setcookie('pass', $_POST['pass'], time()+3600); // you cant use absolute value here
if (!isset($_COOKIE['user'])) echo $_POST['user'] . ' ' . $_POST['pass'];
else echo $_COOKIE['user'] . ' ' . $_COOKIE['pass'];
, but if you really want to store password in cookie (very bad idea), then at least hash it. Code with hashing could look like this:
setcookie('pass', hash(whirlpool/*algorithm*/, $_POST['pass']/*text*/), time()+3600);
, then when you check the password, just hash it and compare hashes.
You cannot send and receive cookie in the same request. Here is a modification you should try
if(isset($_POST['user']) && isset($_POST['pass'])){
setcookie("user",$_POST['user'],3600);
setcookie("pass",$_POST['pass'],3600);
header("location:".$_SERVER['PHP_SELF']);
}
if(isset($_COOKIE['user']) && isset($_COOKIE['user']))
{
echo $_COOKIE["user"] . " " . $_COOKIE["pass"]; // This is line 28
}
However, it advisable to use sessions instead. Start by going through PHP_SESSION

can not retrieve a cookie value from echo $_COOKIE['name'] in php

setcookie(name, value, expire, path, domain, secure, httponly);
When I try to echo $_COOKIE['name'], it print a blank
This should work for you:
<?php
if(!isset($_COOKIE['action'])) {
$cookie_value = "menuopen";
setcookie("action", $cookie_value, time()+3600, "/", ".acvd.com");
}
if(!empty($_COOKIE['action']))
echo $_COOKIE['action'];
?>
You have to look that your using the right name of the cookie also that acvd.com is your domain! And you can't have a secure connection but httponly!
<?php
$cookie_value = "menuopen";
if(!empty($cookie_value))
{
setcookie("action", $cookie_value, time()+3600, "/", "acvd.com", 1, 1); ?>
}
?>
I hope it will work for you.
first Check the cookie name you have used during setcookie method
You can also check your cookie value in browser following are the steps:
1 open debugger tool
2 go to resource tab
3 then go to cookies
4 then go to your domain name
there you will find the cookie name with corresponding value, check for your name is it there and what is the name whether it is name or action.
if name then echo $_COOKIE['name']
if action then echo $_COOKIE['action']

strange result when trying to increment a n php cookie

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.

php session problem

I think this code should echo "first" in first usage and after refresh it should echo timestamp,but it will show first every time,where is my problem?
I set cookie permition on always.
if (isset($_SESSION['TestSession']))
{
echo ($_SESSION['TestSession']);
}
else
{
echo "first";
$_SESSION['TestSession'] = time();
}
Have you placed
session_start();
at the top of your page?
To start a session, first session_start() should be called. Otherwise $_SESSION won't be set, and you will initialize it every time.

Php sessions aren't working

I am new to php, and very new to sessions, so I have no idea what I am doing wrong. I followed the tutorial on tizag, and put this code on my site:
<?php
session_start();
echo SID . "<br><br>";
if(isset($_SESSION['views'])) {
$_SESSION['views'] = $_SESSION['views'] + 1;
} else {
$_SESSION['views'] = 1;
echo "views = ". $_SESSION['views'];
}
?>
The SID changes whenever I refresh, and the number does not count up.
Update: Url: http://121.73.150.105/PIA/
FIXED BY: Putting session_start() before my doctype, title etc.
Are cookies enabled in you're browser ? phpsessid is stored as a cookie , you can set different parameters for it , one that could be usefull in you're case could be session_get_cookie_params() , and see if everithing is oki with the session cookie params .
If anything is wrong like expiration date you can set the params with session_set_cookie_params()
Your PHP setup may have been configured to not save sessions in cookies.
To verify if this is the case, you can take a look at session.use_cookies in your php.ini, or using ini_get, like so:
<?php echo ini_get('session.use-cookies'); ?>
You can correct it at runtime, as well, using ini_set, like so:
<?php ini_set('session.use-cookies', '1'); ?>
either you are outputting something to the browser before calling session start, or you have cookies disabled.
You don't output the $_SESSION['view'] after the if statement. I think that's why it doesn't change.
Try:
<?php
session_start();
echo SID . "<br><br>";
if(isset($_SESSION['views'])) {
$_SESSION['views'] = $_SESSION['views'] + 1;
} else {
$_SESSION['views'] = 1;
}
echo "views = ". $_SESSION['views'];
?>
So you always output the new $_SESSION['views'] value.
EDIT:
I think the right answer is that the session is not set. But I'm curious, how can the code always outputs "view = 1"? Can I open a new question referencing this question or just discuss it here?
in your code if you cant see session ID you can write
session_id() in place of SID.
ini_set("session.use_cookies",1);
ini_set("session.use_only_cookies",1);
this two parameter must set to gether if you want it work

Categories