我正在开展一个学校项目,当我尝试测试我的login.php和signup.php页面时,我收到此错误并注意到。
对于signup.php:
注意:未定义的变量:第3行的C:\ xampp \ htdocs \ SneakerTrade \ signup.php中的用户
致命错误:未捕获错误:在C:\ xampp \ htdocs \ SneakerTrade \ signup.php中调用未知的成员函数is_loggedin():3堆栈跟踪:在C:\ xampp \ htdocs中抛出#0 {main}第3行的SneakerTrade \ signup.php
对于login.php:
注意:未定义的变量:第5行的C:\ xampp \ htdocs \ SneakerTrade \ login.php中的用户
致命错误:未捕获错误:在C:\ xampp \ htdocs \ SneakerTrade \ login.php中调用未知的成员函数is_loggedin():5堆栈跟踪:在C:\ xampp \ htdocs中抛出#0 {main}第5行的SneakerTrade \ login.php
我在一个名为Class.User.php的文件中设置了一组公共函数,用于注册,登录和主页。编写代码时,它编译得很好。我试图在这里和那里修复一些东西,但我还是PHP和PDO的新手,所以我不知道我做错了什么。
这是我的Class.User.php:
<?php
require_once('connect.php');
class USER
{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
public function register($firstName,$lastName,$Username,$email,$Password)
{
try
{
$new_password = password_hash($Password, PASSWORD_DEFAULT);
$stmt = $this->db->prepare("INSERT INTO users(firstName,lastName,Username,email,Password)
VALUES(:firstName, :lastName, :Username, :email, :Password)");
$stmt->bindparam(":Username", $Username);
$stmt->bindparam(":email", $email);
$stmt->bindparam(":Password", $new_password);
$stmt->bindparam(":firstName", $firstName);
$stmt->bindparam(":lastName", $lastName);
$stmt->execute();
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function login($Username,$email,$Password)
{
try
{
$stmt = $this->db->prepare("SELECT * FROM users WHERE Username=:Username OR email=:email LIMIT 1");
$stmt->execute(array(':Username'=>$Username, ':email'=>$email));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
if(password_verify($Password, $userRow['Password']))
{
$_SESSION['UserSession'] = $userRow['UserId'];
return true;
}
else
{
return false;
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function is_loggedin()
{
if(isset($_SESSION['UserSession']))
{
return true;
}
}
public function redirect($url)
{
header("Location: $url");
}
public function logout()
{
session_destroy();
unset($_SESSION['UserSession']);
return true;
}
}
?>
Connect.php,如果它有帮助:
<?php
// $user = 'root';
// $pass = '';
// $db = new PDO ( 'mysql:host=localhost;dbname=sneakertrade', $user, $pass );
class Database
{
private $host = "localhost";
private $db_name = "sneakertrade";
private $username = "root";
private $password = "";
public $conn;
public function dbConnection()
{
$this->conn = null;
try
{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
signup.php:
<?php
require_once 'connect.php';
if($user->is_loggedin()!="")
{
$user->redirect('home.php');
}
if(isset($_POST['signUp']))
{
$Username = trim($_POST['userName']);
$email = trim($_POST['email']);
$Password = trim($_POST['password']);
if($Username=="") {
$error[] = "Please Provide A UserName";
}
else if($email=="") {
$error[] = "Please Provide An Email";
}
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error[] = 'Please Enter A Valid Email Address';
}
else if($Password=="") {
$error[] = "Please Provide A Password";
}
else if(strlen($Password) < 6){
$error[] = "Password Must Be At Least 6 Characters";
}
else
{
try
{
$stmt = $DB_con->prepare("SELECT Username, email FROM users WHERE Username=:Username OR email=:email");
$stmt->execute(array(':Username'=>$Username, ':email'=>$email));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if($row['Username']==$Username) {
$error[] = "This User Already Exists";
}
else if($row['email']==$email) {
$error[] = "This Email Is Already In Use";
}
else
{
if($user->register($FirstName,$Lastname,$Username,$email,$Password))
{
$user->redirect('signup.php?joined');
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sign Up</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Index Custom CSS -->
<link href="css/signup.css" rel="stylesheet">
<!-- Animate.css -->
<link href="css/animate.css" rel="stylesheet">
<!-- Custom styles for this website -->
<link href="css/custom.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="col-md-8 col-md-offset-2">
<h1 id="loginPrompt">Sign Up</h1>
</div>
</div>
<form class="form-horizontal" role="form" method="post"
action="signup.php">
<div class="form-group">
<label for="inputName" class="col-md-2 col-md-offset-2 control-label">First
Name</label>
<div class="col-md-4">
<input type="text" class="form-control" id="inputName2"
name="firstName" placeholder="John">
</div>
</div>
<div class="form-group">
<label for="inputName2"
class="col-md-2 col-md-offset-2 control-label">Last Name</label>
<div class="col-md-4">
<input type="text" class="form-control" id="inputName2"
name="lastName" placeholder="Doe">
</div>
</div>
<div class="form-group">
<label for="inputUserName"
class="col-md-2 col-md-offset-2 control-label">Username</label>
<div class="col-md-4">
<input type="text" class="form-control" id="inputUserName"
name="userName" placeholder="JDoe">
</div>
</div>
<div class="form-group">
<label for="inputEmail3"
class="col-md-2 col-md-offset-2 control-label">Email</label>
<div class="col-md-4">
<input type="email" class="form-control" id="inputEmail3"
name="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputPassword3"
class="col-md-2 col-md-offset-2 control-label">Password</label>
<div class="col-md-4">
<input type="password" class="form-control" id="inputPassword3"
name="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-2">
<button type="submit" name="signUp" class="btn btn-default">Sign Up</button>
</div>
</div>
</form>
<div class="container">
<div class = "col-md-8 col-md-offset-2">
<h3 id="signUpMessage"></h3>
</div>
</div>
</body>
</html>
并登录.php:
<?php
session_start();
require_once 'connect.php';
if($user->is_loggedin()!="")
{
$user->redirect('home.php');
}
if(isset($_POST['submit']))
{
$Username = $_POST['email'];
$email= $_POST['email'];
$Password = $_POST['password'];
if($user->login($Username,$email,$Password))
{
$user->redirect('home.php');
}
else
{
$error = "Your Credentials Are Incorrect.";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="Landing Page">
<meta name="author" content="Jordan C. McRae">
<link rel="icon" href="images/favicon.ico">
<title>Login</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Login Custom CSS -->
<link href="css/login.css" rel="stylesheet">
<!-- Custom styles for this website -->
<link href="css/custom.css" rel="stylesheet">
</head>
<body class="body">
<div class="container">
<div class="col-md-8 col-md-offset-2">
<h1 id="loginPrompt">Log In</h1>
</div>
</div>
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-md-2 col-md-offset-2 control-label">Email</label>
<div class="col-md-4">
<input type="email" class="form-control" id="inputEmail3" name="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-md-2 col-md-offset-2 control-label">Password</label>
<div class="col-md-4">
<input type="password" class="form-control" id="inputPassword3" name="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-2">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-2">
<button type="submit" name="submit" class="btn btn-default">Log in</button>
</div>
</div>
</form>
</body>
</html>
就像我说的那样,我对PHP和PDO仍然很陌生,而且我一直试图弄清楚这几个小时,但无济于事。任何人都可以帮我解决这个问题吗?任何帮助表示赞赏。