Jump to content

Archived

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

ytmnd522

Login Script

Recommended Posts

Hello guys.. Im having a hard time loging a user in. This is my script.. When I submit the data it just refreshes with no response if the user was found on da database or anything..what's up with my script.
I have a mysql database named "mike" with a few records of users with passwords.
thanks in advanced.

[code]<? session_start()?>

<html>
<body>
<form action="vtcsignin.php" method="post">
users:
<INPUT type="text" name="user"><br>
password:
<INPUT type="password" name="password"><br>
<INPUT type="submit">
</form>
</body>
</html>
<?

if(isset($_POST['user']) && isset($_POST['pass'])){
    if($logged_in_user == $user){
        echo $_POST['user'] . " Your are already logged in";
    exit;
    
    }
}

$db = mysql_connect("localhost","root") or die ("cant connect to database");
mysql_select_db("mike", $db) or die("could not select database");
$result = mysql_query("SELECT * FROM users WHERE userName = '".$user."' AND userPassword = PASSWORD('".$pass."')");

if(!$result){
    print "no information brought back";
}

if(mysql_num_rows($result) > 0){
$_SESSION['logged_in_user']=$user;
echo "welcome, " . $user;
exit;
}elseif(mysql_num_rows($result)==0){
    echo "No record found in the database";
    session_destroy();
}


?>[/code]

Share this post


Link to post
Share on other sites
[code]if(mysql_num_rows($reuslt) > 0){[/code] shouldn't that be $result?

Share this post


Link to post
Share on other sites
yea i noticed that..and I thought that was the problem..but turns out it's not. BTW im running php 4.3.10

Share this post


Link to post
Share on other sites
[!--quoteo(post=381998:date=Jun 9 2006, 01:50 PM:name=MikeyLopez)--][div class=\'quotetop\']QUOTE(MikeyLopez @ Jun 9 2006, 01:50 PM) [snapback]381998[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hello guys.. Im having a hard time loging a user in. This is my script.. When I submit the data it just refreshes with no response if the user was found on da database or anything..what's up with my script.
I have a mysql database named "mike" with a few records of users with passwords.
thanks in advanced.

[code]<? session_start()?>

<html>
<body>
<form action="vtcsignin.php" method="post">
users:
<INPUT type="text" name="user"><br>
password:
<INPUT type="password" name="password"><br>
<INPUT type="submit">
</form>
</body>
</html>
<?

if(isset($_POST['user']) && isset($_POST['pass'])){
    if($logged_in_user == $user){
        echo $_POST['user'] . " Your are already logged in";
    exit;
    
    }
}

$db = mysql_connect("localhost","root") or die ("cant connect to database");
mysql_select_db("mike", $db) or die("could not select database");
$result = mysql_query("SELECT * FROM users WHERE userName = '".$user."' AND userPassword = PASSWORD('".$pass."')");

if(!$result){
    print "no information brought back";
}

if(mysql_num_rows($result) > 0){
$_SESSION['logged_in_user']=$user;
echo "welcome, " . $user;
exit;
}elseif(mysql_num_rows($result)==0){
    echo "No record found in the database";
    session_destroy();
}
?>[/code]
[/quote]

I dont see where these two variables are set
$logged_in_user
$user

Share this post


Link to post
Share on other sites
You are relying on register globals to be ON here, and you are querying regardless of POST or not being set.
AND you are checking for posted variable "pass" while your password textfield is named "password".
Work your way with this one, i've not included a "is logged in"-check though.
[code]
<?
session_start()

if(isset($_POST['submitform']))
{
if(!empty($_POST['user']) || !empty($_POST['pass']))
{
$user = htmlspecialchars($_POST['user']);
$pass = htmlspecialchars($_POST['pass']);

$db = mysql_connect("localhost","root") or die ("cannot connect to database");
mysql_select_db("mike", $db) or die("could not select database");

$result = mysql_query("SELECT * FROM users WHERE userPassword = PASSWORD('$pass') AND userName = '$user'");

if(mysql_num_rows($result) == "1")
{
$_SESSION['logged_in_user'] = $user;
echo "welcome, " . $user;

exit();
}
else
{
echo "No record found in the database";
}
}
else
{
echo "Please fill out both username and password field..";
}
}
echo <<<_HTML

<form action = "vtcsignin.php" method = "post">
users: <INPUT type = "text" name = "user"><br>
password: <INPUT type = "password" name = "pass"><br>
<INPUT type = "submit" name = "submitform">
</form>

_HTML;

?>
[/code]
You should also consider moving away from using PASSWORD in your query as it's not supported in newer mysql versions. Move to md5 or sha1 instead.

Share this post


Link to post
Share on other sites

×

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.