Jump to content

Recommended Posts

I have a set of functions that store and retrieve info from a database about the user based on a single cookie (Kind of like php sessions). I am having trouble with getting the username from the database based on their cookie. here is my code:

 

<?php
//Connect
mysql_connect(Ya right);
mysql_select_db(lol);
//End Connect

function set_cookie($username) {
for ( $i = 0; $i < 24; $i++ ) {
	$uid .= chr(rand(65,90));
}
mysql_query("UPDATE cookie SET uid = '{$uid}' WHERE username = '{$username}' LIMIT 1 ;");
setcookie("uid", $uid);
} 

function get_cookie() {
if ( $_COOKIE['uid'] ) {
	$cookie = mysql_fetch_array(mysql_query("SELECT username FROM cookie WHERE uid = '{$_COOKIE['uid']}' LIMIT 1 ;"));
	return $cookie['username'];
} else {
	return false;
}
}

function kill_cookie() {
$username = get_cookie();
mysql_query("UPDATE cookie SET uid = '' WHERE username = '{$username}' LIMIT 1 ;");
}
?>

 

When i call get_cookie() on another page, it returns nothing for the username. Here is that code:

 

<?php
require("cookie_functions.php");
set_cookie("jjacquay712");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cookie Test</title>
</head>

<body>
<?php
echo get_cookie();
?>

</body>
</html>

 

Any ideas about what might be causing my problem? Thanks, John

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

the set cookie function works fine, the problem is with get_cookie function.

 

Hey, what do I know? I have only been coding in PHP for the past 12 years.

 

But in the very slight case I am wrong try this:

 

function get_cookie() {
   if (isset($_COOKIE['uid'])) {
      $query = mysql_query("SELECT username FROM cookie WHERE uid = '{$_COOKIE['uid']}' LIMIT 1") or die('COOKIE UID:' . $_COOKIE['uid'] . ' SQL STATEMENT: ' . mysql_error());
      $cookie = mysql_fetch_array($query);
      return $cookie['username'];
   } else {
      echo 'COOKIE UID Has not been set.';
      return false;
   }
}

 

Give that a try and see what happens.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.