php登录注册页面完整代码
时间: 2023-09-11 12:09:01 浏览: 65
以下是一个简单的 PHP 登录注册页面的完整代码:
index.php:
```
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login Form</h2>
<form action="login.php" method="post">
<label>Username:</label>
<input type="text" name="username">
<br><br>
<label>Password:</label>
<input type="password" name="password">
<br><br>
<button type="submit" name="login">Login</button>
</form>
<h2>Register Form</h2>
<form action="register.php" method="post">
<label>Username:</label>
<input type="text" name="username">
<br><br>
<label>Password:</label>
<input type="password" name="password">
<br><br>
<button type="submit" name="register">Register</button>
</form>
</body>
</html>
```
login.php:
```
<?php
session_start();
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
$con = mysqli_connect('localhost','root','','php_login');
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result) == 1){
$_SESSION['username'] = $username;
header("location: welcome.php");
exit();
}else{
echo "Invalid username or password";
}
}
?>
```
register.php:
```
<?php
if(isset($_POST['register'])){
$username = $_POST['username'];
$password = $_POST['password'];
$con = mysqli_connect('localhost','root','','php_login');
$sql = "INSERT INTO users (username,password) VALUES ('$username','$password')";
$result = mysqli_query($con,$sql);
if($result){
echo "Registration successful";
}else{
echo "Registration failed";
}
}
?>
```
welcome.php:
```
<?php
session_start();
if(!isset($_SESSION['username'])){
header("location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome <?php echo $_SESSION['username']; ?></h2>
<a href="logout.php">Logout</a>
</body>
</html>
```
logout.php:
```
<?php
session_start();
session_destroy();
header("location: index.php");
exit();
?>
```
请注意,这只是一个基本的登录注册页面,没有安全性和错误处理。在实际使用中,您应该采取更多的措施来保护用户数据和应用程序。
相关推荐
![php](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)