• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP/MySQL Help, please

agent007

New Member
Okay, I'm making a little "shoutbox" script more as a learning experience than anything else. What I need to do is check if the username and password is correct. I'm pretty sure it's something like this, but it doesn't work.

Here's what I currently have:
Code:
        $sql = "SELECT COUNT($username) AS num FROM shout_users WHERE username='$nick' AND password = '$password'";
  $result=mysql_query($sql);
  if($num == 0) {
echo "Please recheck your username and password combination";
  } else{ 
setcookie ("shoutuser", $username,time()+31536000);
echo "<META HTTP-EQUIV=\"refresh\" content=\"1;URL=$page\">";
  }
No matter what I put in the username/password fields, it always goes to the else statement, rendering it useless. :(

Also, this error shows up whenever I try to "login":
Code:
Warning: Cannot add header information - headers already sent by (output started at /home/gta3tips/gta3tips.hey.nu/index.php:4) in /home/gta3tips/gta3tips.hey.nu/shoutbox.php on line 79
Line 79 in my code is the setcookie function.
 
Well, I may just be tired but I don't see where the '$num' variable is declared - your testing to see if a non-existant variable is 0 - so you should go $result == 0, because $result was holding how many things returned - right? If I had it my way I would go:

Code:
$sql = "SELECT * FROM shout_users WHERE username='$nick' AND password = '$password'";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
  if($num == 0) {
echo "Please recheck your username and password combination";
  } else{ 
setcookie ("shoutuser", $username,time()+31536000);
echo "<META HTTP-EQUIV=\"refresh\" content=\"1;URL=$page\">";
  }
 
Okay, now it successfully checks the user/pass combination. But, when I try to set a cookie with the user's username, it says:
Warning: Cannot add header information - headers already sent by (output started at /home/gta3tips/gta3tips.hey.nu/index.php:4) in /home/gta3tips/gta3tips.hey.nu/shoutbox.php on line 80
Any ideas? :(
 
Make sure you haven't echoed or printed anything, and don't have any blank lines (or anything) before the <?
 
Okay, I've made sure there are no spaces/anything before/after the <? and ?> tags. It still won't work. You can go here to see the problem. You can login with the username testuser and the password test.

Any help is appreciated! :)

[edit] If you want to see the code, I'm willing to post it up here. :)
 
the problem is you are using a header after you output

remove any echos or other output b4 you use a header
 
Back
Top