我试图达到使用相同 php 控制器文件的登录页面和注册页面的地步。输入 site.com/?login 时,会显示登录页面(已完成),然后当用户现在提交登录页面时,我希望它显示“授予/拒绝访问”。注册页面也是如此(site.com/?registeration 进入注册页面并打印提交是否成功)。我遇到了一个问题,即在提交任一页面时,它都没有显示我期望的输出。
<?php
include $_SERVER['DOCUMENT_ROOT'] . 'includes/db_connection.php';
/*********************Registration page code***************************/
if (isset($_GET['register']))
{
$action = 'register_form';
include 'register.html.php';
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'register_form') {
$username = mysqli_real_escape_string($connection, $_POST['username']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = mysqli_real_escape_string($connection, $_POST['password']);
$output = 'Username: ' . $username . ' Email: ' . $email . ' Password: ' . $password;
$sql = 'INSERT INTO user (username,email,password) VALUES
("'. $username .'","'. $email .'","'. $password .'")';
if (!mysqli_query($connection, $sql))
{
$error = 'Error registering user: ' . mysqli_error($connection);
include 'output.html.php';
exit();
}
include 'output.html.php';
exit();
}
/*************************************************************************/
if (isset($_GET['login']))
{
$action = 'login_form';
include 'login.html.php';
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'login_form' )
{
$username = mysqli_real_escape_string($connection, $_POST['username']);
$password = mysqli_real_escape_string($connection, $_POST['password']);
$output = 'Username: ' . $username . ' Password: ' . $password;
$sql = 'SELECT COUNT(*) FROM user WHERE username="'.$username.'" AND password="'.$password.'"';
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error seeing if user exists in the DB.';
include 'output.html.php';
exit();
}
$num = mysqli_num_rows($result);
if($num > 0) {
$output .= '\nAccess Granted!';
} else {
$output .= '\nAccess Denied! Please register first...';
}
include 'output.html.php';
exit();
}
$output = 'Database connection established.<br/>Server root: ' . $_SERVER['DOCUMENT_ROOT'];
include 'output.html.php';
?>
下面的代码是我的注册页面(登录页面很相似):
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
<!--link type="text/css" rel="stylesheet" href="stylesheet.css"/-->
</head>
<body>
<div id="content">
<form id="registration_form" method="POST" action="?<?php htmlout($action); ?>">
<label for="username-id">Username:</label>
<input id="username-id" type="text" name="username" autofocus="autofocus" /><br/><br/>
<label for="email-id">Email:</label>
<input id="email-id" type="email" name="email" /><br/><br/>
<label for="password1-id">Password:</label>
<input id="password1-id" class="text-input" type="password" name="password" /><br/><br/>
<label for="password2-id">Retype Password:</label>
<input id="password2-id" class="text-input" type="password" name="password_repeat" /><br/><br/>
<input type="submit" value="Register"/>
</form>
</div>
</body>
</html>
output.html.php 页面只是一个回显$output 和$error 的简单页面。
关于我做错了什么的任何想法?我对 PHP 很陌生,大部分代码都遵循了使用 PHP 和 MySQL 构建您自己的数据库驱动的网站一书。