所以我正在为我的网站开发一个 PHP/SQL 登录脚本。服务器是一个运行 Apache 的 UNIX 系统,并且安装了 PHP,这点我很确定。我已经设置了 SQL 数据库,并且我的 PHP 代码被分成了多个部分。
http://www.woodlandastronomy.org/login.php:
<?php
session_start();
require_once 'http://www.woodlandastronomy.org/cgi-bin/classes/membership.php';
$membership = new Membership()
if($_POST && !empty($_POST['username']) && !empty($_POST['pwd'])) {
$response = $membership->validate_User($_POST['username'], $_POST['pwd']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="Woodland Astronomy Club Website">
<meta name="keywords" content="Woodland, Astronomy, Club, Website, Moss, Lake, Neighborhood, Astronomical, Association">
<link rel="stylesheet" href="wasmain.css">
<link rel="icon" href="http://www.woodlandastronomy.org/favicon.ico" type="image/x-icon">
<script type="text/javascript" src="script1.js"></script>
<title>Woodland Astronomy Club - Home</title>
</head>
<body>
<div id="container">
<div id="header">
<img src="IMG_9897 2 (3) copy.jpg" alt="we has a issue, sorrys!1!!!" width="100%" height="200px">
<div id="linkbar">
<p class="linkbarbutton">
<b><a class="linkbarlink" href="http://www.woodlandastronomy.org/index.html">Home</a></b>
</p>
<p class="linkbarbutton"><b><a class="linkbarlink" href="http://www.woodlandastronomy.org/aboutus.html">About Us</a></b></p>
<p class="linkbarbutton"><b><a class="linkbarlink" href="http://www.woodlandastronomy.org/events.html">Club Events</a></b></p>
<p class="linkbarbutton"><b><a class="linkbarlink" href="http://www.woodlandastronomy.org/eventpix.html">Club Photos</a></b></p>
<p class="linkbarbutton"><b><a class="linkbarlink" href="http://www.woodlandastronomy.org/astropix.html">Astrophotography</a></b></p>
<p class="linkbarbutton"><b><a class="linkbarlink" href="http://www.weatherlink.com/user/theweathercat/" target="_blank">Weather Station</a></b></p>
<p class="linkbarbutton"><b><a class="linkbarlink" href="http://www.woodlandastronomy.org/contact.html">Contact Us</a></b></p>
<p class="linkbarbutton"><b><a class="linkbarlink" href="http://www.woodlandastronomy.org/links.html">External Links</a></b></p>
<p class="linkbarbutton"><b><a class="linkbarlink" href="http://www.woodlandastronomy.org/members.html">Members</a></b></p>
</div>
</div>
<div id="content">
<div id="fluffy"></div>
<div id="login">
<form method="post" action="">
<h2 class="yellowlabel">Login <small>Enter Your Credentials</small></h2>
<p class="yellowlabel">
<label for="username" >Username: </label>
<input type="text" name="username">
</p>
<p class="yellowlabel">
<label for="password">Password: </label>
<input type="password" name="pwd">
</p>
<p>
<input type="submit" id="submit" value="Login" name="submit">
</p>
</form>
<?php
if(isset($response)) echo "<h4 class='alert'>" . $response . "</h4>";
?>
</div>
</div>
</div>
</body>
</html>
然后,
http://www.woodlandastronomy.org/cgi-bin/classes/membership.php:
<?php
require 'http://www.woodlandastronomy.org/cgi-bin/classes/Mysql.php';
class Membership {
function validate_user($un, $pwd) {
$mysql = New Mysql();
$ensure_credentials = $mysql->verify_Username_and_Pass($un, $pwd);
if($ensure_credentials) {
$_SESSION['status'] = 'authorized';
header("location: http://www.woodlandastronomy.org/members.html");
} else return "Please enter a correct username and password";
}
}
?>
然后,
http://www.woodlandastronomy.org/cgi-bin/classes/mysql.php:
<?php
require_once 'http://www.woodlandastronomy.org/cgi-bin/classes/constants.php';
class Mysql {
private $conn;
function __construct() {
$this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or
die('There was a problem connecting to the database.');
}
function verify_Username_and_Pass($un, $pwd) {
$query = "SELECT *
FROM Membership
WHERE username = ? AND password = ?
LIMIT 1";
if($stmt = this->conn->prepare($query)) {
$stmt->bind_param('ss', $un, $pwd);
$stmt->execute();
if($stmt->fetch()) {
$stmt->close();
return true;
}
}
}
}
?>
最后,
http://www.woodlandastronomy.org/cgi-bin/classes/constants.php:
<?php
define('DB_SERVER', 'localhost');
define('DB_USER', '/*I didn't want to put my database credentials on the web, you understand...');
define('DB_PASSWORD', '');
define('DB_NAME', 'Member');
?>
所以之前,页面是完全空白的,但这是包含文件中的语法错误。在我找到这个之后,我开始收到错误消息,告诉我 PHP 无法连接到 MySQL 数据库。经过更多的工作,我设法完全摆脱了错误消息,但是现在当我单击登录时,它只是尝试加载,直到浏览器最终给出消息“与服务器的连接已重置”。
我迷路了。我的邻居擅长 PHP,但他从今天下午开始就出城了,我似乎无法自己解决这个问题。
如果有人能告诉我我做错了什么,那就太好了。
很抱歉这个问题太长了。
谢谢,哈罗德