Jump to content

cookie problems


contra10

Recommended Posts

sometimes when i click logout my cookie does not die  heres my scripts

 

Sets Cookie

<?php
// if login is ok then we add a cookie 
$_POST['username'] = stripslashes($_POST['username']); 
$hour = time() + 3600; 
setcookie(ID_my_site, $_POST['username'], $hour); 
setcookie(Key_my_site, $_POST['pass'], $hour);

header("Location: http://localhost/main/"); 
?>

 

if cookie is already set

  <?php 
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("registration") or die(mysql_error()); 

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{ 
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check )) 
{
if ($pass != $info['password']) 
{
}
else
{
header("Location: http://localhost/main/");

}
}
}
?>

 

header for pages

<?php
//checks cookies to make sure they are logged in 
if(isset($_COOKIE['ID_my_site'])) 
{ 
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['Key_my_site']; 
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
$in = mysql_query("SELECT username FROM users WHERE username ='$username'") or die (mysql_error());
$in2 = mysql_query("SELECT id FROM users WHERE username ='$username'") or die (mysql_error());
while($info = mysql_fetch_array( $check )) 
while($row = mysql_fetch_assoc($in))
while ($row2 = mysql_fetch_assoc($in2))
{ 
?>

 

logout

<?php 
$past = time() - 100; 
//this makes the time in the past to destroy the cookie 
setcookie(ID_my_site, gone, $past); 
setcookie(Key_my_site, gone, $past); 

header("Location: http://localhost/"); 
?> 	
<html>
<head><title>Thanks</title>
</head>
<body>
<h1>You've Logged Out</h1>

</body>
</html>

 

Link to comment
https://forums.phpfreaks.com/topic/147513-cookie-problems/
Share on other sites

At first looking over your code I thought maybe it was a Undefined Constant issue, however I think you did define these constants, but you have left out that code and more. This makes it hard to troubleshoot your code. Please post the script more completely. Of course leave out the actual values for the constants. Also make your subtraction from time larger it won't hurt. Finally is "gone" defined?

Link to comment
https://forums.phpfreaks.com/topic/147513-cookie-problems/#findComment-774516
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.