您好,欢迎访问一九零五行业门户网

怎么用php写登入成功跳转

php是一种广泛应用于网络开发的服务器端脚本语言,可以用于动态网页的制作和web应用程序的开发。在网站开发中,登陆系统是非常常见的一种功能,而登陆成功后跳转到指定页面也是用户体验的重要部分。本篇文章将介绍如何使用php编写实现登陆成功跳转的功能。
1.建立表单页面
首先,我们需要创建一个登陆页面,使用户可以在其中输入用户名和密码。为了使该页面更有可读性,我们可以在html中使用css样式来美化页面。我们的表单页面应该包括以下内容:
登陆表单;包含用户名和密码输入框的表单;使用post方法提交表单的按钮;输入错误提示。html代码如下:
<!doctype html><html><head>    <title>登陆页面</title>    <style>        body{            background-color: #f4f4f4;        }                .login{            margin: 100px auto;            border: 1px solid #ccc;            width: 400px;            background-color: #fff;            padding: 20px;            border-radius: 10px;            box-shadow: 0 0 10px #ccc;        }                input[type=text], input[type=password]{            width: 100%;            padding: 10px;            margin: 8px 0;            display: inline-block;            border: 1px solid #ccc;            box-sizing: border-box;            border-radius: 5px;        }                button[type=submit]{            background-color: #4caf50;            color: white;            padding: 10px 18px;            margin: 8px 0;            border: none;            border-radius: 4px;            cursor: pointer;        }                button[type=submit]:hover{            background-color: #45a049;        }                .error{            color: red;        }    </style></head><body>    <div class="login">        <h2>登陆页面</h2>        <form action="process.php" method="post">            <label for="username">用户名</label>            <input type="text" id="username" name="username" placeholder="请输入用户名">            <label for="password">密码</label>            <input type="password" id="password" name="password" placeholder="请输入密码">            <button type="submit">登陆</button>            <?php if(isset($_get['error'])){ if($_get['error'] == 'empty'){ echo "<p class='error'>请输入所有字段!</p>;                    }                    elseif ($_get['error'] == 'wrong') {                        echo <p class='error'>请输入正确的用户名和密码!</p>;                    }                }            ?>        </form>    </div></body></html>
2.验证用户名和密码
接下来,我们需要验证用户输入的用户名和密码是否正确。我们可以在服务器端使用php来实现该过程。首先,我们需要在服务器端创建一个process.php文件。该文件将接收表单数据,验证用户名和密码是否正确。如果正确,我们将在该文件中将用户重定向到另一个页面。
<?php session_start(); if(isset($_post['username']) && isset($_post['password'])){ include 'db_connection.php'; $username = $_post['username']; $password = $_post['password']; if(empty($username) || empty($password)){ header("location: index.php?error=empty"); exit(); } else{ $sql = "select * from users where username='$username' and password='$password'"; $result = mysqli_query($conn, $sql); if(mysqli_num_rows($result) === 1){ $row = mysqli_fetch_assoc($result); if($row['username'] === $username && $row['password'] === $password){ $_session['username'] = $row['username']; header("location: welcome.php"); exit(); } } else{ header("location: index.php?error=wrong"); exit(); } } } else{ header("location: index.php"); exit(); }?>
在上面的代码中,我们首先使用session_start()函数打开一个新的会话。然后,我们检查 $_post 数组中是否存在 username 和 password。如果存在,我们从 db_connection.php 文件导入数据库连接变量,因为我们需要从数据库中检索用户数据。然后,我们使用一个 if 语句来检查输入的字段是否为空。如果为空,则重定向用户到原始表单页,并在 url 中包含错误代码。
如果输入字段不为空,则我们可以检索数据库中该用户的记录。如果我们得到了一个用户名和密码匹配的记录,则我们可以将该用户的 username 存储在 $_session 中,并使用 header() 函数将用户重定向到 welcome.php 页面。如果数据库中没有这样的记录,则我们将重定向用户到原始表单页,并在 url 中包含错误代码。
3.创建欢迎页面
最后,我们需要创建一个欢迎页面,在用户成功登陆后,将其重定向到该页面。欢迎页面可以简单地显示一个欢迎消息,以及一个按钮,该按钮使用户能够注销并退出登录。
<!doctype html><html><head>    <title>欢迎页</title></head><body>    <h1>欢迎</h1>    <p>您已经成功登陆, <?php echo $_session['username']; ?>!</p>    <a href="logout.php">注销</a></body></html>
在欢迎页面中,我们使用 php 的 $_session 机制来显示当前用户名。我们还在页面中添加了一个连接,该链接通过页面 logout.php 文件实现注销功能。
4.实现注销功能
最后,我们需要使用 php 编写 logout.php 文件,以实现注销功能。
<?php session_start(); session_unset(); session_destroy(); header("location: index.php"); exit();?>
在 logout.php 文件中,我们首先使用 session_start() 函数启动会话。我们则使用 session_unset()、session_destroy() 函数来清除所有 session 数据,并将用户重定向回首页。这将使用户在“注销”链接上单击后退出登录。
总结
在这篇文章中,我们介绍了如何使用 php 编写实现登陆成功跳转的简单系统。通过使用 php 和 mysql,在表单提交时验证用户名和密码,在验证成功后使用户跳转到另一个页面,同时在遇到错误时让用户返回原始表单页。我们还介绍了如何在欢迎页面中显示当前已登录的用户名,并在 logout.php 文件中添加注销功能。这是一个简单但功能实用的登陆成功跳转系统,可以在您的网站上为访客提供良好的用户体验。
以上就是怎么用php写登入成功跳转的详细内容。
其它类似信息

推荐信息