You are not logged in.

#1 2009-12-18 15:47:47

Lexion
Member
Registered: 2008-03-23
Posts: 510

php/mysql login [SOLVED]

I use a mysql database to store usernames and password and a php script to md5 encrypt them and access the database.  I wrote these two php scripts to access the database. login.php and register.php.  And there is a copy of the code at the bottom of this post.

It doesn't work.  There are no error messages from mysql, but the addUser function in register.php doesn't make a new mysql entry.  I'm not sure about login.php as I need an entry to test it.  Does anybody know what my problem is?

register.php

<?php
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "swordfish");
define("DB_NAME", "users");
define("TBL_USERS", "users");

$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $connection) or die(mysql_error());

function addUser($username, $password)
{
        global $connection;
        $password = md5($password);
        $q = "INSERT INTO " . TBL_USERS . " VALUES ('$username', '$password')";
        return mysql_query($q, $connection);
}

if (isset($_POST["reg"]))
{
        addUser($_POST["username"], $_POST["password"]);
        echo("<a href='login.php'>login</a><p />");
}
?>

<html>
<body>
<p />REGISTER WINDOW<br />
================<br />
<form method="Post" name="login">
        <input type="text", name="username" /> Username<br />
        <input type="text", name="password" /> Password<br />
        <input type="submit" name="reg", value="Register" />
</form>
</body>
</html

login.php

<?php
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "swordfish");
define("DB_NAME", "users");
define("TBL_USERS", "users");

function checkLogin($username, $password)
{
        $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
        mysql_select_db(DB_NAME, $connection) or die(mysql_error());

        $username = str_replace("'","''", $username);
        $password = md5($password);

        $q = "SELECT password FROM " . TBL_USERS . " WHERE username = '$username'";
        $result = mysql_query($q, $connection);
        if(!$result || (mysql_numrows($result) < 1))
        {
                echo("FAILED LOGIN ('$result')<br />");
                return 1;
        }

        $dbarray = mysql_fetch_array($result);
        if ($password == $dbarray['password'])
        {
                echo("SUCCESS<br />");
                return 0;
        }
        else
        {
                echo("FAILED LOGIN ('$password', '".$dbarray['password']."'<br />");
                return 0;
        }
}

if (isset($_POST["login"]))
{
        echo("Trying login... ");
        checkLogin($_POST["username"], $_POST["password"]);
}
?>

<html>
<body>
<p />LOGIN WINDOW<br />
============<br />
<form method="Post" name="login">
        <input type="text" name="username" /> Username<br />
        <input type="text" name="password" /> Password<br />
        <input type="submit" name="login" value="Login" />
</form>
<a href="register.php">Register</a>
</body>
</html>

Last edited by Lexion (2009-12-19 01:23:44)


urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand

Offline

#2 2009-12-19 01:23:28

Lexion
Member
Registered: 2008-03-23
Posts: 510

Re: php/mysql login [SOLVED]

I deleted the database and remade it from scratch.  It now works. hmm


urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand

Offline

Board footer

Powered by FluxBB