gpt4 book ai didi

php - mySQL 和 html 形式

转载 作者:行者123 更新时间:2023-11-29 10:31:36 26 4
gpt4 key购买 nike

我在 bootstrap studio 的帮助下制作了一个表单,并且已经创建了一个数据库并使用 php 建立了连接。这只是html(没有css)

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/fonts/ionicons.min.css">
<link rel="stylesheet" href="assets/css/Login-Form-Dark.css">
<link rel="stylesheet" href="assets/css/Registration-Form-with-Photo.css">
<link rel="stylesheet" href="assets/css/styles.css">
</head>

<body>
<div class="login-dark" style="background-image:url(&quot;assets/img/login-background.jpg&quot;);background-color:rgb(255,255,255);">
<form action="login.php" method="POST">
<h2 class="sr-only">Login Form</h2>
<div class="illustration"><i class="icon ion-ios-locked-outline"></i></div>
<div class="form-group">
<input class="form-control" type="text" name="username" placeholder="Username">
</div>

<div class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password">
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit">Log In</button>
</div><a href="#" class="forgot">Forgot your email or password?</a></form>
</div>
</body>
</html>

这是 php:

<?php
include('includes/DB.php');
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
echo submitted;
//Runs query to see if username exists
if (DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$username))) {
//Runs query to search for password
$passwordDB = DB::query('SELECT password FROM users where username=:username', array(':username'=>$username))[0]['password'];
//Checks if password already exists
if($password==$passwordDB) {
echo 'Logged in!';
header('Location: main-page');
//Create cookiee for user to be logged in
$cstrong = True;


$token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong));
$user_id = DB::query("SELECT id FROM users WHERE username=:username", array('username'=>$username))[0]['id'];
//store token in database
//sha1 will hash the token
DB::query("INSERT INTO login_tokens VALUES ('', :token, :user_id)",array(':token'=>sha1($token), ':user_id'=>$user_id));

//cookie to log the user in
//cookie will be valid for 1 week (time()+60*60*24*7)
setcookie("SNID",$token, time()+60*60*24*7, '/', NULL,NULL,TRUE);
setcookie("SNID_",1,time()+60*60*24*3, '/', NULL,NULL,TRUE);
header('Location: main-page.php');
} else {
echo 'Incorrect Password!';
}
}
else {
echo 'User not registered!';
}
};
?>

但是,当我登录时,什么也没有发生。它应该回显登录状态,然后将我重定向到另一个页面(如果有帮助的话,我将 XAMPP 与 phpmyadmin 一起使用)

我尝试添加

include("login.html");

在 php 文件之上,但是没有区别。

在我使用 bootstrap studio 之前,我创建了一个简单的纯 html 表单并且它有效,当我使用 bootsrap studio 使表单看起来更好时,我遇到了这个问题。对 php 和 mySQL 相当陌生,我该如何修复它?

最佳答案

试试这个,让我知道显示的内容。

html 文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/fonts/ionicons.min.css">
<link rel="stylesheet" href="assets/css/Login-Form-Dark.css">
<link rel="stylesheet" href="assets/css/Registration-Form-with-Photo.css">
<link rel="stylesheet" href="assets/css/styles.css">
</head>

<body>
<div class="login-dark" style="background-image:url(&quot;assets/img/login-background.jpg&quot;);background-color:rgb(255,255,255);">
<form action="login.php" method="POST">
<h2 class="sr-only">Login Form</h2>
<div class="illustration"><i class="icon ion-ios-locked-outline"></i></div>
<div class="form-group">
<input class="form-control" type="text" name="username" placeholder="Username">
</div>

<div class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password">
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit" name="login">Log In</button>
</div><a href="#" class="forgot">Forgot your email or password?</a></form>
</div>
</body>
</html>

PHP

<?php
include('includes/DB.php');
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
echo "username: ";
echo $username;
echo "<br>";
echo "password: ";
echo $password;

//Runs query to see if username exists
if (DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$username))) {
//Runs query to search for password
$passwordDB = DB::query('SELECT password FROM users where username=:username', array(':username'=>$username))[0]['password'];
//Checks if password already exists
if($password==$passwordDB) {
echo 'Logged in!';
//Create cookiee for user to be logged in
$cstrong = True;


$token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong));
$user_id = DB::query("SELECT id FROM users WHERE username=:username", array('username'=>$username))[0]['id'];
//store token in database
//sha1 will hash the token
DB::query("INSERT INTO login_tokens VALUES ('', :token, :user_id)",array(':token'=>sha1($token), ':user_id'=>$user_id));

//cookie to log the user in
//cookie will be valid for 1 week (time()+60*60*24*7)
setcookie("SNID",$token, time()+60*60*24*7, '/', NULL,NULL,TRUE);
setcookie("SNID_",1,time()+60*60*24*3, '/', NULL,NULL,TRUE);

die();
header('Location: main-page.php');
} else {
echo 'Incorrect Password!';
die();
}
}
else {
echo 'User not registered!';
die();
}
};
?>

关于php - mySQL 和 html 形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47311171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com