php登录注册页面完整代码
时间: 2023-07-26 09:24:14 浏览: 88
这里提供一个简单的PHP登录注册页面的完整代码,仅供参考:
index.php
```php
<?php
session_start();
if(isset($_SESSION['username'])) {
header('Location: home.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login and Registration Form</title>
</head>
<body>
<h1>Login and Registration Form</h1>
<h2>Login</h2>
<form action="login.php" method="POST">
<label>Username:</label>
<input type="text" name="username" required>
<br><br>
<label>Password:</label>
<input type="password" name="password" required>
<br><br>
<button type="submit">Login</button>
</form>
<br>
<h2>Register</h2>
<form action="register.php" method="POST">
<label>Username:</label>
<input type="text" name="username" required>
<br><br>
<label>Password:</label>
<input type="password" name="password" required>
<br><br>
<button type="submit">Register</button>
</form>
</body>
</html>
```
login.php
```php
<?php
session_start();
require_once('db.php');
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $username;
header('Location: home.php');
} else {
echo "Invalid username or password.";
}
?>
```
register.php
```php
<?php
require_once('db.php');
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
$result = mysqli_query($conn, $sql);
if($result) {
echo "Successfully registered.";
} else {
echo "Registration failed.";
}
?>
```
db.php
```php
<?php
$host = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = mysqli_connect($host, $username, $password, $dbname);
if(!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
```
注意:这段代码仅为示例代码,实际使用时需要进行更改和完善,例如加入数据库输入验证和防止SQL注入等。
阅读全文