Forum Moderators: coopster

Message Too Old, No Replies

PHP: Cookie/Session problems

mysql errors when checking the cookie

         

freshmold

9:42 pm on Apr 5, 2003 (gmt 0)

10+ Year Member



HI!

I set up a cookie for a "remember me" thing. It uses the session_id() as the value. Here's the function for finding out if someone's logged in or not, and if the cookie is there:


27 function checkLoggedIn($status){
28 switch($status){
29 case "yes":
30 if((!$_SESSION["loggedIn"]) & ($_COOKIE['mem']!= '' )){
31 $query = mysql_query("SELECT * FROM users WHERE sessionid = '{$_COOKIE['mem']}");
32 $crumb = mysql_fetch_array($query);
33 $login = $crumb[1];
34 $_SESSION["login"]=$crumb[1];
35 $_SESSION["loggedIn"]=true;
36 header("Location: site.com/logged_in.php);
37 } else if (!$_SESSION["loggedIn"]){
38 header("Location: site.com/not_logged_in.php");
39 exit;
40 }
41 break;
42 case "no":
43 if($_SESSION["loggedIn"]){
44 header("Location: site.com/logged_in.php);
45 }
46 break;
47 }
48 return true;
49 }

Now, when a user returns to the site, two errors happen:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/yup/function.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at /home/yup/function.php:32) in /home/yup/function.php on line 36

When I refresh the site, those errors go away (because loggedin = true).

So, I'm guessing I need to rewrite that bit about the cookie. Can anyone help?

Thanks.

sepang

12:23 am on Apr 6, 2003 (gmt 0)

10+ Year Member



yo

don't use the mysql_query() function without precising what it is supposed to do if it didn't work.

Moreover, you have an obvious MySQL syntax error... just put the following:

so,

$query = mysql_query("SELECT * FROM users WHERE sessionid = '".$_COOKIE['mem']."'")OR die(mysql_error);

The last problem is dued to an HTML output which is dued itself to the display of the mySQL error, so if the first works, the second should work too.

SepanG
France

freshmold

12:54 am on Apr 6, 2003 (gmt 0)

10+ Year Member



Thanks man.

I'm sure that fixed the problem. I'll have to wait a bit (to make certain that I'm using the cookie to log in) to check if it's fully fixed. Probably is.

Thanks again.

- fresh