Reputation: 58
after a lot of research i coulndt find a way for getting my dream come true. Im currently making a cloud hosted on m< "localhost" (will get online someday) and implemented only one Cookie for remember Users after logged in. I need a way to logout through button for my users but theres the problem i couldnt find any way that fits my "case". I already tried to change the date of the cookie and a lot of methods where they delete the Cookie but it never worked, it could be the problem that a Session Cookie dont have a expiration date. The Cookie name is always "PHPSESSID" it will get created after logged in:
<?php
session_start();
require("db.php");
if(isset($_SESSION['code'])){
$code = $_SESSION['code'];
$saftycode = $db_link->real_escape_string($code);
$check = 0;
$db_res = mysqli_query($db_link, "SELECT * FROM user WHERE code = '$saftycode'");
while($row = mysqli_fetch_array($db_res)){
$check = 1;
}
if($check == 1){
header('Location: home.php');
}
}
?>
<html>
...
</html>
<?php
if(isset($_POST['username']) || isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($username){
if($password){
require("db.php");
$saftyusername = $db_link->real_escape_string($username);
$saftypassword = $db_link->real_escape_string($password);
$code = null;
$db_res = mysqli_query($db_link, "SELECT * FROM user WHERE username = '$saftyusername' AND password = '$saftypassword'");
while($row = mysqli_fetch_array($db_res)){
$code = $row['code'];
}
if($code){
$_SESSION['code'] = $code;
$_SESSION['ERROR'] = "";
header('Location:home.php');
}else{
$_SESSION['code'] = $code;
$_SESSION['ERROR'] = "";
header('Location:index.php');
}
}else{
$_SESSION['ERROR'] = "";
header('Location: index.php');
}
}else{
$_SESSION['ERROR'] = "";
header('Location: index.php');
}
}
there is no session_destroy() implemented in the entire script. I actually dont know if its possible to delete a PHPSESSID cookie inside javascript, if this isnt possible i would be okay with using php for delete the cookie. Is there anyway to delete a PHPSESSID Cookie through script?, can i delete all cookies at once? these are the questions i have for you guys
the Cookie is saved under localhost>Cookies>PHPSESSID
maybe this question is a duplicate, but i need to know how to logout users
Upvotes: 1
Views: 1222
Reputation: 17566
To delete a cookie you have only set the expiry date to date before (yesterday for example)
setcookie("sessioncookie", "", time()-3600);
Upvotes: 0
Reputation: 3116
You're correct in saying there is no way to do this in PHP, what you can do instead if you want to remove a cookie is to set the expiry to be at a time in the past:
// Set to 1 second in the past, this will invalidate the cookie.
setcookie("cookie_name", "", time() - 1, "/");
It is also a good idea to unset the cookie index within the $_COOKIE
global since it can exist in there as the rest of the page is parsed.
unset($_COOKIE["cookie_name"])
Upvotes: 3