[PHP] Simple Check Script

Newbie Spellweaver
Joined
Feb 10, 2008
Messages
30
Reaction score
0
PHP:
<?
/* Created by: VoilACMS Team */
//Require the database connector
require_once("includes/db_fns.php");
//Start session
session_start();
 
 //Define variables
 $username = strip_tags($_POST['username']); #Strip HTML Tags
 $password = sha1($_POST['password']); #SHA1 Encryption
 
 //Select stuff from the users "profile" based on the username and password
 $sql = "SELECT * from `user` where `username` = '$username' and `password` = '($password')";
 $result = mysql_query($sql);
 $fetch = mysql_fetch_array($result);
 
 if($fetch)
 {
 echo "Good";
 }
 else
 {
 echo "Bad";
 }
?>

There it is. Its just to check if the username and password exists in the database. But I always get an error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*******/public_html/check.php on line 19

Any suggestions?
 
PHP:
 $sql = "SELECT * from user where username = '$username' and `password` = '($password')";
why do you have your password in () when you don't need to. take them out and try that. =]
 
Wow thanks. Also I found another error of mine. When I first started coding this project I didnt know I was going to use sha1 encryption. My password row was set to varchar 25 and not 40. If i login with previous accounts = bad if i make a new account with the modded varchar limit = good. So thanks for the help. Stupid errors but Im not sure why I even caught that () error and it didnt work. I must of modded something else too.

Thanks again,

Aaron.
 
PHP:
 $sql = "SELECT * from user where username = '$username' and `password` = '($password')";
why do you have your password in () when you don't need to. take them out and try that. =]

PHP:
 $sql = "SELECT * from user where `username` = '$username' and `password` = '$password'";

@andrew951, there were `` around password but not username =x
 
PHP:
 $sql = "SELECT * from user where `username` = '$username' and `password` = '$password'";

@andrew951, there were `` around password but not username =x
i was editing my own copy of that script and i dunt like the ` in mysql scripts for some reason.
anyways, i was looking through it and then i found it.
i tried copying his but my copying just sux
so i just copied mine. =]
 
PHP:
<?php

$query = mysql_query('SELECT id FROM `user` WHERE `username` = '.$username.' AND `password` = '.$password.' LIMIT 1');
$row = mysql_fetch_assoc($query);

echo 'Username\'s ID: '.$row['id'];

?>

I would personally do it this way.
 
Well i changed the code around a little bit and I now have a full working login script. Of course I still have to add more because it has security flaws. Thanks for the help.
 
Back