When i close windows tab, session was dead!
how can i stop that ?
i use this :
session_start(['cookie_lifetime' => 86400,]);
but when user close tab or move to another page session was dead !
example :
i'm in page => "home"
when i try to go this url "example.com/users"
the session was dead.
• please note this , this problem is just in my website, i can use that ( users page ) in "localhost". but i never can't close browser ( in both (localhost/website) ,
i guess if session will alive for long time ( example 1 day ) , the problem could solved.
thanks.
a session stays active as long as the browser is active, when it closes the session closes as well. If you want to stop this from happening i recommend you to create a cookie instead. Read all about it here
cookie example from W3schools on how to create a cookie:
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
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];
}
?>
</body>
</html>
AND the way you are trying to use a session is wrong!
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 know that by using $_SESSION, one can store values over time. The default time is 1440 seconds = 24 minutes. I would like my values to be stored: for a longer time/until the browser has been closed.
Let's say I want to store the a boolean value and string value. Is session the best way?
For example: $_SESSION["value"] = true; and $_SESSION["value2"] = "my_string";?
Is session the best way, or are there any other good/better solutions? The values has to be available for all the pages (.php) on my website.
You can use cookies to store data for longer period of time.
so using cookies would be like
Set Cookie
<?php
$cookie_name = "value2";
$cookie_value = "my_string";
//Cookie to hold true false can set using 0 or 1 like
//setcookie('value', '0'); 0 for false, 1 for true
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
Retrieve Cookie value
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!";
echo "Cookie Value is: " . $_COOKIE[$cookie_name];
}
?>
Setting and retrieving cookie for boolean
<?php
$CookieVar = true;
// setting the cookie
setcookie('myCookie', $CookieVar ? '1' : '0'); //set '1' if true else set '0'
if (isset($_COOKIE['myCookie']) AND $_COOKIE['myCookie'] === '1') {
echo 'true';
} else {
echo 'false';
}
?>
Screenshot
You can use the local storage for saving the data and for using it from any page on the website.
Using Javascript
window.localStorage.setItem(key, value);
This key can be removed using
window.localStorage.removeItem(key, value);
You can get the key value using
window.localStorage.getItem(key);
I've set session.cookie_secure to 1/true in php.ini and running the following code behind apache or php-fpm+nginx server --
<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
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];
}
?>
</body>
</html>
After restarting php-fpm/apache, the 'secure' attribute does not appear in the Set-Cookie header. Checked using wget and firefox (developer tools > toggle tools > network)
echo session_get_cookie_params()[secure];
returns 1.
Forcing HTTPS CGI parameter to on/off doesn't make a difference. This change was verified using --
echo $_SERVER['HTTPS'];
This give the same result in RHEL/CentOS/EL 6 (PHP 5.3.3), RHEL/CentOS/EL 7 (PHP 5.4) and Gentoo (PHP 5.6.29)
session.cookie_secure configures the secure flag only for cookies sent by PHP's session extension; i.e. Set-Cookie headers triggered by session_start(), session_regenerate_id() calls.
It does not affect setcookie() or setrawcookie() (nor anything else in PHP that may send cookies to the client) - these functions have their own $secure parameter for that purpose.
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];
}
?>
My login does not seem to be creating a cookie. The form gets all the way to the cookie creation portion of the script and even echos that a cookie was made but it does not actually create one.
Here is the cookie portion of my code:
if (!$error) {
if (isset($_POST['rememberme'])) {
setcookie('guruemail', $loginemail, time() + 86400 * 365, '/', NULL);
setcookie('gurupassword', md5($loginpassword), time() + 86400 * 365, '/', NULL);
echo "Long-term cookie made";
} else {
setcookie('guruemail', $loginemail, false, '/', NULL);
setcookie('gurupassword', md5($loginpassword), false, '/', NULL);
echo "Short-term cookie made";
}
}
The login can be visited at http://protein.guru/signin.phtml
The cookie test can be viewed at: http://protein.guru/testcookie.php
Here is the cookietest code:
<?php
echo "Value is: " . $_COOKIE[$guruemail];
echo "Value is: " . $_COOKIE[$gurupassword];
?>
For the sign-in:
I am using the email: tester3651#outlook.com
Password is: meatloaf
Note:Possible newbie mistake? -- I do not have a session_start(); anywhere in either code. Not sure if I would need that for a straight cookie login.
Any feedback would be appreciated. Thanks everyone.
As mentioned in the comments:
Access the $_COOKIE arrays with strings, instead of a variables.
<?php
echo "Value is: " . $_COOKIE['guruemail'];
echo "Value is: " . $_COOKIE['gurupassword'];
?>
You'll need quotes around the cookie variable
<?php
echo "Value is: " . $_COOKIE['guruemail'];
echo "Value is: " . $_COOKIE['gurupassword'];
?>
Actually it would be much more secure to use $_SESSION instead for users login as users can manually set $_COOKIE.
More details at the following answer: Making login more secure