Ho inviato la mia domanda here e prima di modificare il post era chiusa come non è una domanda reale!controllare nome utente e password sul database (script incluso)
Ho un form di login come questo:
<html>
<head>
<title>Password Checking Script</title>
</head>
<body>
<form action="check_user-pass.php" method="POST">
<h3>Please Login</h3>
User Name: <input type="text" name="user_name"><br>
Password: <input type="password" name="password">
<input type="submit" name="submit" value="Login!">
</form>
</body>
</html>
Come si vede, questa forma autentica l'utente attraverso check_user-pass.php
.
Cerca quelle credenziali sul mio database; se esistono, restituisce OK
, altrimenti restituisce il valore NO
.
Quindi la mia domanda è: esattamente quale codice dovrei includere in check_user-pass.php
?
Ho provato ad aggiungere altro codice ma non ho potuto farlo anche io! Il mio codice attuale è:
<?php
ob_start();
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("$db_name") or die(mysql_error());
echo "Connected to Database<br />";
// Check $username and $password
/*
if(empty($_POST['username']))
{
echo "UserName/Password is empty!";
return false;
}
if(empty($_POST['password']))
{
echo "Password is empty!";
return false;
}
*/
// Define $username and $password
$username=$_POST['username'];
$password=md5($_POST['pass']);
// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if ($count==1) {
echo "Success! $count";
} else {
echo "Unsuccessful! $count";
}
ob_end_flush();
?>
Questo codice deve funzionare. Qual è il problema che stai affrontando ?? –
@PdsIr ** Benvenuto in StackOverflow! ** Assicurati di [leggere il faq] (http://stackoverflow.com/faq). –