Delete PHP Cookie?

I currently have a set of cookies as follows:

setcookie("username",$username,time()+3600*24*5); 

How can I clear the value of this cookie so that the username is no longer populated?

I cleared as follows:

 setcookie("username","",time()-60000); 

User username appears anyway.

HTML form:

 <?php session_start(); $username = NULL; $password = NULL; if(isset($_SESSION['username'])){ $username = $_COOKIE['username']; $password = $_COOKIE['password']; } ?> <html> <title>Login</title> <body bgcolor='#000000'> <font color="white"> <H2><div align='center'>Login</div></H2> <form align='center' action='login.php' method='POST'> Username: <input type='text' name='username' value='<?$_COOKIE['username']?>'><br \> Password: <input type='password' name='password' value='<?$password?>'><br \> Remember Me <input type='checkbox' name='remember' value='rememberme'><br \> <input type='submit' value='Login'> </form> </font> </body> </html> 

PHP script to process the form:

 <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; //Hash password in a new variable $password2 = md5($password); require_once "/home/a7435766/public_html/scripts/dbconnect.php"; $query = mysql_query("SELECT * FROM userstwo WHERE username = '$username' && password = '$password2'"); if((mysql_num_rows($query)) != 0) { //Store username and password in a cookie if($_POST['remember'] == 'rememberme') { setcookie("username",$username,time()+3600*24*5,'','.ohjustthatguy.com'); setcookie("password",$password,time()+3600*24*2,'','.ohjustthatguy.com'); } else { setcookie("username","",time()-10,'','.ohjustthatguy.com'); setcookie("password","",time()-10,'','.ohjustthatguy.com'); } $_SESSION['username'] = $username; header('Location: http://www.ohjustthatguy.com/uploads/uploads.html'); } else { header('Location: http://www.ohjustthatguy.com/uploads/'); } ?> 

Pastebine source

+7
source share
3 answers

Make sure you delete the cookie with the same name and content with which you set it. Cookies for example.com and www.example.com will be treated as two different cookies. Similarly, cookies set in example.com and example.com/Support will have different paths. It is good practice to use .example.com as the domain and '/' as the path for cookies with a username so that they can be shared in your subdomains as well.

To debug this, you can use the FireCookie plugin for Firefox, which will display all this information.

+11
source

Setting the time of its expiration for some time in the past, you should clear it:

 setcookie("username",$username,time()-10); 

If you use PHP sessions to manage users, you probably also want session_destroy()

+7
source

You really should not store your user password in a cookie, especially if you are not using HTTPS! The password will be sent in clear text over the network for each request! In addition, never send your password to the user, this is a good server.

+1
source

All Articles