Jump to content

Login Script


ytmnd522

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]
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

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.