我看到了一些错误,这些错误已在下面修复。
这就是我所犯的错误:
if (login())
{?>
<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>
<?php}
else
{
echo "Incorrect login details. Please login";
}
这就是我要做的:
<html>
some code
<?php
function login()
{
if (empty ($_POST['username']))
{
return false;
}
if (empty ($_POST['password']))
{
return false;
}
$username = trim ($_POST['username']);
$password = trim ($_POST['password']);
$scrambled = md5 ($password . 'foo');
$link = mysqli_connect('localhost', 'root', 'password');
if (!$link)
{
$error = "Unable to connect to the database server";
include 'error.html.php';
exit ();
}
if (!mysqli_set_charset ($link, 'utf8'))
{
$error = "Unable to set database connection encoding";
include 'error.html.php';
exit ();
}
if (!mysqli_select_db ($link, 'foo'))
{
$error = "Unable to locate the foo database";
include 'error.html.php';
exit ();
}
$sql = "SELECT COUNT(*) FROM admin WHERE username = '$username' AND password = '$scrambled'";
$result = mysqli_query ($link, $sql);
if (!$result)
{
return false;
exit ();
}
$row = mysqli_fetch_array ($result);
if ($row[0] > 0)
{
return true;
}
else
{
return false;
}
}
if (login())
{
echo '<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>';
}
else
{
echo "Incorrect login details. Please login";
}
?>
some more html code
</html>