gpt4 book ai didi

php - 登录页面,在同一页面查看密码

转载 作者:行者123 更新时间:2023-11-29 13:21:39 26 4
gpt4 key购买 nike

我试图创建一个登录页面,但无法使其工作。我有三个文件:标题、登录和索引页。以下是代码:admin_header.php:

<html>
<head>
<title>Project</title>
<link type="text/css" rel="stylesheet" href="styles.css"/>
</head>
<body>
<body id="home">
<div class="top" id="header">
<ul>

<li><a class="menu" href="index.php">Home</a></li>
<li><a class="menu" href="search.php">Add an Artist</a></li>
<li><a class="menu" href="contact.php">List the Artists</a></li>
<li><a class="menu" href="contact.php">Add a movie</a></li>
<li><a class="menu" href="contact.php">List the movies</a></li>
<li><a class="menu" href="contact.php">Add an admin</a></li>
</ul>


</div>
<?php
$server = 'localhost';
$user = 'root';
$pass = 'blablabla';
$mydb = 'projectdb';
$movies = 'movies';
$artists='artists';
$roll='roll';
$users='Users';
$connect = mysqli_connect($server, $user, $pass);
mysqli_select_db($connect,$mydb)or die("cannot select DB");
session_start();
define('ADMIN',$_SESSION['username']);
if(!$_SESSION['username'])
{
header("location:admin_login.php");
}

?>
</body></html>

管理员登录:

<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php

$login_form = <<<EOD
<form name="login" id="login" method="POST" action="">
<p><label for="username">Please Enter Username: </label><input type="text" size="100" name="username" id="username" value="" /></p>
<p><label for="password">Please Enter Password: </label><input type="password" size="40" name="password" id="password" value="" /></p>
<p><input type="submit" name="submit" id="submit" value="Submit"/> <input type="reset" name="reset" id="reset" value="reset"/></p>
</form>
EOD;
if (isset($_GET['msg']))
{
$msg=$_GET['msg'];
if($msg!='')
{
echo '<p>'.$msg.'</p>';
}
}
//If message is set echo it}

echo "<h1>Please enter your Login Information</h1>";
echo $login_form;
ob_start();


if (!isset($_POST['username'])||($_POST['password']))
{
echo "Please, enter your login information";
}
else
{
$username = $_POST['username'];
$password = $_POST['password'];
if (!$connect)
{
echo ("Cannot connect to $server using $user");
}
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($connect,$username);
$password = mysqli_real_escape_string($connect,$password);
$sql="SELECT * FROM $users WHERE username='".$username."' and password='".$password."'";
$result=mysqli_query($connect,$sql);
$count=mysqli_num_rows($result);
if($count==1)
{
session_start();
$_SESSION["username"]=$username;
$_SESSION["password"]=$username;
header("location:admin_index.php");

}
else
{
$msg = "Wrong Username or Password. Please retry";
echo $msg;
}
}
ob_end_flush();
?>
</body>
</html>

管理索引:

<html>
<body>
<?php
include('admin_header.php');
?>

<html>
<body>
Login Successful
</body>
</html>

我没有收到任何错误,但无论我输入什么数据,都不会重定向到索引页。你能帮忙吗?

最佳答案

一些问题:

  • 当您已经将文本输出到浏览器时,您正在使用 header() 调用。这是行不通的,在向浏览器输出任何内容之前,您需要将所有 php 内容(例如 session_startheader 调用)放在脚本的最顶部。
  • 切勿使用纯文本密码,您应该在存储密码之前对它们进行加盐和哈希处理。

关于php - 登录页面,在同一页面查看密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20748830/

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