简单的php登录代码
本例无演示,因为就下面这些简单的代码
PHP Code
- <?php
- session_start();
- $conn = mysqli_connect("localhost","root","123456789","freejs");
- $message="";
- if(!emptyempty($_POST["login"])) {
- $result = mysqli_query($conn,"SELECT * FROM users WHERE user_name='" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'");
- $row = mysqli_fetch_array($result);
- if(is_array($row)) {
- $_SESSION["user_id"] = $row['user_id'];
- } else {
- $message = "Invalid Username or Password!";
- }
- }
- if(!emptyempty($_POST["logout"])) {
- $_SESSION["user_id"] = "";
- session_destroy();
- }
- ?>
XML/HTML Code
- <?php if(empty($_SESSION["user_id"])) { ?>
- <form action="" method="post" id="frmLogin">
- <div class="error-message"><?php if(isset($message)) { echo $message; } ?></div>
- <div class="field-group">
- <div><label for="login">Username</label></div>
- <div><input name="user_name" type="text" class="input-field"></div>
- </div>
- <div class="field-group">
- <div><label for="password">Password</label></div>
- <div><input name="password" type="password" class="input-field"> </div>
- </div>
- <div class="field-group">
- <div><input type="submit" name="login" value="Login" class="form-submit-button"></span></div>
- </div>
- </form>
- <?php
- } else {
- $result = mysqlI_query($conn,"SELECT * FROM users WHERE user_id='" . $_SESSION["user_id"] . "'");
- $row = mysqli_fetch_array($result);
- ?>
- <form action="" method="post" id="frmLogout">
- <div class="member-dashboard">Welcome <?php echo ucwords($row['user_name']); ?>, You have successfully logged in!<br>
- Click to <input type="submit" name="logout" value="Logout" class="logout-button">.</div>
- </form>
- </div>
- </div>
- <?php } ?>
原文地址:http://www.freejs.net/article_biaodan_798.html