Jump to content

Archived

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

Fixxer

Questions about MySQL

Recommended Posts

If I store the information using the MD5 function, how do I use php to convert it back to normal when I want to check a login? I used myphpadmin to convert it to MD5.

Also, what would I put for local host, my domain name?

Share this post


Link to post
Share on other sites
This is the whole point of MD5 , that you cannot reverse engineer it.

What you do is you store the MD5 informtion somewhere.

Then, when the user enters their login, you MD5 it and check if it is equal to the previous one.

So, if there is a field called PASSWORD in your table, you could do this to check it against what someone entered in a form:
[code]
$pass = $_POST['pass'];
$md5Pass = md5($pass);
$sql = "SELECT * FROM table WHERE PASSWORD='$md5Pass' ";
$result = mysql_query($sql);
[/code]
I left some things out like DB connection etc. but it should get your started.

Share this post


Link to post
Share on other sites
as far as your 2nd question: in most cases, your local host can simply be "localhost" if that doesn't work, then yes, you will have to put your database server.

Share this post


Link to post
Share on other sites
Ok am trying this code, but so far no login!

[code]
<?php

$dbHost = "localhost";
$dbUsername = "edited";
$dbPassword = "edited";
$dbName = "Xiemsoft_Site";
#$username = $_POST['user'];
#$password = $_POST['pass'];
$username = "testname";
$password = "testpass";
$md5Password = MD5($password);

$link = mysql_connect($dbHost,$dbUsername,$dbPassword);

mysql_select_db($dbName,$link);

$SQL = "SELECT username FROM webhosting WHERE username = \"$username\" AND password = \"$md5Password\"";

$result = mysql_query($SQL,$link);

if(mysql_num_rows($result) == 0)
{
  $message = "Invalid Login.";
}
else
{
  $message = "Logged In!";
}

?>
[/code]

Share this post


Link to post
Share on other sites

Your problem might be that MySQL wants string surrounded by single quotes, not double quotes. Make sure you have an "or die" clause on all your mysql functions that might cause an error.

[code]<?php
$db_host = "host";
$db_user = "name";
$db_pass = "pass";
$db_name = "name";
$pass = "name"
$username = "pass"
$link = mysql_connect($db_host, $db_user, $db_pass) or die("Couldn't connect to the database<br>" . mysql_error());
mysql_select_db($db_name, $link) or die("Couldn't select database<br>" . mysql_error());
$query = "SELECT username FROM webhosting WHERE username = '$username' AND password = '$pass' ";
$result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
if(mysql_num_rows($result) == 0)
{
  $message = "LOSER";
}
else // the number of rows is either going to be zero or one, so you only need the else here
{
  $message = "WINNER";
}
?>
<html>
<head>
<title>TEST</title>
</head>
<body>
TESTING 123...
<?php echo $message; ?>
</body>
</html>[/code]
Whenever you're debugging, make sure the error reporting is set to the maximum by putting this at the top of your script:
[code]<?php
// Report all PHP errors (bitwise 63 may be used in PHP 3)
error_reporting(E_ALL);
?>[/code]

Ken

Share this post


Link to post
Share on other sites
You're right, at the top of all your files you should put this:
[code]error_reporting(E_ALL);[/code]
Because PHP should have thrown an error on the SQL line because of your use of double quotes inside a double quotes string.

Share this post


Link to post
Share on other sites
Ahh thanks, it appears that I cannot connect :x

Couldn't connect to the database
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (46)

Share this post


Link to post
Share on other sites
It most likely means that your mysql database is not up.

Ken

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.